Take the 2-minute tour ×

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?

share|improve this question
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
add comment

5 Answers

up vote 5 down vote accepted

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.

share|improve this answer
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. –  Steve 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. –  Steve Evers Jan 20 '12 at 17:22
add comment

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.

share|improve this answer
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
add comment

Possible scenario:

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

share|improve this answer
add comment

You can also read this article. Especially section Reflection emit - it has few subsections, each other is more optimized. :)

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


