Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating C# Classes at runtime

Tags:

c#

.net

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?

like image 684
John Hartsock Avatar asked Apr 17 '10 19:04

John Hartsock


People also ask

How is C created?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.

What is C creation?

Creatinine (/kriˈætɪnɪn/ or /kriˈætɪniːn/; from Greek: κρέας, romanized: kreas, lit. 'flesh') is a breakdown product of creatine phosphate from muscle and protein metabolism. It is released at a constant rate by the body (depending on muscle mass). Creatinine. Names.

What is C used to create?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.

Why C is created?

The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.


2 Answers

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.

like image 134
Marc Gravell Avatar answered Sep 19 '22 18:09

Marc Gravell


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.

like image 33
Henk Holterman Avatar answered Sep 20 '22 18:09

Henk Holterman