【发布时间】:2015-11-17 08:30:45
【问题描述】:
我有一个基类 CrmObject 和一些继承它的类(Aufgabe、Kontakt、...)。我只有一个子类的字符串值,我想在一个语句中同时获取 CrmObject 的属性和特定的子类。
我会像这样得到子类的属性:
var propertyInfos = typeof(CrmObject).Assembly.GetType("Aufgabe").GetProperties().Where(p => Attribute.IsDefined(p, typeof(ImportParameter)));
但我也想获得 CrmObject 的属性。可能在同一个语句中。
[更新] 这应该是它。我稍后会测试它。谢谢你们。 :)
var propertyInfos = typeof(CrmObject).Assembly.GetType("DAKCrmImport.Core." +crmType).GetProperties().Where(p => Attribute.IsDefined(p, typeof(ImportParameter)));
奇怪的是,您不需要 bindingflag 参数来展平层次结构。显然这是默认值?嗯..随便。它有效:)
【问题讨论】:
-
你需要
BindingFlags.FlattenHierarchy。 -
您的代码应该可以正常工作。尝试删除 where 子句。
-
哦,谢谢。所以声明看起来像这样:
var propertyInfos = typeof(DAKCrmImport.Core.Entities.CrmData.CrmObject).Assembly.GetType(crmType).GetProperties(BindingFlags.FlattenHierarchy).Where(p => Attribute.IsDefined(p, typeof(ImportParameter))); -
GetType 期望类型的全名。
标签: c# reflection