开发者

Dynamic Inheritance

I am writing a p开发者_JAVA技巧rogram 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.


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#.


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.


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.

  1. The is operator. It determines if a class "is-a" instance of the type (or a subclass). Also see the as operator.
  2. Type.IsInstanceOfType(). It determines if a type is of the given type (or a subclass)
  3. Type.IsSubclassOf(). It determine if a type is a subclass of the type (but not the type itself)
  4. Directly compare type objects with == to tell if the types are exactly the same.

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜