[英]Get nested generic type object's property and attribute values through Reflection at run time
I created a custom attribute class 我创建了一个自定义属性类
[AttributeUsage(AttributeTargets.Property)]
public class MyCustomAttribute : Attribute
{
public string Name{ get; set; }
}
I have a complex nested object below: 我在下面有一个复杂的嵌套对象:
public class Parent
{
[MyCustom(Name = "Parent property 1")]
public string ParentProperty1 { get; set; }
[MyCustom(Name = "Parent property 2")]
public string ParentProperty2 { get; set; }
public Child ChildObject { get; set; }
}
public class Child
{
[MyCustom(Name = "Child property 1")]
public string ChildPropery1 { get; set; }
[MyCustom(Name = "Child property 2")]
public string ChildProperty2 { get; set; }
}
I want to get the list of property names, attribute name value for each property if this object is passed in as a generic object at run time, how can I do this if the input object at run-time is "Parent"? 如果要在运行时将该对象作为通用对象传入,我想获取属性名称列表,每个属性的属性名称值,如果运行时输入对象是“父对象”,该怎么办?
I know how to do this for a flat structure generic object using the code below, but I'm unsure how to retrieve all nested object property and attribute, do I need to use some sort of recursive function? 我知道如何使用下面的代码对平面结构通用对象执行此操作,但是我不确定如何检索所有嵌套对象的属性和属性,是否需要使用某种递归函数?
public void GetObjectInfo<T>(T object)
{
//Get the object list of properties.
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
//Get the attribute object of each property.
var attribute = property.GetCustomAttribute<MyCustomAttribute>();
}
}
Note, the object I used is a very simplistic version of what is in real life, I could have multiple level of nested child or nested list/array ect.. 注意,我使用的对象是现实生活中的一个非常简单的版本,我可以有多个级别的嵌套子级或嵌套列表/数组等。
You can create a function that recursively enumerates properties with your custom attribute. 您可以创建一个函数,该函数以自定义属性递归枚举属性。
public static IEnumerable<PropertyInfo> EnumeratePropertiesWithMyCustomAttribute(Type type)
{
if (type == null)
{
throw new ArgumentNullException();
}
foreach (PropertyInfo property in type.GetProperties())
{
if (property.HasCustomAttribute<MyCustomAttribute>())
{
yield return property;
}
if (property.PropertyType.IsReferenceType && property.PropertyType != typeof(string)) // only search for child properties if doing so makes sense
{
foreach (PropertyInfo childProperty in EnumeratePropertiesWithMyCustomAttributes(property.PropertyType))
{
yield return childProperty;
}
}
}
}
Note: this function accepts a type. 注意:此函数接受类型。 To call it on an object: 在对象上调用它:
public static IEnumerable<PropertyInfo> EnumeratePropertiesWithMyCustomAttributes(object obj)
{
if (obj == null)
{
throw new ArgumentNullException();
}
return EnumeratePropertiesWithMyCustomAttributes(obj.GetType());
}
To get the full list: 要获取完整列表:
Parent parent = new Parent();
PropertyInfo[] propertiesWithMyCustomAttribute = EnumeratePropertiesWithMyCustomAttributes(parent).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.