Click here to Skip to main content
13,189,324 members (67,991 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I googled but couldn't find a good answer, heres my question;
Is it possible to derive classses in runtime?
Posted 4-Nov-12 2:20am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There is one way: you can create an assembly during run time, using System.Reflection.Emit, and, in this assembly, create a type with required features (it could be a class derived from some existing type) and later use it using System.Reflection — in in the same runtime. Please see:[^],[^].

This is an interesting simple introductory article:[^].

This is one CodeProject article, more in depth:
Introduction to Creating Dynamic Types with Reflection.Emit[^],
Introduction to Creating Dynamic Types with Reflection.Emit: Part 2[^].

A good CodeProject article:
Dynamic Type Using Reflection.Emit[^].

I generally recommend to look at CodeProject articles on the topic; some are very interesting:[^].

I must add that creating code using System.Reflection.Emit is a very important feature to be used in some scenarios. For example, it is used in serialization, for getting good performance of the code generated from metadata. This CodeProject article shows another important application of it:
Using Reflection.Emit to Precompile Expressions to MSIL[^].

It requires good knowledge if MSIL. The work is tedious enough and the code is really hard to debug. The code in C# being debugged only shows you the process of code generation, but not generated code. So, you should only use it when you want to get some miraculous and very fundamental universal results. This is not a work for your everyday programming.

Espen Harlinn 4-Nov-12 11:36am
Thank you, Espen.
Maciej Los 4-Nov-12 13:15pm
What can i say? GREAT, +5!
Thank you, Maciej.
tasinsahin 4-Nov-12 17:01pm
thank you Sergey! Now, i m studying your links :)
tasinsahin 4-Nov-12 17:01pm
thank you Sergey! you are great!
You are welcome. Will you accept the answer formally (green button)? -- thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171016.2 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100