Click here to Skip to main content
13,296,377 members (54,805 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 23 Aug 2007

Generic Dynamic Methods Primer

, 23 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Sample how to use generics and dynamic methods together to get simple type safe access to private variables


Do you want to do something bad? Very, very bad? Something that every decent programming book warns you against? Good. I'll show you how:).

Put in another way, I'll show you how to read and modify (without reflection) private variables of another class instance.

And he can't do anything to prevent you from peeping...


Data Abstraction is a very useful concept, and I am in no way going to tell you to drop it. But ... (the way to hell starts with "but", I think) sometimes, you must, absolutely must access this very integer that is stored somewhere deep inside another guy's code. Why? Probably you are implementing custom serialization logic, or object to DB persistence, or pre-condition verification. Or you are just curious...

You can use standard Reflection practices (get/set value methods of FieldInfo).

I'll show you another, probably slightly faster way to accomplish this.

Using the Code

Let's look at a sample class:

//first sample class.
//Note - variables are private
class SampleA 
    private int m_value;
    private List<string> m_list;

    public SampleA(int a)
          Value = a;

    public int Value
       get { return m_value; }
       set { m_value = value; }

    public List<string> List
       get { return m_list; }
       set { m_list = value; }

Now, how can we access those variables? Take a look:

static void Main(string[] args)
    //create first test instance
    SampleA sampleA = new SampleA(17);

    //create get/set accessors that can work on int field "m_value" on any instance
    AccessorBuilder.GetFieldValueUnboundDelegate<int, SampleA> valueGetter = 
    AccessorBuilder.CreateGetter<int, SampleA>("m_value");
    AccessorBuilder.SetFieldValueUnboundDelegate<int, SampleA> valueSetter = 
    AccessorBuilder.CreateSetter<int, SampleA>("m_value");

    //fetch value
    int value = valueGetter(sampleA);


    //change value
    valueSetter(sampleA, 23);


    value = valueGetter(sampleA);


Note how we created two very similar and innocent looking entities - getter and setter for the property. They are unbound - i.e. not related with any particular instance of the class, and you have to pass the actual instance reference for each operation.

You had to know the field type and name to create them - and that's all.

Now, we will use the bound version of the same getter and setter:

SampleB sampleB = new SampleB(55);

//define instance bound get/set accessors for int field of SampleB
AccessorBuilder.GetFieldValueBoundDelegate<int> instanceValueGetterB = 
    AccessorBuilder.CreateGetter<int, SampleB>(sampleB, "m_anotherValue");
AccessorBuilder.SetFieldValueBoundDelegate<int> instanceValueSetterB = 
    AccessorBuilder.CreateSetter<int, SampleB>(sampleB, "m_anotherValue");

//store setters in type safe list
List<AccessorBuilder.SetFieldValueBoundDelegate<int>> uniformSet = 
    new List<AccessorBuilder.SetFieldValueBoundDelegate<int>>();


//store getters in type safe list
List<AccessorBuilder.GetFieldValueBoundDelegate<int>> uniformGet = 
    new List<AccessorBuilder.GetFieldValueBoundDelegate<int>>();


//initialize both variables to 43
        delegate(AccessorBuilder.SetFieldValueBoundDelegate<int> item) 

//get and print
    delegate(AccessorBuilder.GetFieldValueBoundDelegate<int> item)
       int v = item();
       Console.WriteLine("{0}={1}", item.Target.GetType().Name, v);

So what do you see here?
We create two int getters and setters, each working with a different type. We put them in the same list, and access private values uniformly, just like those classes (SampleA and SampleB) are implementing an imaginary IIntFieldOwner interface. It looks like polymorphic access (it is not exactly the same, because neither class can override/overload this access, but still...).

Note that we are now passing an instance (sampleB) to the CreateSetter/CreateGetter methods, so created delegates are bound to specific object instances.

The same functionality can be easily achieved through regular Reflection techniques (with the help of some generic syntax sugar, like here).
But Reflection.Emit makes things faster (and complicated).


Emit is old technology, existing from Framework 1.0, and targeted to .NET assembler wizards, knowing how to generate new code online, but not on the high level language like C# using CodeDom - on .NET assembler level. I don't know how many people actually used it, anyway it is pretty hard to build something complex using Emit.

In Framework 2.0, they added a more lightweight version of Emit - dynamic modules, assemblies, etc. It allows you to build and generate classes and methods faster, and use them directly from memory. Moreover, they added a pretty nice feature - the ability to bypass security checks. When Dynamic method is built (without the need to create a module, assembly, etc), you can just pass additional parameters to its constructor - "Type that dynamic method is associated with". This clear specification hides the fact that this dynamic method will have access to all private members of the "Type".

So, how do you create a setter, for example?

static private DynamicMethod createSetterImpl<T, O>(string fieldName)
  if (fieldName == null)
    throw new ArgumentNullException("fieldName");

  if (fieldName.Length == 0)
    throw new ArgumentException("Field name must be non-empty string");

  FieldInfo fInfo = typeof(O).GetField(fieldName,
    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

  if (fInfo == null)
    throw new MissingFieldException("Can't obtain field " + fieldName + 
        " from class " + typeof(O).Name);

  if (!typeof(T).IsAssignableFrom(fInfo.FieldType))
    throw new InvalidCastException("Field " + fieldName + " of type " + 
        fInfo.FieldType + " can not be casted to " + typeof(T).FullName);

   //build setter
   DynamicMethod setterDef = new 
    DynamicMethod(typeof(O).Name + "_" + fieldName + "_Setter",
        new Type[] { typeof(O), typeof(T) },

    ILGenerator setterIL = setterDef.GetILGenerator();
    setterIL.Emit(OpCodes.Ldarg_0); //we expect in top of the stack object instance
    setterIL.Emit(OpCodes.Ldarg_1); //here goes value
    //Sfld means store something that is in the stack head to the field
    setterIL.Emit(OpCodes.Stfld, fInfo); 
    setterIL.Emit(OpCodes.Ret); //note: we have to return, even if method is void

    return setterDef;

Ok, so we've got some low level beast - DynamicMethod . How do we create our beautiful object-oriented, type safe, generic delegate? This can be done very easily as follows:

static public SetFieldValueUnboundDelegate<T,O > CreateSetter<T, O>(string fieldName)
   SetFieldValueUnboundDelegate<T,O> setter = null;
   DynamicMethod setterDef = createSetterImpl<T, O>(fieldName);

   setter = (SetFieldValueUnboundDelegate<T,O>)setterDef.CreateDelegate

   return setter;

And we are back to the line:

AccessorBuilder.SetFieldValueUnboundDelegate<int, SampleA> valueSetter = 
    AccessorBuilder.CreateSetter<int, SampleA>("m_value");

SampleA sampleA = new SampleA(17);

valueSetter(sampleA, 23);

So, as you can see, almost all work is done by the .NET Framework. We just add some generic - related stuff to make things look prettier (or uglier:), it depends...).


AccessorBuilder can be optimized to cache field information, to prevent FieldInfo fetch for each request.

Sample Project

It is essentially the same code that you see here, but the full version, that includes all four variations: generation of bound and unbound getters and setters. The solution includes a separate file, AccessorBuilder.cs, where all interesting code resides. An example of usage is in program.cs.


  • 23rd August, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Arlievsky
Architect mPrest Systems
Israel Israel
I am working with various Microsoft technologies since 1994 (remember Windows 3.1 ?Smile | :) ). Language of choice - C++/C#. I am mainly interested in software engineering and "generic solutions", so frequently I find myself designing / implementing frameworks and application skeletons, although sometimes I do various stuff from DB to Kernel Mode device drivers. I also love to understand why things are in the way they are, not just how to use them.
Currently I am Chief Software Architect and CTO of the software outsourcing company mPrest Ltd , Israel.

You may also be interested in...


Comments and Discussions

GeneralRe: Source Link Pin
huwsimpson24-Aug-07 0:08
memberhuwsimpson24-Aug-07 0:08 
GeneralRe: Source Link Pin
Alexander Arlievsky24-Aug-07 1:55
memberAlexander Arlievsky24-Aug-07 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by Alexander Arlievsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid