Re: Dynamic class creation at runtime

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Jon,
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Just for example:

I have in my assembly the base class and 2 inherited classes: BaseClass,
Class1 and Class2
At runtime I want to create Class3 (that inherits directly from the
BaseClass), just by typing the new code for the mustoverride function of the
BaseClass, and saving it. After doing that I need to be able to use (at
runtime) class3 in the same way I use Class1 and Class2.

Can you help me with this?

Thank you very much


"Jon Skeet [C# MVP]" wrote:

> Piggy <Piggy@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> > I?m developing a project in VB.NET 2003 to analyze the financial markets
> > using several algorithms. To do that I created a ?mustinherit? class to
> > manage the behavior of the standard algorithm, and then a number of inherited
> > classes for each single algorithm.
> > Since the algorithms are potentially infinite, I will never be able to code
> > them all in the project at design time. So, I would like now to create an
> > editor to allow the users (using my software GUI) to dynamically create a new
> > class (inherited from the generic class) for each algorithm they want to use.
> > I do not have any idea on how I can do it. I read something about the
> > dynamic definition of Assemblies, but I do not want to create a new Assembly,
> > I would only add (and save) a new inherited class to my existing assembly.
> > Any help (samples, ideas, links) will be greatly appreciated.
>
> I'm not entirely sure which bit is causing you problems, but
> Activator.CreateInstance is the way to go for creating new instances of
> a type which is only known at runtime. You can find all the types in an
> assembly which inherit from a given base class by using
> Assembly.GetTypes and then checking IsAssignableFrom for each type,
> passing in the base class. (This is better than using the BaseType
> property, as that will only find classes that inherit directly from the
> base class.)
>
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>
.



Relevant Pages

  • Re: Abstract Class Theory
    ... >> there would be no need for abstract methods, and no abstract classes. ... >> the derived class will be overriding, or even overloading, the base class ... > class inherit from both. ... Actually you are confusing abstract classes and interfaces. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract Class Theory
    ... > the derived class will be overriding, or even overloading, the base class ... If you read into the Patterns literature, you can see that the requirement ... In addition, if you define two abstract classes, you can have a concrete ... class inherit from both. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Visual Studio 2003, CollectionBase.RemoveAt Method
    ... You don't think of it as such because Microsoft also hid the ... The VB Collection class does not inherit System.Collections.CollectionBase. ... you must explicitly define a "RemoveAt" method in order to ... you can't hide anything in a derived class from a base class. ...
    (microsoft.public.vsnet.general)
  • Re: What the diff between a virtual method and an inheirited method
    ... To inherit a method from a base class defined as virtual is ... > to allow the derived classes to define their own unique ... functional programming, and believe me it will make you a better ...
    (comp.lang.cpp)
  • Re: Dynamic class creation at runtime
    ... > classes for each single algorithm. ... > dynamic definition of Assemblies, but I do not want to create a new Assembly, ... assembly which inherit from a given base class by using ... as that will only find classes that inherit directly from the ...
    (microsoft.public.dotnet.framework.clr)