zoukankan      html  css  js  c++  java
  • 对泛型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"] ;

  • 相关阅读:
    jQuery基础学习5——JavaScript方法获取页面中的元素
    jQuery基础学习4——jQuery容错性
    jQuery基础学习3——jQuery库冲突
    jQuery基础学习2——DOM和jQuery对象
    Windows Server 2003 R2 64位简体中文版下载
    jQuery基础学习1
    关闭Linux里边的selinux
    linux下安装apache详解
    Autolayout学习(1)-了解Autoreszing
    新浪微博客户端(64)-下拉放大
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1665830.html
Copyright © 2011-2022 走看看