[英]Get BaseType properties with Reflection in C#
我需要列出包含类的所有属性,我有很多,所以我不知道代码中的类型,但是我可以通过prop.BastType.FullName来获取它,但是我不能进行强制转换,并且我不想更改所有类以创建每个“铸造”方法。 为了避免打印诸如DeclaringType,ReflectedType等的Property Properties,我在代码中排除了它们,但是我不喜欢这种方式...
if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
.Contains(prop.Name))
continue;
方法的完整代码:
private static void GetAllPropertiesFor(object oo, int level, List<KeyValuePair<string,string>> result)
{
Type entType = oo.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(entType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(oo, null);
if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
.Contains(prop.Name))
continue;
if (propValue == null)
{
result.Add(new KeyValuePair<string, string>((new string(' ', level * 2)) + prop.Name, "Object"));
//GetAllPropertiesFor(prop, level + (1*3), result);
}
else
{
if (result == null) result = new List<KeyValuePair<string, string>>();
result.Add(new KeyValuePair<string, string>((new string(' ', level)) + prop.Name, propValue.ToString()));
}
if (entType.Namespace == "System.Data.Entity.DynamicProxies" && entType.BaseType.FullName.StartsWith("RE2012.Data.Models."))
{
GetAllPropertiesFor(prop, level + 1, result);
}
// Do something with propValue
}
}
有什么建议么?
这样我没有结果,值:
PropertyType : System.Int32
Attributes : None
CanRead : True
CanWrite : True
正是我所需要的。
免责声明 我无法真正解决您想要的东西,所以此答案只是在黑暗中拍摄
基于我的理解,您希望从特定的类中获取属性,因此这是我的示例:
首先,我的继承
class BaseClass
{
public int Base { get; set; }
}
class ChildClass : BaseClass
{
public double Child { get; set; }
}
class ChildChildClass : ChildClass
{
public string ChildChild { get; set; }
}
现在让我们查找单个属性
class Program
{
static void Main(string[] args)
{
var obj = new ChildChildClass();
var ChildChildType = obj.GetType();
var p = new Program();
// here we get the ChildClass properties
var t = p.getBaseType(ChildChildType, 1);
Console.WriteLine(t.Name);
p.getproperties(t);
// here we get the BaseClass properties
t = p.getBaseType(ChildChildType, 2);
Console.WriteLine(t.Name);
p.getproperties(t);
// here we get the Object properties
t = p.getBaseType(ChildChildType, 3);
Console.WriteLine(t.Name);
p.getproperties(t);
Console.Read();
}
internal Type getBaseType(Type t, int level)
{
Type temp ;
for (int i = 0; i < level; i++)
{
temp = t.BaseType;
if(temp == null)
throw new ArgumentOutOfRangeException("you are digging to deep");
else
t = temp;
}
return t;
}
private void getproperties(Type t)
{
PropertyInfo[] properties = t.GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
Console.WriteLine("");
}
}
如果你想了解的BindingFlags一些信息是一个链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.