Click here to Skip to main content
11,721,289 members (84,396 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Inheritance
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 at 4-Nov-12 11:36am
Sergey Alexandrovich Kryukov at 4-Nov-12 11:50am
Thank you, Espen.
Maciej Los at 4-Nov-12 13:15pm
What can i say? GREAT, +5!
Sergey Alexandrovich Kryukov at 4-Nov-12 13:46pm
Thank you, Maciej.
tasinsahin at 4-Nov-12 17:01pm
thank you Sergey! Now, i m studying your links :)
tasinsahin at 4-Nov-12 17:01pm
thank you Sergey! you are great!
Sergey Alexandrovich Kryukov at 4-Nov-12 19:25pm
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
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 279
2 Mika Wendelius 170
3 F-ES Sitecore 150
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560

Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2015
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