2012-01-20 14 views
5

Estoy agregando propiedad dinámica a una clase con la ayuda de Reflection. Pero no puede encontrar Propiedades nuevas/dinámicas..NET Reflection: no se pueden recuperar las propiedades dinámicas

proceso: He creado una DynamicClass mediante la implementación de la interfaz ICustomTypeDescriptor e implementa la función GetPropertiesI, hice todo tipo de manipulación aquí para obtener las propiedades actualizadas pero no está funcionando ..

Mi código DynamicClass está aquí :

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()' refleja el 'tipo' en tiempo de compilación, no la instancia. Es por eso que no estás viendo las nuevas propiedades. –

+0

Entonces dime que hacer? –

+0

Ver mi respuesta a continuación. –

Respuesta

3

Para recuperar las propiedades de ejecución de un objeto, utilice el metho System.ComponentModel.TypeDescriptor.GetProperties() d, en lugar del método Type.GetProperties(), que solo recupera las propiedades de tiempo de compilación del type.

+0

proceso que no activa la función ICustomTypeDescriptor.GetProperties() ... !! –

+0

Lo que quiere hacer parece innecesariamente complicado. ¿Estás seguro de que la palabra clave '' dynamic' de C# no sería suficiente (¿qué versión del framework estás usando)? Consulte http://stackoverflow.com/questions/6166236/add-properties-at-runtime, y luego eche un vistazo a http://blogs.msdn.com/b/parthopdas/archive/2006/01/03/509103 .aspx, cerca de la parte inferior. –

Cuestiones relacionadas