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"] ;

  • 相关阅读:
    [HNOI2002]彩票
    贼有意思[最长上升公共子序列](SAC大佬测试题)
    空间漫游(SAC大佬的测试)
    计蒜客NOIP模拟赛D2T3 数三角形
    计蒜客NOIP模拟赛D2T2 直线的交点
    计蒜客NOIP模拟赛(3)D2T1 小区划分
    计蒜客NOIP模拟赛(3)D1T3 任性的国王
    UpdateAfterEvent
    长链剖分学习笔记
    P4292 [WC2010]重建计划
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1665830.html
Copyright © 2011-2022 走看看