tags:

views:

69

answers:

4

Hello, I have the following:

Class 1 (Text, State, Level)
Class 2 (Text, State, Level, Ident)

Is there a way for me to cast an object of Class 2 into into Class 1, and not having to do the usual cast code (Text = c.Text, State = c.State etc.)? Possibly by identifying the property names of each class and copying the value over?

+1  A: 

It might be overkill for your specific problem, but Automapper can help with this problem.

Shane Fulmer
I immediately thought of AutoMapper when I read this question.
Alastair Pitts
+3  A: 

Why not derive Class 2 from Class 1, or have a common base class?

e.g.

class Class1 
{ 
   string Text;
   string State; 
   int    Level; 
} 

class Class2 : Class1 
{ 
   int Ident;
   // ...
}

A Class 2 instance can now be used everywhere a Class 1 instance is required.

Mitch Wheat
Could you elaborate a bit? How would this help/how would I achieve what I need by doing this? Thank you!
Alex
+1, Inheritance is a perfect solution (on the face of it)
Alastair Pitts
+1  A: 

Here is a very simple example without any error checking, it simply uses reflection to iterate over the properties of the source object and set the value of the destination object only if the types match.

class Program
{
    static void Main(string[] args)
    {
        var bar = new Bar();
        var foo = new Foo {A = 10, B = "Hello World"};

        foo.CopyTo(bar);

        Console.WriteLine("{0} - {1}", bar.A, bar.B);
    }
}

public static class Extensions
{
    public static void CopyTo(this object source, object destination)
    {
        var sourceType = source.GetType();
        var destinationType = destination.GetType();

        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

        var properties = sourceType.GetProperties(flags);
        foreach (var sourceProperty in properties)
        {
            var destinationProperty = destinationType.GetProperty(sourceProperty.Name, flags);
            if (destinationProperty.PropertyType.Equals(sourceProperty.PropertyType))
            {
                destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
            }
        }
    }
}
Rohan West
This is nice.... is there a performance penalty for using Reflection?
Alex
Defiantly, using reflection to get and set property values is considerably slower. There are plenty of interesting discussions on reflection. If you were really keen, you could create an expression and cache its result for later use.. or just use Automapper
Rohan West
.NET 2.0 onwards caches `MemberInfo` automatically. If you're using a lot of reflection the performance hit is not that bad. Obviously it's slower than not using reflection but its not this super-evil everyone thinks it is. http://msdn.microsoft.com/en-au/magazine/cc163759.aspx#S7
cottsak
@cottsak Good to know!
Rohan West
To me this seems like a classic case of the overuse of reflection.
Mitch Wheat
Now that .Net 4.0 is out, consider using `dynamic` objects for property setting.
Gabe
+1  A: 

Maybe the problem is more complicated than the question. If not, have you tried inheritance?

class Class1
{
  //Text, State, Level
}

class Class2 : Class1
{
  //Indent
} 

Since Class2 inherits from class 1, you can pass it around as Class1 no casting needed. This works for example:

Class1 obj = new Class2();
Josh Sterling