I have read many articles on the creation of dynamic types and classes at run time. For example, the TypeBuilder class in C# lets one create dynamic types. Python has this type function by which one can create dynamic classes. I have certainly good information on how I should create a class at run time, but I don't find enough information on why I should do this.

What is a scenario where one would be forced to create types at runtime?

  • I changed the tag since the term "dynamic programming" is most often used for a completely different thing (en.wikipedia.org/wiki/…). I added "dynamic typing", because this is something a language must support to let one actually use dynamically created classes. – Doc Brown Jan 20 '12 at 12:35

Mock frameworks line NMock (for generating mock classes in unit test scenarios) do this.

Another application I can think of may be a generic database tool generating classes at run time for each table in the DB (of course, there may be better solutions for this purpose).

In C#, every time when you have the requirement to construct a function at runtime (because, for example, the user is able to enter a function defined by himself in your application), you will need a dynamic class for that purpose since in C# functions cannot live "on their own".

And another application I know of are the dynamically created COM wrappers in C#4.0 for every COM component. Up to C# 3.5, you needed first to generate some type library wrapper which had to be compiled first before you could use it. With C#4.0 and the dynamic keyword, this is not necessary any more, the wrapper can be constructed at run time by the framework and you can write code which calls methods of those classes.

  • doesn't most ORMs work this way? – kagali-san Jan 20 '12 at 12:19
  • @kagalo-san: the ORMs I have seen so far worked with generated classes beforehand (allowing the user to add own code in each class), or they generate tables from classes, not vice versa. – Doc Brown Jan 20 '12 at 12:29
  • in C# functions cannot live "on their own". delegates, and Func<>s come to mind. – Steven Evers Jan 20 '12 at 14:03
  • @SnOrfus: "delegates" do typically refer to member functions of some class. I am not quite sure about anonymus functions, doesn't the CLR generate some hidden class in the background for those kind of functions? I think to emit some function in C# via type build, one has to emit at least one class also. Am I wrong? – Doc Brown Jan 20 '12 at 16:47
  • @DocBrown: delegate/Action<T>/Func<T> aren't anonymous (though they can be assigned an anonymous method). How they're implemented, I don't know. I recall that Action and Func wrap delegates for some sugar. I'm not sure though; I'm curious about that now so I'm going to look into it. – Steven Evers Jan 20 '12 at 17:22

TypeBuilder and other entities of System.Reflection.Emit namespace are used to create Intermediary Language (IL) code. The first usage which comes to mind is for the compilers, but I've seen other scenarios where simple Reflection wasn't enough, and you had to use Emit in your code (sadly, I don't remember where).

Note that it's not the same thing as dynamic word in .NET Framework 4, which is useful when you interact with dynamic languages like JavaScript.

  • But the dynamic keyword allows to write method calls in code for objects which classes still don't exist at compile time. – Doc Brown Jan 20 '12 at 12:32

In Python you would create a class and/or type (metaclass) whenever you have a situation where you cannot generate said class or type when writing the code.

The two examples that spring to mind are database classes (one class per table, and you don't know the table layout ahead of time), and allowing user code in your application.

It's basically one more (advanced) tool to work with, but if you have the option of writing the class ahead of time, it's best to do it that way.


I had a requirement when different customers of the same product were expectedly sending different fields for reporting purposes, which later inserted into certain DB columns following some mapping scheme. So, to maintain the cache I needed to generate classes at the runtime. Per say, today my cache class contains 5 fields but in future, it needed to have 3 more fields, what will happen, would I change the code? compile it again to accommodate the changes? Of course not, so, I'll be creating classes at the run-time by reading these properties from any configurable medium such as XML spec file, etc.

  • If you needed to have 3 more fields, then it would easy to add them, isn't it? May be you could refine your answer to add more clarity – Nachiappan Kumarappan Nov 7 '19 at 10:39
  • The code is in production, we dont want code changes per every field change, would we? Sure, I extend my answer for clarity. – Iqra. Nov 7 '19 at 11:23

Possible scenario:

some kind of dynamic cache for storing long heavily interlinked dataset which is for some reason represented as individual objects per entry?

Not the answer you're looking for? Browse other questions tagged or ask your own question.