简体   繁体   中英

Get all properties for a class name using reflection

I am loading the dll as shown below,

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.

The Type.GetProperties() function returns an array of PropertyInfo objects.

So you would have:

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

You could also do: typeof(StdAdapter).GetProperties()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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