Home All Groups Group Topic Archive Search About

Reflection and Inheritance question.

Author
26 Mar 2007 8:50 AM
Frank O'Hara
I'm new to reflection and would like to know if what I want to do is
possible.  So far I haven't seem much value to reflection (for my
needs) but that could be the result of not using it properly, or
perhaps my architecture is inherently flawed.

I have the following:

class baseClass
{
    protected object _sharedProperty;

    public new baseClass(){}

    public object Shared
    {
        get {return _sharedProperty;}
        set {_ sharedProperty = value;}
    }

    public void SharedMethod()
    {
        // do something
    }
}


class classA : baseClass
{
    private object _property1;

    public new classA(){}

    public object Property1
    {
        get {return _ property1;}
        set {_ property1 = value;}
    }
}

class classB : baseClass
{
    private object _property2;

    public new classB(){}

    public object Property2
    {
        get {return _ property2;}
        set {_ property2= value;}
    }
}

public static void Test(string className,object propertyValue)
{
    Type runtimeClass = Type.GetType(className);

    if(((int)myEnum.value1 & runtimeClass.Shared) != 0)
    {
        System.Reflection.PropertyInfo myProperty =
runtimeClass.GetProperty("Property1");
        myProperty.SetValue(runtimeClass,propertyValue,null);    <- fails
here
    }
    if(((int)myEnum.value2 & runtimeClass.Shared) != 0)
    {
        System.Reflection.PropertyInfo myProperty=
runtimeClass.GetProperty("Property2");
        myProperty.SetValue(runtimeClass,propertyValue,null);    <- fails
here
    }
}

So, basically both ClassA and ClassB inherit from baseClass.  They
obviously share properties/methods but also have their own.

Based on certain conditions I want to set a property value of a class
(if it exists - I'm not testing for it in the code provided but plan
on it) without knowing which class until run-time.  The class that I
want to instantiate will be only known to the procedure as a string
value.

I'd like to be able to dynamically declare a variable as type
"className" at run-time based on what the string value is passed in
(for the sake of example say that the string being passed in IS the
fully qualified name of the class.  I then want to be able to access
and enter property values for the particular class.

But the problem I'm having is that when I try to set my property value
the first argument of the SetValue expects an instance of an object
with the type equal to that which I'm trying to set (which I think is
kinda dumb given that if I knew it I would probably just instantiate
it and set the properly directly).  Anyway, clearly I don't know what
this object is supposed to be so I don't know how to get past this
exception.

Does anyone have any thoughts?  What I'd love to do is something along
these lines (pseudo-code):

public static void Test(string className,object propertyValue)
{
    baseClass runtimeClass = new baseClass();

    if(((int)myEnum.value1 & runtimeClass.Shared) != 0)
    {
        ((className)runtimeClass).Property1 = propertyValue;
    }
    if(((int) myEnum.value2 & runtimeClass.Shared) != 0)
    {
        ((className)runtimeClass).Property2 = propertyValue;
    }

    runtimeClass.SharedMethod();
}

What I don't want is the following because this can and will get quite
enormous given the number of properties and flag values I have:

public static void Test(string className,object propertyValue)
{
    baseClass runtimeClass = new baseClass();

    if(((int)myEnum.value1 & runtimeClass.Shared) != 0)
    {
        if (className == "ClassA")
        {
            ((ClassA)runtimeClass).Property1 = propertyValue;
        }
        if (className == "ClassC")    <-- assume this class also inherits from
baseClass
        {
            ((ClassC)runtimeClass).Property1 = propertyValue;
        }
    }
    if(((int) myEnum.value2 & runtimeClass.Shared) != 0)
    {
        if (className == "ClassB")
        {
            ((ClassB)runtimeClass).Property2 = propertyValue;
        }
        if (className == "ClassC")    <-- assume this class also inherits from
baseClass
        {
            ((ClassC)runtimeClass).Property2 = propertyValue;
        }
    }

    runtimeClass.SharedMethod();
}

Thanks,
Frank

Author
26 Mar 2007 1:30 PM
Jon Skeet [C# MVP]
On Mar 26, 9:50 am, "Frank O'Hara" <mrpubni***@hotmail.com> wrote:
> I'm new to reflection and would like to know if what I want to do is
> possible.  So far I haven't seem much value to reflection (for my
> needs) but that could be the result of not using it properly, or
> perhaps my architecture is inherently flawed.

<snip>

> But the problem I'm having is that when I try to set my property value
> the first argument of the SetValue expects an instance of an object
> with the type equal to that which I'm trying to set (which I think is
> kinda dumb given that if I knew it I would probably just instantiate
> it and set the properly directly).  Anyway, clearly I don't know what
> this object is supposed to be so I don't know how to get past this
> exception.

It's the object you're trying to set the property on. In this case,
runtimeClass, but you can't create a new instance of *baseClass*, you
have to create an instance of the derived class, e.g. with
Activator.CreateInstance.

Jon

AddThis Social Bookmark Button