Is it possible to add a method to an EXISTING class at runtime? why or why not?

15,566

Solution 1

In regular C# / .NET, the answer is a simple "no". The most you can do is write a DynamicMethod which can behave like a method of that type (access to private fields etc), but it won't ever be on the API - you just end up with a delegate.

If you use dynamic, you can do pretty much anything you want. You can emulate that with ExpandoObject by attaching delegates in place of methods, but on a custom dynamic type you can do most anything - but this only impacts callers who use the dynamic API. For a basic ExpandoObject example:

dynamic obj = new ExpandoObject();
Func<int, int> func = x => 2*x;
obj.Foo = func;
int i = obj.Foo(123); // now you see it
obj.Foo = null; // now you don't

For properties and events (not methods), you can use the System.ComponentModel approach to change what appears at runtime, but that only impacts callers who get access via System.ComponentModel, which means mainly: UI data-binding. This is how DataTable represents columns as pseudo-properties (forgetting about "typed datasets" for the moment - it works without those).

For completeness, I should also mention extension methods. Those are more a compiler trick, not a runtime trick - but kinda allow you to add methods to an existing type - for small values of "add".

One final trick, commonly used by ORMs etc - is to dynamically subclass the type, and provide additional functionality in the subclass. For example, overriding properties to intercept them (lazy-loading, etc).

Solution 2

is it possible to delete methods / classes at runtime?

Suppose that it was possible. Calls to those methods would fail and produce undefined (but usually catastrophic) behaviour.

So I'm sure it is not possible.

Share:
15,566
user420667
Author by

user420667

Develop mainly in C# for ASP.NET, and JavaScript with jQuery. Currently looking into Silverlight. Have used Matlab, C, C++, Java, Python, Prolog. Still wanting to learn COM and calling various external dlls. Always hoping to make the workflow more efficient. (Wordpress) blog to come.

Updated on June 15, 2022

Comments

  • user420667
    user420667 about 2 years

    I would imagine this might use Reflection.Emit,

    but a similar question on SO only answers how to create a class/method dynamically, not how to update an existing class.

    In a similar vein, is it possible to delete methods / classes at runtime? If so, I suppose one could just delete the class, and add it back with its old methods plus the new one.

    Thanks in advance.

    P.S. I don't have an intended use for this, it is merely a matter of curiosity.

  • Andrew Barber
    Andrew Barber about 12 years
    Undefined usually means catastrophic! ;)
  • user420667
    user420667 about 12 years
    catch(UndefinedMethod/UndefinedClassException) ? :-P
  • user420667
    user420667 about 12 years
    haha, "for small values of add". I guess that means for static methods then :-P.
  • Marc Gravell
    Marc Gravell about 12 years
    @user420667 no, actually I mean "they're not really methods on the type".
  • svick
    svick about 12 years
    @user420667 There is MissingMethodException and TypeLoadException. They can happen when you build your code against one version of an assembly, but then run it against another version, which is missing the method or type.
  • user420667
    user420667 about 12 years
    great response. Any ideas about the why though? Would it break some sort of development principal / be hard to implement?
  • Christophe De Troyer
    Christophe De Troyer over 10 years
    Just because it could cause runtime errors doesn't mean that's a reason to no being able to do it.
  • Henk Holterman
    Henk Holterman over 10 years
    @ChristopheDeTroyer - in .NET that is a good reason to prevent it.
  • Aridane Álamo
    Aridane Álamo almost 10 years
    Using dynamic you enter the realm of the DLR, so the MissingMethodException will be thrown in runtime, not compile time.