Jov*_*ita 7 c# reflection system.type comobject gettype
我正在做一个程序,我想做一个反射,但是为此,我需要一个Type类的Object,对吗?使用.GetProperties()方法...所以我尝试了这个:
Type typeName = simObjects.getType();
但是.GetType()返回“ System .__ COMObject”。这没有帮助。.typeof()也会发生同样的情况。我搜索并找到了另一个代码,这个代码:
Type typeName = (Type)Microsoft.VisualBasic.Information.TypeName(simObjects);
但是此方法返回一个String,而我在System.Type中需要它,请问有什么帮助吗?
我没有像我希望的那样使用反射,但它工作得很好。
foreach(PropertyDescriptor descrip in TypeDescriptor.GetProperties(COMObject))
{
if(descrip.Name == "Attribute Name")
{
foreach(PropertyDescriptor descrip2 in TypeDescriptor.GetProperties(descrip))
{
if(descrip2.Name == "sub attribute Name")
{
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码返回属性的名称,例如,假设我的 COMObject 具有以下属性:
int age;
string name;
Son Phill;
Run Code Online (Sandbox Code Playgroud)
和儿子有:
int age;
string name;
Run Code Online (Sandbox Code Playgroud)
在第一个循环中,descrip.Name 将是“age”、“name”和“Phill”,在第二个循环中(假设条件为“Son”返回真),“age”和“name”。
| 归档时间: |
|
| 查看次数: |
15968 次 |
| 最近记录: |