Click to See Complete Forum and Search --> : Going crazy with reflection


Marraco
February 17th, 2009, 06:34 PM
Somebody knows, how to use reflection, to instance a class, inherited many times, from a base virtual class, whose constructor is protected and have a parameter?:ehh:

I tried ConstructorInfo and Invoke, tried Activator.CreateInstance.
Also tried all possible combination of binding flags and permission, and got no clue.:confused:


abstract class a
{
protected a(ref string param)
{
}
}

abstract class b : a
{
}

class c : b
{
}

class d : c
{
internal d(ref string param) : base(param)
{
}
}I want to instantiate b, c and his derivative classes

cjard
February 17th, 2009, 08:19 PM
b is abstract. How will you make an instance of it at all?

Marraco
February 17th, 2009, 08:26 PM
b is abstract. How will you make an instance of it at all?
I want to make instances of the non abstract derivative classes.

Mutant_Fruit
February 18th, 2009, 04:06 AM
You could create instances of 'd' but you'll more than likely have to use some special binding flags to get the internal constructor. Type.GetConstructors (typeof (d), BindingFlags.Private) (or something like that). By default only public properties/methods are selected (if i remember correctly).

The 'ref string' parameter will also be a pain in the butt to handle. Do you really need it to be ref?

Marraco
February 18th, 2009, 08:28 AM
You could create instances of 'd' but you'll more than likely have to use some special binding flags to get the internal constructor. Type.GetConstructors (typeof (d), BindingFlags.Private) (or something like that). By default only public properties/methods are selected (if i remember correctly).

The 'ref string' parameter will also be a pain in the butt to handle. Do you really need it to be ref?I cannot control if derivative class are by ref or by val. I only code the virtual base class, and a factory, and it should work with any inheritor.

The by ref can be managed by adding a "&" to a sting containing the object Type, but I cannot manage the derivated class who have not a constructor with the same parameters. I cannot instantiate them.

I tried all binding flags, I put an unsigned integer in a loop and tried each possible flag. It does not works.