Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用TypeDescriptor获取私有属性_C#_Reflection_Private Members_Typedescriptor - Fatal编程技术网

C# 使用TypeDescriptor获取私有属性

C# 使用TypeDescriptor获取私有属性,c#,reflection,private-members,typedescriptor,C#,Reflection,Private Members,Typedescriptor,我想使用c#中的TypeDescriptor获取类的私有属性 到目前为止 TypeDescriptor.GetProperties(myType); 仅返回公共的非静态属性 我还没有找到一种方法来影响GetProperties或GetProvider方法,迫使它们返回“默认”(公共、非静态)成员以外的其他成员 请不要建议反射(我很清楚BindingFlags),除非它给我一个PropertyDescriptor对象。要做到这一点,您必须编写并注册一个使用反射的自定义TypeDescriptio

我想使用c#中的TypeDescriptor获取类的私有属性

到目前为止

TypeDescriptor.GetProperties(myType);
仅返回公共的非静态属性

我还没有找到一种方法来影响GetPropertiesGetProvider方法,迫使它们返回“默认”(公共、非静态)成员以外的其他成员


请不要建议反射(我很清楚BindingFlags),除非它给我一个PropertyDescriptor对象。

要做到这一点,您必须编写并注册一个使用反射的自定义
TypeDescriptionProvider
。但是,您当然可以这样做—您甚至可以拥有实际与字段(而不是属性)对话的
PropertyDescriptor
实例。您可能还需要编写自己的bespke
PropertyDescriptor
实现,因为
ReflectPropertyDescriptor
internal
(您可能可以使用反射来获得它)。最终,您必须使用反射来实现,但是您可以实现
TypeDescriptor.GetProperties(Type)
返回所需的
PropertyDescriptor
实例的要求

您也可以对无法控制的类型执行此操作。然而,应该强调的是,你的意图是不寻常的

如果您使用的是
.GetProperties(instance)
重载,那么还可以通过实现
ICustomTypeDescriptor
来实现这一点,它比完整的
TypeDescriptionProvider
更简单


有关挂接定制提供商的示例,请参见您可以创建自己的
CustomPropertyDescriptor
,它从
PropertyInfo
获取信息

最近我需要获取非公共属性的
PropertyDescriptorCollection

在使用
type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)
获取非公共属性之后,我使用以下类创建相应的
PropertyDescriptor

class CustomPropertyDescriptor : PropertyDescriptor
{
    PropertyInfo propertyInfo;
    public CustomPropertyDescriptor(PropertyInfo propertyInfo)
        : base(propertyInfo.Name, Array.ConvertAll(propertyInfo.GetCustomAttributes(true), o => (Attribute)o))
    {
        this.propertyInfo = propertyInfo;
    }
    public override bool CanResetValue(object component)
    {
        return false;
    }

    public override Type ComponentType
    {
        get
        {
            return this.propertyInfo.DeclaringType;
        }
    }

    public override object GetValue(object component)
    {
        return this.propertyInfo.GetValue(component, null);
    }

    public override bool IsReadOnly
    {
        get
        {
            return !this.propertyInfo.CanWrite;
        }
    }

    public override Type PropertyType
    {
        get
        {
            return this.propertyInfo.PropertyType;
        }
    }

    public override void ResetValue(object component)
    {
    }

    public override void SetValue(object component, object value)
    {
        this.propertyInfo.SetValue(component, value, null);
    }

    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }
}

我不知道用
TypeDescriptor
,而不使用反射,有什么方法可以做到这一点。您不想使用反射的原因是什么?如果您需要调用,那么TypeDescriptor会快得多,而使用TypeDescriptor是无法做到的。你需要求助于反思。此外,如果需要从外部访问它,它应该是公共的,而不是私人的。你说得有一半是对的。您可以“教育”
TypeDescriptor
返回OP想要的内容,但您需要使用反射来实现。然而,调用代码只知道
TypeDescriptor
PropertyDescriptor
。我以前见过这个想法,看起来很可怕。你有例子吗?@BanditoBunny我已经在hyperdescriptor的例子中编辑过了-有什么用吗?很好,我们已经有了HyperTypeProvider的自定义实现(使用il代码),并且我们已经更改了属性提取块的BindingFlags,并使用了自定义的HyperTypeProvider,而不是默认的。