Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is the gibberish at the end, and using <code>Stfld</code> to call a property; I have no idea where that end stuff came from, but I <em>do not</em> believe that came from <code>ExampleMethod</code> - I think that might have been from a previous build of yours. In particular, you haven't defined a second local, so <code>Ldloc_1</code> makes no sense; but there is absolutely no need for any labels / branches here. Here is what I have, that works (note I didn't know what your <code>settableProperties</code> were, so I did it just using <code>FieldInfo</code> / <code>PropertyInfo</code>:</p> <pre><code> var dm = new DynamicMethod(string.Format("Deserialize{0}", Guid.NewGuid()), typeof(object), new[] { typeof(Document) }, true); var il = dm.GetILGenerator(); var ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); il.DeclareLocal(type); il.Emit(OpCodes.Newobj, ctor); il.Emit(OpCodes.Stloc_0); var getFieldValue = typeof(Document).GetMethod("Get", BindingFlags.Instance | BindingFlags.Public); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var field in fields) { il.Emit(OpCodes.Ldloc_0);// [target] il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldstr, field.Name); il.Emit(OpCodes.Callvirt, getFieldValue); il.Emit(OpCodes.Stfld, field); } var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in props) { var setter = prop.GetSetMethod(); if (setter == null) continue; il.Emit(OpCodes.Ldloc_0);// [target] il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldstr, prop.Name); il.Emit(OpCodes.Callvirt, getFieldValue); il.EmitCall(OpCodes.Callvirt, setter, null); } il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ret); return (Func&lt;Document, object&gt;)dm.CreateDelegate(typeof(Func&lt;Document, object&gt;)); </code></pre> <p>And for comparison purposes, here is what I get when looking at <code>ExampleMethod</code> in reflector (in a release build, etc):</p> <pre><code>.method public hidebysig static class PocoObject ExampleMethod(class Document document) cil managed { .maxstack 3 .locals init ( [0] class PocoObject poco) L_0000: newobj instance void PocoObject::.ctor() L_0005: stloc.0 L_0006: ldloc.0 L_0007: ldarg.0 L_0008: ldstr "ID" L_000d: callvirt instance string Document::Get(string) L_0012: stfld string PocoObject::ID L_0017: ldloc.0 L_0018: ldarg.0 L_0019: ldstr "DisplayText" L_001e: callvirt instance string Document::Get(string) L_0023: stfld string PocoObject::DisplayText L_0028: ldloc.0 L_0029: ldarg.0 L_002a: ldstr "PropId" L_002f: callvirt instance string Document::Get(string) L_0034: callvirt instance void PocoObject::set_PropId(string) L_0039: ldloc.0 L_003a: ret } </code></pre> <p>Things to note:</p> <ul> <li>no labels / branching (I have no idea where that came from, but that is not from what you posted)</li> <li>it defines a local</li> <li>no <code>nop</code></li> </ul>
    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