Reflection.Emit private field/property access
<p>I'm using Reflection.Emit to generate getters for fields on the fly. So far, my code works in all cases I've tested except where the field isn't public. When the field isn't public, calling the delegate throws "System.NullReferenceException : Object reference not set to an instance of an object."</p>

<p>I'm using this code to generate: (field is a FieldInfo)</p>

<pre><code>DynamicMethod dm = new DynamicMethod(String.Concat ("_Set", field.Name, "_"), typeof(void),
                                             new Type[] { typeof(object), typeof(object) },
                                             field.DeclaringType, true);
ILGenerator generator = dm.GetILGenerator ();

generator.Emit (OpCodes.Ldarg_0);
generator.Emit (OpCodes.Ldarg_1);
if (field.FieldType.IsValueType)
    generator.Emit (OpCodes.Unbox_Any, field.FieldType);
generator.Emit (OpCodes.Stfld, field);
generator.Emit (OpCodes.Ret);

return (Action&lt;object, object&gt;)dm.CreateDelegate (typeof(Action&lt;object, object&gt;));
</code></pre>
