views:

319

answers:

6

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.

+2  A: 

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.

Darin Dimitrov
Is this concept of dynamic inheritance actually used or even recommended ?
HotTester
hi thanksdo you have any sample about it?if you have please help me
Mehdi
@user222820, sample of what? In your question you didn't post any details about how your classes look like, what class do you want to emit at runtime, ... So many things to specify before we can talk about samples.
Darin Dimitrov
@HotTester, yes reflection emit is used in some popular ORMs and Mocking frameworks.
Darin Dimitrov
class _child{ string child_name;}class _parent { string parent_name;}i wanna know how can i say that my child class inherit from parent class at runtime please help methanks
Mehdi
@Darin Thanks!!! There is always something new to learn in SO
HotTester
A: 

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.

dboarman
A: 

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?

AlexRednic
+3  A: 

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
shf301
A: 

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.

Daniel Earwicker
+3  A: 

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

Eric Lippert