2012-01-20 39 views
5

我在反射的幫助下將動態屬性添加到類。但無法找到新的/動態的屬性。.NET反射:無法檢索動態屬性

過程:我已經通過實施ICustomTypeDescriptor接口創建一個DynamicClass並實現功能GetPropertiesI,我做了我這裏的所有操作以獲取更新的特性,但它不工作..

我DynamicClass代碼是在這裏:

public class DynamicClass : ICustomTypeDescriptor 
{ 
    // Collection to code add dynamic properties 
    /* 
    KeyedCollection<string, DynamicProperty> _properties; 
    public KeyedCollection<string, DynamicProperty> Properties 
    { 
     get;// { return _properties; } 
     set;// { _properties = value; } 
    } 
    */ 
    public void AddProperty(DynamicProperty _property) 
    { 
     PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    // Fill property collection with founded properties 
    PropertyDescriptorCollection propsCollection = 
     new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray()); 

    propsCollection.Add(new DynamicPropertyDescriptor(
     _property.ComponentType, 
     _property.PropertyName, 
     _property.PropertyType, 
     _property.Component, 
     _property.Value 
    )); 
} 

public void AddProperties(KeyedCollection<string, DynamicProperty> _properties) 
{ 
    PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    // Fill property collection with founded properties 
    PropertyDescriptorCollection propsCollection = 
     new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray()); 

    foreach (var p in _properties) 
    { 
     propsCollection.Add(new DynamicPropertyDescriptor(
      p.ComponentType, 
      p.PropertyName, 
      p.PropertyType, 
      p.Component, 
      p.Value 
      )); 
    } 
} 

PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() 
{ 
    PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    PropertyDescriptorCollection propsCollection = 
     new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray()); 
    return propsCollection; 
} 

// ICustomTypeDescriptor implementation 
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() 
{ 
    // Properties founded within instance 
    PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    // Fill property collection with founded properties 
    PropertyDescriptorCollection propsCollection = 
     new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray()); 
    //propsCollection.Cast.<CustomPropertyDescriptor>().Concat(Properties).ToArray(); 

    // Fill property collection with dynamic properties (Properties) 

    //List<DynamicPropertyDescriptor> _dynamicPropertyDescriptors = new List<DynamicPropertyDescriptor>(); 

    //instanceProps.Cast<DynamicPropertyDescriptor>().Concat().ToArray(); 
    return propsCollection; 
} 

#region ICustomTypeDescriptor Members 

public System.ComponentModel.AttributeCollection GetAttributes() 
{ 
    throw new NotImplementedException(); 
} 

public string GetClassName() 
{ 
    throw new NotImplementedException(); 
} 

public string GetComponentName() 
{ 
    throw new NotImplementedException(); 
} 

public TypeConverter GetConverter() 
{ 
    throw new NotImplementedException(); 
} 

public EventDescriptor GetDefaultEvent() 
{ 
    throw new NotImplementedException(); 
} 

public PropertyDescriptor GetDefaultProperty() 
{ 
    throw new NotImplementedException(); 
} 

public object GetEditor(Type editorBaseType) 
{ 
    throw new NotImplementedException(); 
} 

public EventDescriptorCollection GetEvents(Attribute[] attributes) 
{ 
    throw new NotImplementedException(); 
} 

public EventDescriptorCollection GetEvents() 
{ 
    throw new NotImplementedException(); 
} 

public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
{ 
    PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    PropertyDescriptorCollection propsCollection = 
     new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray()); 
    return propsCollection; 

    /* 
    if (!this.DesignMode) 
    { 
     PropertyDescriptorCollection collection = TypeDescriptor.GetProperties(this, attributes, true); 

     PropertyDescriptorCollection newList = new PropertyDescriptorCollection(new PropertyDescriptor[0]); 

     foreach (PropertyDescriptor prop in collection) 
     { 
      Attribute a = prop.Attributes[typeof(CategoryAttribute)]; 

      if (a != null && ((CategoryAttribute)a).Category == "SixDisciplines") 
       newList.Add(prop); 
     } 

     return newList; 
    } 
    else 
     //The control must be passed to the method. 
     return TypeDescriptor.GetProperties(this, attributes, true); 
    */ 
} 

public object GetPropertyOwner(PropertyDescriptor pd) 
{ 
    throw new NotImplementedException(); 
} 

#endregion 

}

+5

'GetProperties()'反映了編譯時'類型',而不是實例。這就是爲什麼你沒有看到新的屬性。 –

+0

那麼告訴我該怎麼辦? –

+0

請參閱下面的答案。 –

回答

3

要檢索的對象的運行屬性,使用System.ComponentModel.TypeDescriptor.GetProperties()實現方法具d而不是Type.GetProperties()方法,該方法僅檢索type的編譯時屬性。

+0

進程沒有觸發函數ICustomTypeDescriptor.GetProperties()... !! –

+0

你想做什麼似乎不必要的複雜。你確定C#'dynamic'關鍵字不夠用(你使用的是什麼版本的框架)?請參閱http://stackoverflow.com/questions/6166236/add-properties-at-runtime,然後查看http://blogs.msdn.com/b/parthopdas/archive/2006/01/03/509103 .aspx,靠近底部。 –