15

I have been curious about dynamically create class at runtime in C# and stumbled across this article. http://olondono.blogspot.com/2008/02/creating-code-at-runtime.html I am curious to hear some pros and cons regarding construction of a class at runtime.

Any opinions?

John Hartsock
  • 85,422
  • 23
  • 131
  • 146

6 Answers6

20

Meta-programming has all the advantages of build-time code-gneneration, but without the additional code step. This is very common in library code, such as ORMs, serializers, some types of AOP, DI/IoC containers, etc.

  • + avoids the need for extra build steps or writing mundane code
  • + such code can handle what actually is the case at runtime, rather than having to handle any uncommon edge-cases or lots of wrappers around wrappers (decorator pattern)
  • + allows codegen in scenarios where the metadata is only known at runtime
  • + runtime IL can have more access to private fields etc, thanks to how DynamicMethod can be associated with a type; fully generated (dll) code would require [InternalsVisibleTo] or similar, which may be impossible
  • - not all systems support runtime code-gen; it is disabled on some server setups, compact framework, iPhone, etc
  • - it is bug ugly to do. Regardless of how you do it, this is not normal code.
  • - it needs a really good understanding of how things actually work under the covers
  • + if forces you to get a really good understanding of how things actually work under the covers

I'm currently re-writing an existing library to use runtime IL generation; it is very rewarding and I'm happy with it; but it is unlike anything I've written before.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
6

This is not a matter of pros and cons.

Sometimes it is convenient to create a class based on information that is tedious to convert to code in another way or on information that is not available until runtime.

The example in the linked article is not something you (as an application programmer) would normally do. But it is useful in tools that, for instance, generate classes based on a database or XML schema.

H H
  • 263,252
  • 30
  • 330
  • 514
2

There's a time and a place for everything, including this (For example, generics). However, I would consider the alternatives to generating classes at runtime before I go ahead and do it. There's most likely a better and easier to maintain alternative to generating classes.

Joel
  • 16,474
  • 17
  • 72
  • 93
0

You probably have experience with runtime-generated classes already. Generics are built at runtime in the CLR, when they are first needed. So are implementations of XmlSerializer.

Building classes from the ground up at runtime is likely to be pretty ugly (unless you like looking at a lot of reflection and Code Dom code). It'd be a rare situation where this was the clearest and simplest solution.

harpo
  • 41,820
  • 13
  • 96
  • 131
0

Advantage: You can create at runtime whatever is needed.

Disadvantage: You have no compile-time checking, so if anything goes wrong, everything blows up.

Basically the same as advantages/disadvantages for using Reflection.

Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
0

One con that springs to mind is memory usage. If you dynamically build a class to handle some request each time you receve a request, then you run the risk of generating a new class for the same values each time. Once a class or assembly is loaded into an app domain, it can't be unloaded. So, try to cache generated assemblies and classes wherever possible.

Damian Powell
  • 8,655
  • 7
  • 48
  • 58