C# C中属性的自定义属性#

C# C中属性的自定义属性#,c#,reflection,custom-properties,C#,Reflection,Custom Properties,我有一个有两个属性的类人 public sealed class Person { [Description("This is the first name")] public string FirstName { get; set; } [Description("This is the last name")] public string LastName { get; set; } } 在我的控制台应用程序代码中,我希望为每个实例的每个属性获取Description属性的值 类似于 Pe

我有一个有两个属性的类人

public sealed class Person
{
[Description("This is the first name")]
public string FirstName { get; set; }
[Description("This is the last name")]
public string LastName { get; set; }
}
在我的控制台应用程序代码中,我希望为每个实例的每个属性获取Description属性的值

类似于

Person myPerson = new Person();
myPerson.LastName.GetDescription() // method to retrieve the value of the attribute
有可能完成这项任务吗? 有人能给我提个建议吗? 顺致敬意,
Fab

使用这种语法是不可能的。使用表达式树是可能的。。。例如:

public static class DescripionExtractor 
{
    public static string GetDescription<TValue>(Expression<Func<TValue>> exp) 
    {
        var body = exp.Body as MemberExpression;

        if (body == null) 
        {
            throw new ArgumentException("exp");
        }

        var attrs = (DescriptionAttribute[])body.Member.GetCustomAttributes(typeof(DescriptionAttribute), true);

        if (attrs.Length == 0) 
        {
            return null;
        }

        return attrs[0].Description;
    }
}

请注意,
person
的值与此无关。它可以是
null
,并且一切都将正常工作,因为
person
实际上没有被访问。只有它的类型才是重要的。

使用这种语法是不可能的。使用表达式树是可能的。。。例如:

public static class DescripionExtractor 
{
    public static string GetDescription<TValue>(Expression<Func<TValue>> exp) 
    {
        var body = exp.Body as MemberExpression;

        if (body == null) 
        {
            throw new ArgumentException("exp");
        }

        var attrs = (DescriptionAttribute[])body.Member.GetCustomAttributes(typeof(DescriptionAttribute), true);

        if (attrs.Length == 0) 
        {
            return null;
        }

        return attrs[0].Description;
    }
}

请注意,
person
的值与此无关。它可以是
null
,并且一切都将正常工作,因为
person
实际上没有被访问。只有它的类型才是重要的。

LastName返回一个字符串,因此您不能从那里做很多事情。最终,您需要
属性info
。有两种方法可以做到这一点:

  • 通过
    表达式
    (可能是
    SomeMethod(p=>p.LastName)
  • 字符串
    (可能通过
    名称
例如,您可以执行以下操作:

var desc = Helper.GetDescription<Person>(nameof(Person.LastName));
var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(
    type.GetProperty(propertyName), typeof(DescriptionAttribute));
return attrib?.Description;
比如:

var desc = Helper.GetDescription<Person>(nameof(Person.LastName));
var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(
    type.GetProperty(propertyName), typeof(DescriptionAttribute));
return attrib?.Description;

.LastName
返回一个字符串,因此您不能从中执行太多操作。最终,您需要
属性info
。有两种方法可以做到这一点:

  • 通过
    表达式
    (可能是
    SomeMethod(p=>p.LastName)
  • 字符串
    (可能通过
    名称
例如,您可以执行以下操作:

var desc = Helper.GetDescription<Person>(nameof(Person.LastName));
var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(
    type.GetProperty(propertyName), typeof(DescriptionAttribute));
return attrib?.Description;
比如:

var desc = Helper.GetDescription<Person>(nameof(Person.LastName));
var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(
    type.GetProperty(propertyName), typeof(DescriptionAttribute));
return attrib?.Description;

使用这种语法不,这是不可能的。对于其他语法,它是可能的(如链接问题中的示例所示),语法是不可能的。考虑到nameof仅在C#6.0中可用,因此使用其他语法是可能的(如链接问题中所示)。@Alexey I确实使用了“也许”一词。)好吧,考虑到这个名字,因为只有C#6.0才有。@Alexey我确实用了“也许”这个词。)