title: Attempt by method x to access method y failed
body: <p>I have write a code base on other code.
  It is a plugin for <code>Dynamic CRM 2011.</code>
  Anyways, I wanna replace this plugin with existing <code>Plugin</code>.
  It is almost a mirror of another .dll <code>Plugin</code>.
  Anyways,  In customization of CRM, I have disabled All steps of previous <code>Plugin</code> and want to insert new Plugin via Plugin Registration.
  I build my class library via VS, VS  makes a .dll file in <code>/bin/Debug</code> folder of project.
  I select insert that .dll as Register new assembly.
  I chose <code>Sandbox</code> as Isolation mode.
  And then database as location ( Not <code>Disk</code> or <code>GAC</code> ).
  It successfully updated my selected Plugin.
  After that, I insert my steps( Just like the previous Plugin I wanna replace with ).
  But in one the create methods, I get this Error :</p>
  
  <pre><code>    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault,
           Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: 
          Unexpected exception from plug-in (Execute): OrderReceiptStepPlugin: System.MethodAccessException:
           Attempt by method 'Microsoft.Xrm.Sdk.Entity.ToEntity&lt;System.__Canon&gt;()' to access method 'StepType..ctor()' failed.Detail: 
          &lt;OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" 
          xmlns="schemas.microsoft.com/.../Contracts"&gt;
            &lt;ErrorCode&gt;-2147220956&lt;/ErrorCode&gt;
            &lt;ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" /&gt;
            &lt;Message&gt;Unexpected exception from plug-in (Execute): OrderReceiptStepPlugin: System.MethodAccessException: 
          Attempt by method 'Microsoft.Xrm.Sdk.Entity.ToEntity&amp;lt;System.__Canon&amp;gt;()' to access method 'StepType..ctor()' failed.&lt;/Message&gt;
            &lt;Timestamp&gt;xxxxxxxxxx.4628797Z&lt;/Timestamp&gt;
            &lt;InnerFault i:nil="true" /&gt;
            &lt;TraceText&gt;
  
  [OrderReceiptStep: OrderReceiptStepPlugin]
  [xxxxxx-xxxx-xxxx11-xxx-xxxxxxxxxxxx: OrderReceiptStepPlugin: Create of bmsd_orderreceiptstep]
  
  Entered .Execute(), Correlation Id: 47c5ddcb-9290-4945-9ac0-fd88925480b1, Initiating User: 5b6b27be-ca0f-e811-bebe-000c2964dd77
   is firing for Entity: bmsd_orderreceiptstep, Message: Create, Correlation Id: 47c5ddcb-9290-4945-9ac0-fd88925480b1, Initiating User: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
  Exiting .Execute(), Correlation Id: xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, Initiating User: xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx
  
  
  &lt;/TraceText&gt;
  &lt;/OrganizationServiceFault&gt;
  </code></pre>
  
  <p>I googled about MethodAccessException Find out that this method occurs because of .Net version 4 and later,
  And some code you can add in your Assebmly.cs to avoid this exception, according to this thread.
  But in my case no success. 
  I added the code below to my assembly.cs but no success :</p>
  
  <pre><code>    [assembly: SecurityTransparent()]
  [assembly: AllowPartiallyTrustedCallersAttribute]
  </code></pre>
  
  <p>Here is the code that cause the error, It is in pre-Create of custom entity :</p>
  
  <pre><code>  private void OrderReceiptStepPreCreateEventHandler(LocalPluginContext context)
  {
      SalesOrderReceiptStepRepository salesOrderReceiptStepRepository = new SalesOrderReceiptStepRepository(context);
      SalesOrderReceiptStep salesOrderReceiptStep = salesOrderReceiptStepRepository.CreateInstance();
      salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value + salesOrderReceiptStep.FreightAmount.Value);
      salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value - salesOrderReceiptStep.DiscountAmount.Value);
      switch (salesOrderReceiptStep.ReceiptStepType)
      {
          case ReceiptStepType.TimeOriented:
          case ReceiptStepType.PrePayment:
          case ReceiptStepType.Others:
              salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalDetailAmount.Value + salesOrderReceiptStep.FreightAmount.Value - salesOrderReceiptStep.DiscountAmount.Value);
              salesOrderReceiptStep.TotalAmount = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value + salesOrderReceiptStep.TotalTax.Value);
              break;
      }
      OrderReceiptStepCalculationPlugin orderReceiptStepCalculationPlugin = new OrderReceiptStepCalculationPlugin();
      orderReceiptStepCalculationPlugin.CreateORderReceiptStep(context, salesOrderReceiptStep);
      if (salesOrderReceiptStep.Contains("bmsd_totallineitemamount") &amp;&amp; salesOrderReceiptStep.Contains("bmsd_orderid"))
      {
          EntityReference attributeValue = salesOrderReceiptStep.GetAttributeValue&lt;EntityReference&gt;("bmsd_orderid");
          if (attributeValue != null)
          {
              SalesOrderAdapter salesOrderAdapter = new SalesOrderAdapter();
              OrganizationServiceContext context2 = new OrganizationServiceContext(context.OrganizationService);
              Entity ordersById = salesOrderAdapter.GetOrdersById(context2, attributeValue.Id);
              if (ordersById != null)
              {
                  CalculationHelper calculationHelper = new CalculationHelper();
                  Entity entity = calculationHelper.CalculateOrdersAndReturnEntity(context, ordersById, ordersById);
                  entity.Attributes["statuscode"] = null;
                  entity.Attributes["statecode"] = null;
                  context.OrganizationService.Update(entity);
              }
          }
      }
      if (salesOrderReceiptStep.Contains("bmsd_invoiceid") || salesOrderReceiptStep.Contains("statuscode") || salesOrderReceiptStep.Contains("statecode") || salesOrderReceiptStep.Contains("bmsd_totallineitemamount"))
      {
          EntityReference attributeValue2 = salesOrderReceiptStep.GetAttributeValue&lt;EntityReference&gt;("bmsd_invoiceid");
          if (attributeValue2 != null)
          {
              OrganizationServiceContext context2 = new OrganizationServiceContext(context.OrganizationService);
              OrderReceiptItemAdapter orderReceiptItemAdapter = new OrderReceiptItemAdapter();
              Entity invoiceById = orderReceiptItemAdapter.GetInvoiceById(context2, attributeValue2.Id);
              if (invoiceById != null)
              {
                  CalculationHelper calculationHelper = new CalculationHelper();
                  Entity entity2 = calculationHelper.CalculateInvoiceAndReturnEntity(context, invoiceById, invoiceById);
                  entity2.Attributes["statuscode"] = null;
                  entity2.Attributes["statecode"] = null;
                  context.OrganizationService.Update(entity2);
              }
          }
      }
  }
  </code></pre>
  
  <p>I know this is not a syntax or logical error in development ( Cause the mirror is working correctly).
  What other reasons can make this error?
  Also here is the Class that Exception throw from that, It also contains the Execute medthod : </p>
  
  <pre><code>    public void Execute(IServiceProvider serviceProvider)
  {
      if (serviceProvider == null)
      {
          throw new ArgumentNullException("serviceProvider");
      }
      LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);
      localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", new object[1]
      {
          ChildClassName
      }));
      try
      {
          Action&lt;LocalPluginContext&gt; action = (from a in RegisteredEvents
                                               where a.Item1 == localcontext.PluginExecutionContext.Stage &amp;&amp; a.Item2 == localcontext.PluginExecutionContext.MessageName &amp;&amp;
                                               (string.IsNullOrWhiteSpace(a.Item3) || a.Item3 == localcontext.PluginExecutionContext.PrimaryEntityName)
                                               select a.Item4).FirstOrDefault();
          if (action != null)
          {
              localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "{0} is firing for Entity: {1}, Message: {2}", new object[3]
              {
                  ChildClassName,
                  localcontext.PluginExecutionContext.PrimaryEntityName,
                  localcontext.PluginExecutionContext.MessageName
              }));
              action(localcontext);
          }
      }
      catch (FaultException&lt;OrganizationServiceFault&gt; ex)
      {
          localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", new object[1]
          {
              ex.ToString()
          }));
          throw;
      }
      finally
      {
          localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", new object[1]
          {
              ChildClassName
          }));
      }
  }
  </code></pre>