views:

5620

answers:

8

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!

+3  A: 

Essentially what you're doing here is writing a switch statement based on the type fo the object. There is no inherently good way to do this other than your first example (which is tedious at best).

I wrote a small framework for switching on types that makes the syntax a bit more concise. It allows you to write code like the following.

TypeSwitch.Do(
    sender,
    TypeSwitch.Case<Button>(
        () => textBox1.Text = "Hit a Button"),
    TypeSwitch.Case<CheckBox>(
        x => textBox1.Text = "Checkbox is " + x.Checked),
    TypeSwitch.Default(
        () => textBox1.Text = "Not sure what is hovered over"));

Blog Post: http://blogs.msdn.com/jaredpar/archive/2008/05/16/switching-on-types.aspx

JaredPar
+1  A: 

Instead of trying to cast, perhaps you could try setting the properties using reflection.

Craig
+3  A: 
private void FillObject(Object MainObject, Foo Arg1, Bar Arg2)
{
    Type t = MainObject.GetType();

    t.GetProperty("Property1").SetValue(MainObject, Arg1, null);
    t.GetProperty("Property2").SetValue(MainObject, Arg2, null);
}
Mehrdad Afshari
I think generics are more appropriate here, but at very least I would check to make sure the properties exist first.
Joel Coehoorn
I just wanted to demonstrate the way you do it with reflection. This is not *complete* or *recommended* approach for this problem.
Mehrdad Afshari
I think this has disaster written all over it...
Jason Punyon
Agreed. There is no reason to use dynamic typing where a simple polymorphism is the correct answer.
Mehrdad Afshari
+6  A: 

Will you be able to make changes to SomeClassType1, SomeClassType2 etc? If yes then I will suggest that you create an interface containing your common properties and then typecast to this interface within FillObject() to set the properties.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SO566510
{
    interface IMyProperties
    {
        int Property1 { get; set; }
        int Property2 { get; set; }
    }

    class SomeClassType1 : IMyProperties
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
    }

    class SomeClassType2 : IMyProperties
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var obj1 = new SomeClassType1();
            var obj2 = new SomeClassType2();

            FillObject(obj1, 10, 20);
            FillObject(obj2, 30, 40);

        }

        private static void FillObject(IMyProperties objWithMyProperties
                                   , int arg1, int arg2)
        {
            objWithMyProperties.Property1 = arg1;
            objWithMyProperties.Property2 = arg2;
        }
    }
}
SDX2000
While that's certainly an option, it will be an overkill for the simple thing I'm trying to achieve. My code is basically what I posted above, with just a few types and var names changed.
MK_Dev
I was about to suggest the same. If they have the same publicly available properties, an Interface seems to be appropriate.
Jay S
@MK_Dev : It isn't as much work you think it is, you need to give it a try. In fact it might end up reducing the size of your code.
SDX2000
@MK_Dev: How is this overkill? It has less code than either of your examples. The addition of the Interface is more than made up for by the simplification of the FillObject method. The rest is implicit in your code even if you don't show it.
Stephen Martin
Overkill - not in terms of code, but more in terms of what I need to get done. Since one version of the code is already written and works (I know, I know...) and follows a lot of other patterns we use internally, I see no reason to introduce interfaces at this moment.
MK_Dev
+10  A: 

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.

Joel Coehoorn
+1 Nice...I wouldn't have thought to use generics, I would've just cast to the interface.
Jason Punyon
How is the generic Parameter T useful here? Why not simply use FillObject(IMyInterface mainobject,...)?
SDX2000
I'm curious - why would you use generics in this case and not just private void FillObject(IMyInterface MainObject, Foo Arg1, Bar Arg2)?
Stephen Martin
T might save some casting, but yeah it's mostly useless. I think it was just my thought process saw his code and said, "Hey: generics" and exploring that led me to the interface, rather than vice versa.
Joel Coehoorn
+2  A: 

Some ideas:

  1. Have both types inherit from the same type that have the common properties
  2. Have both types implement the same interface that have the common properties
  3. Use reflection to set the properties rather than setting them directly(this is probably more ugly than it's worth)
  4. Use the new dynamic feature, I haven't tried this, but looks like it might address your issue
toby
A: 

I am dynamically casting objects using Reflection in an ASP.NET MVC app. Basically I enumerate the properties of a class, find the corresponding value in the data store and dynamically cast the value and assign it to the object instance. See my blog post Dynamic Casting with .NET

Michael Ceranski