.NET 反射:无法检索动态属性

发布于 2024-12-28 06:47:15 字数 5113 浏览 1 评论 0原文

我正在借助反射向类添加动态属性。但无法找到新的/动态属性。

过程: 我通过实现 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

}

I am adding dynamic property to a class with the help of Reflection. But unable to find new/dynamic Properties.

Process: I have created a DynamicClass by implementing ICustomTypeDescriptor Interface and implements the function GetPropertiesI, I did my all manipulation here to get updated properties but it is not working..

My DynamicClass code is here:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2025-01-04 06:47:15

To retrieve runtime properties of an object, use the System.ComponentModel.TypeDescriptor.GetProperties() method, instead of the Type.GetProperties() method, which only retrieves compile-time properties of the type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文