[英]How do I get data type of property in a class through Reflection C#
I have a namespace called MyNamespace.Demo1
, one static class called STClass
and one property inside that class called prpAssm
. 我有一个名为命名空间MyNamespace.Demo1
,一个叫静态类STClass
和类调用内部一个属性prpAssm
。 How do I get the data-type of property prpAssm
? 如何获取属性prpAssm
的数据类型?
Type classType = typeof(STClass);
PropertyInfo propertyInfo = classType.GetProperty("prpAssm");
Type propertyType = propertyInfo.PropertyType;
if the class is in another project you should load the assembly. 如果该类在另一个项目中,则应加载程序集。
var assembly = Assembly.LoadFrom(@"MyNamespace.Demo1.dll");
Type classType = assembly.GetType("MyNamespace.Demo1.STClass");
PropertyInfo propertyInfo = classType.GetProperty("prpAssm");
Type propertyType = propertyInfo.PropertyType;
BR BR
Try this:- (Use PropertyInfo ) 试试这个:-(使用PropertyInfo )
Assembly myAss = Assembly.LoadFile(@"YourAssemblyPath.dll");
Type myType = myAss.GetType("MyNamespace.Demo1.STClass");
PropertyInfo myProp = myType.GetProperty("prpAssm");
Console.WriteLine(myProp.PropertyType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.