Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Comparing the compiled and the emitted output, there's only one difference:</p> <p>Compiled:</p> <pre><code>callvirt instance int32 ReflectionServices.Node::get_NodeId() </code></pre> <p>Emitted:</p> <pre><code>call instance int32 ReflectionServices.Node::get_NodeId() </code></pre> <p>The type you're calling <code>int32 get_NodeId()</code> on is <code>ReflectionServices.Node</code>, but the type of the object you're passing into the method you're trying to replicate is <code>Address</code>. This leads me to believe that the property accessor defined on <code>ReflectionServices.Node</code> must be called virtually, perhaps because it inherits from another class (or implements an interface) that declares that property before <code>ReflectionServices.Node</code> implements it.</p> <p>When you're emitting that line of code, simply call it virtually:</p> <pre><code>@this.Emit(OpCodes.Callvirt, getMethod); </code></pre> <hr> <p><strong>EDIT: In light of the further provided code, here's the <em>real</em> solution.</strong></p> <p>So, you have a problem with the fundamentals of how you're implementing the interface:</p> <pre><code>var method = type.DefineMethod(methodName, MethodAttributes.Static | Method... // ^^^^^^ </code></pre> <p>Interface methods are not static; they're instance members. Thus, you first need to remove <code>MethodAttributes.Static</code> from the attributes flag upon creating the <code>MethodBuilder</code>.</p> <p>Second, when you go to return this function, you're going to have to include a <em>target object</em>, which is the instance upon which the method is called. To do that, you can use <a href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" rel="nofollow"><code>Activator.CreateInstance</code></a> to call the default-generated constructor and give you an instantiated object to use as the target. Replace the final line of your <code>Generate</code> method with these lines to achieve that.</p> <pre><code>var activatedObject = Activator.CreateInstance(type); return (Action&lt;TObject, TInstance&gt;)Delegate.CreateDelegate( typeof(Action&lt;TObject, TInstance&gt;), activatedObject, createdMethod); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload