Vaibhav Gaikwad
Microsoft .NET developer
Bla Code! Bla Code!

Need a class at runtime - .NET 4.5 helps

Wednesday, August 19, 2015 2:58 PM
Ever came across a need to create class definitions at run-time based on some meta definitions coming from some external source. Well, we will not care about where those meta definition about the class comes from, but the problem at hand is the create a class definition at run-time. 

.NET evolved a lot and with 4.5 there are pretty interesting things to work out. Two of them are:
1. Keyword "dynamic"
2. DynamicObject class from System.Dynamic

Lets assume that our meta definitions on the class properties are stored in a Dictionary and we want to use it to create a new class at run-time and bind those properties to it, then this is how it can be done.

/* code for NewClass */
    public sealed class NewClass : DynamicObject
    {
        private readonly Dictionary<string, object> properties;

        public NewClass(Dictionary<string, object> properties)
        {
            this.properties = properties;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return this.properties.Keys;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (this.properties.ContainsKey(binder.Name))
            {
                result = this.properties[binder.Name];
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (this.properties.ContainsKey(binder.Name))
            {
                this.properties[binder.Name] = value;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
 
/* code end here */

Let see how to utilize this one in the simple program. Lets make a class Employee with properties Name, Code, Telephone


/* Program code starts here */
class Program
    {
        static void Main(string[] args)
        {
            dynamic emp = new NewClass(new Dictionary<string, object>() { {"Code" , 1} , {"Name", "John"} , {Telephone, "3636352"} });
   Console.WriteLine(emp.Code);
   Console.WriteLine(emp.Name);
   Console.WriteLine(emp.Telephone);
           Console.ReadLine();
        }
    }
/* code ends here */

There could be numerous applications to this concept. Some people many think to build the whole middle ware using this because now your entities can modify automatically when the table structure changes.  

Feel free to comment and question.



Feedback

# free imvu credits

Now i am getting the excellent game in 2017 .imvu credits generator .Imvu is online playing game .if you are interested to play this game than visit our website hack imvu on us. 9/28/2017 3:00 PM | imvu credits

# re: Need a class at runtime - .NET 4.5 helps

Currently we use .NET frame work for our back end coding and it just awesome.First I get training from hollywoodvoiceoveracademy and then I start working.Thanks for this method. 10/15/2017 10:02 PM | Jack William

# re: Need a class at runtime - .NET 4.5 helps

In football, an injury is a common thing to have every player.The waverlybarton.com share recent injury images with us so we understand the pain of players. Although we just watch the matches and enjoy for it. 4/21/2018 6:17 PM | Jason Roy

Post a comment