hi there
I am writing a program and I have some class that doesn't extend any class.
I want to know if dynamic inheritance at runtime is possible. I mean if one or more classes can derive from a class at runtime.
hi there
I am writing a program and I have some class that doesn't extend any class.
I want to know if dynamic inheritance at runtime is possible. I mean if one or more classes can derive from a class at runtime.
You could take a look at Reflection Emit. There are also frameworks such as Castle Dynamic Proxy and Cecil which simplify this task.
UPDATE:
Now if I understand you correctly you have some class and you want to make this class derive from another base class at runtime. Let's prove by a counter example that this is not possible:
Assume that there is a method allowing you to do this. Consider the following class:
public class Foo {}
You could use this method to say that Foo derives from System.IO.Stream which obviously is not the case rendering the initial hypothesis wrong.
What is possible with reflection emit is to generate a dynamic class at runtime that inherits from a base class or implements an interface.
A class must gain it's inheritance before runtime. You will have to use Reflection.Emit which will allow you write your class at runtime. As Darin suggests, Castle gives you the ability to use a dynamic proxy, adding the functionality you desire. I would recommend this over the burdens and toils of Reflection.Emit.
I don't think it's possible because during runtime every class must have a structure and changing that structure during runtime would result in problems with the objects already created. At least that's how I see it.
But why on Earth would you want such a thing? Could you please post some code samples or link them so we could see what's it all about and what are you trying to accomplish?
Is what you are asking how to detect at run time if a class is of a certain type? There are a couple of ways of doing that depending on what you are looking for.
Here's an example:
using System;
namespace ConsoleApplication1 {
class Program {
class Base { }
class Derived : Base { }
class OtherClass { };
static void Main(string[] args) {
Base b = new Base();
Derived d = new Derived();
OtherClass oc = new OtherClass();
TestObject(b);
TestObject(d);
TestObject(oc);
}
static void TestObject(object o) {
Type baseType = typeof(Base);
Type paramType = o.GetType();
Console.WriteLine("Type of o: {0}", paramType.Name);
Console.WriteLine("o is Base: {0}", o is Base);
Console.WriteLine("o is IsInstanceOfType Base: {0}", baseType.IsInstanceOfType(o));
Console.WriteLine("o is IsSubclassOf Base: {0}", paramType.IsSubclassOf(baseType));
Console.WriteLine("o is of type Base: {0}", baseType == paramType);
Console.WriteLine();
}
}
}
Output:
Type of o: Base
o is Base: True
o is IsInstanceOfType Base: True
o is IsSubclassOf Base: False
o is of type Base: True
Type of o: Derived
o is Base: True
o is IsInstanceOfType Base: True
o is IsSubclassOf Base: True
o is of type Base: False
Type of o: OtherClass
o is Base: False
o is IsInstanceOfType Base: False
o is IsSubclassOf Base: False
o is of type Base: False
It is impossible to take an existing class and give it a new base class. This is because although new types can be built dynamically, they are always new types, not modifications of already loaded types. It is not possible to "unload" an existing type in order to modify its definition and reload it. You can unload an entire AppDomain, but objects in separate AppDomains can't directly interact with each other and so there would be little significance in what their base classes were.
Languages which support prototype inheritance (JScript, for example) allow you to do something very like what you describe. That is, with prototype inheritance you can decide at runtime what the shape of the inherited methods are for "instances" associated with a particular "prototype".
C# does not support this feature, sorry. If you describe why you want it, perhaps we can find another way to do that in C#.