使用反射读取属性的属性

发布于 2024-12-02 04:02:23 字数 376 浏览 0 评论 0原文

我需要使用反射读取属性的属性

例如我得到以下信息:

    [XmlElement("Id")]
    [CategoryAttribute("Main"), ReadOnly(true),
    Description("This property is auto-generated")]
    [RulesCriteria("ID")]
    public override string Id
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

我想使用反射获取该属性的“只读”值 有人可以帮忙吗

I need to read the attribute of a property using reflection

For example I get the following :

    [XmlElement("Id")]
    [CategoryAttribute("Main"), ReadOnly(true),
    Description("This property is auto-generated")]
    [RulesCriteria("ID")]
    public override string Id
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

i want to get the " read only "value of this property using reflection
can anybody help

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

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

发布评论

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

评论(2

诗笺 2024-12-09 04:02:23
public static bool PropertyReadOnlyAttributeValue(PropertyInfo property)
{
    ReadonlyAttribute attrib = Attribute.GetCustomAttribute(property, typeof(ReadOnlyAttribute));
    return attrib != null && attrib.IsReadOnly;
}

public static bool PropertyReadOnlyAttributeValue(Type type, string propertyName)
{
    return PropertyReadOnlyAttributeValue(type.GetProperty(propertyName));
}

public static bool PropertyReadOnlyAttributeValue(object instance, string propertyName)
{
    if (instance != null)
    {
        Type type = instance.GetType();
        return PropertyReadOnlyAttributeValue(type, propertyName);
    }
    return false;
}
public static bool PropertyReadOnlyAttributeValue(PropertyInfo property)
{
    ReadonlyAttribute attrib = Attribute.GetCustomAttribute(property, typeof(ReadOnlyAttribute));
    return attrib != null && attrib.IsReadOnly;
}

public static bool PropertyReadOnlyAttributeValue(Type type, string propertyName)
{
    return PropertyReadOnlyAttributeValue(type.GetProperty(propertyName));
}

public static bool PropertyReadOnlyAttributeValue(object instance, string propertyName)
{
    if (instance != null)
    {
        Type type = instance.GetType();
        return PropertyReadOnlyAttributeValue(type, propertyName);
    }
    return false;
}
谎言 2024-12-09 04:02:23

如果不知道类型名称,就很难为您的案例编写代码。希望下面的例子有帮助。

using System;
using System.Reflection;

public class Myproperty
{
    private string caption = "Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.PropertyInfo");

        // Define a property.
        Myproperty Myproperty = new Myproperty();
        Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);

        // Get the type and PropertyInfo.
        Type MyType = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");

        // Get and display the attributes property.
        PropertyAttributes Myattributes = Mypropertyinfo.Attributes;

        Console.Write("\nPropertyAttributes - " + Myattributes.ToString());

        return 0;
    }
}

MSDN

It's difficult to write the code for your case without knowing the Type name. Hope below example helps.

using System;
using System.Reflection;

public class Myproperty
{
    private string caption = "Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.PropertyInfo");

        // Define a property.
        Myproperty Myproperty = new Myproperty();
        Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);

        // Get the type and PropertyInfo.
        Type MyType = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");

        // Get and display the attributes property.
        PropertyAttributes Myattributes = Mypropertyinfo.Attributes;

        Console.Write("\nPropertyAttributes - " + Myattributes.ToString());

        return 0;
    }
}

MSDN

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