TrN*_*TrN 4 c# reflection dynamics-crm-2011
嗨,我正在开发CRM 2011插件,我有反映类型的问题.我已经生成了实体类,我知道属性存在于类型中,但是当我尝试获取其值时,我得到了关于未找到方法的异常.最愚蠢的部分是它在我的机器上完美运行但不适用于客户端.
这是我的代码(我需要从实体获取所有OptionSets并对它们执行操作):
public override void MyExecute()
{
var fse = TargetEntity.ToEntity<Equipment>();
Type equiptmentType = fse.GetType();
TracingService.Trace("FSE object type: {0}", equiptmentType.FullName);
IEnumerable<PropertyInfo> optionSetsProperties = equiptmentType.GetProperties()
.Where(x => x.PropertyType == typeof(OptionSetValue)).ToList(); //I'm getting this property from the object type so it must exist.
foreach (var optionSetsProperty in optionSetsProperties)
{
TracingService.Trace("Resolving ResourceGroup on property: {0}", optionSetsProperty.Name);
ResolveResourceGroupBySkill(optionSetsProperty, fse);
TracingService.Trace("Resoloving ResourceGroup finished");
}
}
private void ResolveResourceGroupBySkill(PropertyInfo optionSetsProperty, Equipment fse)
{
try
{
TracingService.Trace("Trying to get value of: {0}", optionSetsProperty.Name);
OptionSetValue skillLevel = (OptionSetValue)optionSetsProperty.GetValue(fse);
TracingService.Trace("Value equals: {0}", skillLevel);
}
catch (Exception ex)
{
TracingService.Trace("An error occured: {0}", ex.Message);
Exception inner = ex;
while (inner != null)
{
TracingService.Trace(inner.Message);
inner = inner.InnerException;
}
throw new InvalidOperationException(String.Format("Cannot get value of skill level from property: {0}", optionSetsProperty.Name), ex);
}
}
Run Code Online (Sandbox Code Playgroud)
这是日志详细信息:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Method not found: 'System.Object System.Reflection.PropertyInfo.GetValue(System.Object)'.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220891</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Method not found: 'System.Object System.Reflection.PropertyInfo.GetValue(System.Object)'.</Message>
<Timestamp>2014-09-11T12:58:09.2941554Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[OC.CSSFieldService: OC.CSSFieldService.ServiceActivity.MyPlugin]
[424ad2a7-ea29-e411-be7f-00155d0aa109: OC.CSSFieldService.ServiceActivity.MyPlugin: Create of equipment]
FSE object type: OC.Data.Equipment
Resolving ResourceGroup on property: oc_ExpeCommHyper
</TraceText>
</OrganizationServiceFault>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,即使是"试图获得价值"的追踪线也无效.异常没有被抓住......我不知道该怎么做.有什么想法吗?
好吧我明白了.服务器安装了Microsft .NET 4.0,我有.NET 4.5.
在.NET 4.5中,PropertyInfo.GetValue方法有一个新的重载 - 它是PropertyInfo.GetValue(对象obj),因为在4.0中只有PropertyInfo.GetValue(object obj,object [] indexer)
我只需要替换:
OptionSetValue skillLevel = (OptionSetValue)optionSetsProperty.GetValue(fse);
Run Code Online (Sandbox Code Playgroud)
同
OptionSetValue skillLevel = (OptionSetValue)optionSetsProperty.GetValue(fse, null);
Run Code Online (Sandbox Code Playgroud)
像魅力一样工作!
| 归档时间: |
|
| 查看次数: |
1570 次 |
| 最近记录: |