2012-01-20 35 views
5

Tôi đang thêm thuộc tính động vào một lớp với sự trợ giúp của Reflection. Nhưng không thể tìm thuộc tính mới/động..NET Reflection: Không thể truy xuất các thuộc tính động

Process: tôi đã tạo ra một DynamicClass bằng cách thực hiện ICustomTypeDescriptor Giao diện và thực hiện các chức năng GetPropertiesI, tôi đã làm tất cả các thao tác của tôi ở đây để có được thuộc tính được cập nhật nhưng nó không làm việc ..

đang DynamicClass của tôi là đây :

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()' phản ánh qua thời gian biên dịch 'loại', không phải là cá thể. Đó là lý do tại sao bạn không nhìn thấy các thuộc tính mới. –

+0

Vì vậy, hãy cho tôi biết phải làm gì? –

+0

Xem câu trả lời của tôi bên dưới. –

Trả lời

3

Để lấy các thuộc tính thời gian chạy của một đối tượng, sử dụng System.ComponentModel.TypeDescriptor.GetProperties() metho d, thay vì phương thức Type.GetProperties(), chỉ lấy các thuộc tính biên dịch của type.

+0

quá trình không kích hoạt chức năng ICustomTypeDescriptor.GetProperties() ... !! –

+0

Điều bạn muốn làm có vẻ phức tạp không cần thiết. Bạn có chắc là từ khóa C# 'dynamic' không đủ (phiên bản nào của khung công tác bạn đang sử dụng)? Xem http://stackoverflow.com/questions/6166236/add-properties-at-runtime, sau đó xem http://blogs.msdn.com/b/parthopdas/archive/2006/01/03/509103 .aspx, gần phía dưới. –

Các vấn đề liên quan