简体   繁体   English

使用反射获取类名称的所有属性

[英]Get all properties for a class name using reflection

I am loading the dll as shown below, 我正在加载dll,如下所示,

Type[] _Type = Assembly.GetAssembly(typeof(StdAdapter)).GetTypes();

Now I want to get all the properties for a particular 'class name' which is being passed as a string. 现在,我想获取作为字符串传递的特定“类名”的所有属性。

Please suggest how to achieve this. 请建议如何实现。

Thanks 谢谢

You can use 您可以使用

Type objectType = Type.GetType("ClassName");

When you get this time you can use the type for further reflection: 到了这个时候,您可以使用该类型进行进一步的思考:

objectType.GetProperties();

which will return the properties. 这将返回属性。

This mean insteaf of your call into an array, you could use: 这意味着将您的调用插入数组,可以使用:

Assembly.GetAssembly(typeof(StdAdapter)).GetType("ClassName");

The only other thing would be to loop through the array in order to find the right Type, not sure performance wise which would be better, i'd go with GetType() though. 唯一的另一件事是遍历数组以找到正确的Type,不确定性能是否会更好,尽管如此,我还是选择了GetType()。

The Type.GetProperties() function returns an array of PropertyInfo objects. Type.GetProperties()函数返回PropertyInfo对象的数组。

So you would have: 因此,您将拥有:

foreach(Type current in _Type)
{
  PropertyInfo[] properties = current.GetProperties();
}

You could also do: typeof(StdAdapter).GetProperties() 您也可以这样做:typeof(StdAdapter).GetProperties()

您可以使用一点Linq to Objects从数组中获取所需的类:

_Type.SingleOrDefault(t => t.Name == "CLASS_NAME").GetProperties();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM