2014-09-11 1 views
4

Bonjour, Je développe le plugin CRM 2011 et j'ai un problème avec le type reflété. J'ai généré des classes Entités et je sais que la propriété existe dans le type mais quand j'essaie d'obtenir sa valeur, je reçois une exception à propos de la méthode qui n'est pas trouvée. La partie la plus stupide est que cela fonctionne parfaitement sur ma machine mais ne fonctionne pas sur les clients.C# Exception de réflexion Méthode non trouvée

Voici mon code (je dois prendre toutes les OptionSets de l'entité et exécuter l'action sur eux):

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); 
      } 
     } 

est ici les détails du journal:

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> 

Comme vous pouvez le voir même le tracé La ligne "Essayer d'obtenir la valeur de" ne fonctionne pas. L'exception n'est pas attrapée ... Je ne sais pas quoi faire. Des pensées?

Répondre

6

Ok, je l'ai compris. Le serveur a Microsft .NET 4.0 installé et j'ai .NET 4.5.

Dans le .NET 4.5, il y a une nouvelle surcharge pour la méthode PropertyInfo.GetValue - il est PropertyInfo.GetValue (objet obj) depuis la version 4.0 il n'y a que PropertyInfo.GetValue (obj d'objet, objet [] indexeur)

I juste à remplacer:

OptionSetValue skillLevel = (OptionSetValue) optionSetsProperty.GetValue (fse);

avec

OptionSetValue skillLevel = (OptionSetValue) optionSetsProperty.GetValue (fse, null);

travaillé comme un charme!