• 对泛型T的属性操作


    代码
     public class PropertyHelper<T> 
       {      
          
    private T entiy;      

          
    public PropertyHelper(T t)       

        {  entiy 
    = t;            

        BindingAttr 
    = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;      

        }      

      
    public BindingFlags BindingAttr { getset; }      
      
    public T Entiy  { get { return entiy; } }   

      
    public object this[string name] { get{  return this[name, null];  } set { this[name,null= value; }}       
     
    public object this[string name,object[] index] { get {  if (name == nullthrow new ArgumentNullException("name");  object obj = null
    try  {  
           PropertyInfo info 
    = entiy.GetType().GetProperty(name, BindingAttr);  
           obj 
    = info.GetValue(entiy, index);  }  
    catch (Exception e)  
    throw new Exception(e.Message);   }  
      
    return obj;  } 
    set  {  if (name == null)  throw new ArgumentNullException("name"); 
           
    try   { PropertyInfo info = entiy.GetType().GetProperty(name, BindingAttr);
           info.SetValue(entiy,value,index); 
         }  
         
    catch (Exception e)  
         { 
           
    throw new Exception(e.Message); 
          } 
        } 
       } 
     }

    PropertyHelper<BomG> bh = new PropertyHelper<BomG>(bomg);

    string name = bh["Name"] as string;

    2.string dec = "qazxswedc";

    ProPertyHelper<string> dechelper = new PropertyHelper<string>(dec);

    int leng = (int)dechelper["Length"] ;

  • 相关阅读:
    [extjs] ExtJs4.2 Form 表单提交
    [java ] java.util.zip.ZipException: error in opening zip file
    Oracle 11g 执行计划管理1
    Oracle 分区表的统计信息实例
    Oracle 手动收集统计信息
    Oracle 10g 之自动收集统计信息
    Oracle 11g 之自动收集统计信息
    Oracle 11gR2 RAC修改SCAN IP
    共享内存shared pool (6):追踪sql语句
    共享内存shared pool (5):详解一条SQL在library cache中解析
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1665830.html
Copyright © 2020-2023  润新知