C#: Casting types dynamically

MK_Dev picture MK_Dev · Feb 19, 2009 · Viewed 36.4k times · Source

I currently have this type of code:

private void FillObject(Object MainObject, Foo Arg1, Bar Arg2)
{
    if (MainObject is SomeClassType1)
    {
        SomeClassType1 HelpObject = (SomeClassType1)MainObject;
        HelpObject.Property1 = Arg1;
        HelpObject.Property2 = Arg2;
    }
    else if (MainObject is SomeClassType2)
    {
        SomeClassType2 HelpObject = (SomeClassType2)MainObject;
        HelpObject.Property1 = Arg1;
        HelpObject.Property2 = Arg2;
    }
}

Assuming that SomeClassType1 and SomeClassType2 have the same set of properties that I want to assign (although they may differ in other ones), is it possible to dynamically cast MainObject to the appropriate type and then assign the value, without duplicating the code? This is what I would like to see in the end:

private void FillObject(Object MainObject, Foo Arg1, Bar Arg2)
{
    Type DynamicType = null;

    if (MainObject is SomeClassType1)
    {
        DynamicType = typeof(SomeClassType1);
    }
    else if (MainObject is SomeClassType2)
    {
        DynamicType = typeof(SomeClassType2);
    }

    DynamicType HelpObject = (DynamicType)MainObject;
    HelpObject.Property1 = Arg1;
    HelpObject.Property2 = Arg2;
}

And obviously C# complains about not being able to find DynamicType:

The type or namespace name 'DynamicType' could not be found (are you missing a using directive or an assembly reference?)

Is something like this possible in C# 2.0? If it's more messy than my current code, than I see no point in doing this, but I'm very interested to find out. Thanks!

EDIT: Just to clarify, I perfectly understand that implementing an interface is the most appropriate and probably correct solution. That said, I'm more interested in seeing how to I could do it without implementing an interface. Thanks for great replies!

Answer

Joel Coehoorn picture Joel Coehoorn · Feb 19, 2009

It looks like both of the types you care about implement the same two properties. In that case, what you want to do is define an interface for those properties:

public interface IMyInterface
{
   public Foo Property1 {get; set;}
   public Bar Property2 {get;set;}
}

Then, make sure each of your classes tell the compiler that they implement that new interface. Finally, use a generic method with a type argument that is constrained to that interace:

private void FillObject<T>(T MainObject, Foo Arg1, Bar Arg2) 
    where T : IMyInterface
{
    MainObject.Property1 = Arg1;
    MainObject.Property2 = Arg2;
}

Note that even with the extra code to declare the interface, these snippets still end up shorter than either one of the snippets you posted in the question, and this code is much easier to extend if the number of types you care about increases.