C# 按属性值获取属性名
您好,如果我有该属性的自定义属性值,如何获取该类的属性名?当然还有自定义属性名。通过自定义属性获取属性名:C# 按属性值获取属性名,c#,attributes,C#,Attributes,您好,如果我有该属性的自定义属性值,如何获取该类的属性名?当然还有自定义属性名。通过自定义属性获取属性名: public static string[] GetPropertyNameByCustomAttribute <ClassToAnalyse, AttributeTypeToFind> ( Func<AttributeTypeToFind, bool> attributePredicate )
public static string[] GetPropertyNameByCustomAttribute
<ClassToAnalyse, AttributeTypeToFind>
(
Func<AttributeTypeToFind, bool> attributePredicate
)
where AttributeTypeToFind : Attribute
{
if (attributePredicate == null)
{
throw new ArgumentNullException("attributePredicate");
}
else
{
List<string> propertyNames = new List<string>();
foreach
(
PropertyInfo propertyInfo in typeof(ClassToAnalyse).GetProperties()
)
{
if
(
propertyInfo.GetCustomAttributes
(
typeof(AttributeTypeToFind), true
).Any
(
currentAttribute =>
attributePredicate((AttributeTypeToFind)currentAttribute)
)
)
{
propertyNames.Add(propertyInfo.Name);
}
}
return propertyNames.ToArray();
}
}
public class FooAttribute : Attribute
{
public String Description { get; set; }
}
class FooClass
{
private int fooProperty = 42;
[Foo(Description="Foo attribute description")]
public int FooProperty
{
get
{
return this.fooProperty;
}
}
}
// It will return "FooProperty"
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Foo attribute description"
);
// It will return an empty array
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Bar attribute description"
);
测试用例:
public static string[] GetPropertyNameByCustomAttribute
<ClassToAnalyse, AttributeTypeToFind>
(
Func<AttributeTypeToFind, bool> attributePredicate
)
where AttributeTypeToFind : Attribute
{
if (attributePredicate == null)
{
throw new ArgumentNullException("attributePredicate");
}
else
{
List<string> propertyNames = new List<string>();
foreach
(
PropertyInfo propertyInfo in typeof(ClassToAnalyse).GetProperties()
)
{
if
(
propertyInfo.GetCustomAttributes
(
typeof(AttributeTypeToFind), true
).Any
(
currentAttribute =>
attributePredicate((AttributeTypeToFind)currentAttribute)
)
)
{
propertyNames.Add(propertyInfo.Name);
}
}
return propertyNames.ToArray();
}
}
public class FooAttribute : Attribute
{
public String Description { get; set; }
}
class FooClass
{
private int fooProperty = 42;
[Foo(Description="Foo attribute description")]
public int FooProperty
{
get
{
return this.fooProperty;
}
}
}
// It will return "FooProperty"
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Foo attribute description"
);
// It will return an empty array
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Bar attribute description"
);
//它将返回“FooProperty”
GetPropertyNameByCustomAttribute
(
属性=>attribute.Description==“Foo属性描述”
);
//它将返回一个空数组
GetPropertyNameByCustomAttribute
(
属性=>attribute.Description==“条形图属性描述”
);
通过自定义属性获取属性名:
public static string[] GetPropertyNameByCustomAttribute
<ClassToAnalyse, AttributeTypeToFind>
(
Func<AttributeTypeToFind, bool> attributePredicate
)
where AttributeTypeToFind : Attribute
{
if (attributePredicate == null)
{
throw new ArgumentNullException("attributePredicate");
}
else
{
List<string> propertyNames = new List<string>();
foreach
(
PropertyInfo propertyInfo in typeof(ClassToAnalyse).GetProperties()
)
{
if
(
propertyInfo.GetCustomAttributes
(
typeof(AttributeTypeToFind), true
).Any
(
currentAttribute =>
attributePredicate((AttributeTypeToFind)currentAttribute)
)
)
{
propertyNames.Add(propertyInfo.Name);
}
}
return propertyNames.ToArray();
}
}
public class FooAttribute : Attribute
{
public String Description { get; set; }
}
class FooClass
{
private int fooProperty = 42;
[Foo(Description="Foo attribute description")]
public int FooProperty
{
get
{
return this.fooProperty;
}
}
}
// It will return "FooProperty"
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Foo attribute description"
);
// It will return an empty array
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Bar attribute description"
);
测试用例:
public static string[] GetPropertyNameByCustomAttribute
<ClassToAnalyse, AttributeTypeToFind>
(
Func<AttributeTypeToFind, bool> attributePredicate
)
where AttributeTypeToFind : Attribute
{
if (attributePredicate == null)
{
throw new ArgumentNullException("attributePredicate");
}
else
{
List<string> propertyNames = new List<string>();
foreach
(
PropertyInfo propertyInfo in typeof(ClassToAnalyse).GetProperties()
)
{
if
(
propertyInfo.GetCustomAttributes
(
typeof(AttributeTypeToFind), true
).Any
(
currentAttribute =>
attributePredicate((AttributeTypeToFind)currentAttribute)
)
)
{
propertyNames.Add(propertyInfo.Name);
}
}
return propertyNames.ToArray();
}
}
public class FooAttribute : Attribute
{
public String Description { get; set; }
}
class FooClass
{
private int fooProperty = 42;
[Foo(Description="Foo attribute description")]
public int FooProperty
{
get
{
return this.fooProperty;
}
}
}
// It will return "FooProperty"
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Foo attribute description"
);
// It will return an empty array
GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
(
attribute => attribute.Description == "Bar attribute description"
);
//它将返回“FooProperty”
GetPropertyNameByCustomAttribute
(
属性=>attribute.Description==“Foo属性描述”
);
//它将返回一个空数组
GetPropertyNameByCustomAttribute
(
属性=>attribute.Description==“条形图属性描述”
);
请解释更多,您的问题不是很清楚,可能会结束。请解释更多,您的问题不是很清楚,可能会结束。这与属性有什么关系?诚然,这个问题并不清楚,但答案中甚至没有“属性”这个词。不知道为什么会被否决。我对这个问题的看法是OP询问如何检索给定对象的可调用属性列表。考虑到问题中缺乏信息,这个答案似乎是正确的。很明显,我知道他想知道如何使用反射API,因为我听到了关于反射API的其他相关问题,我知道对于不知道这个API的人,很难解释他的意图。你应该得到属性的属性:propertyInfo.attributes,这将有助于回答这个问题。或者对于自定义属性:这与属性有什么关系?诚然,这个问题并不清楚,但答案中甚至没有“属性”这个词。不知道为什么会被否决。我对这个问题的看法是OP询问如何检索给定对象的可调用属性列表。考虑到问题中缺乏信息,这个答案似乎是正确的。很明显,我知道他想知道如何使用反射API,因为我听到了关于反射API的其他相关问题,我知道对于不知道这个API的人,很难解释他的意图。你应该得到属性的属性:propertyInfo.attributes,这将有助于回答这个问题。或对于自定义属性: