2

Suppose I have a C# class like:

class MyClass
{
    String a, b, c, d;
    int e, f, g, h;
}

Now suppose I also have:

class OtherClass1
{
    String a, b, c, d;
    int e, f, g, h;
}

and identically defined OtherClass2, OtherClass3, and so on, up to Otherclass50. All of these classes have the same properties. However they are distinct classes because they are auto-generated from WSDL.

I need a method like

CopyTo<T> (T target, MyClass source) 
{
    target.a = source.a; target.b = source.b;   etc...
} 

where T may be Otherclass1 or Otherclass2, etc. How can I accomplish this? This would be easy to do in C macros, but this is C# (specifically vs2008 with Compact Framework 3.5).

Thanks

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
dablumen
  • 117
  • 1
  • 5

4 Answers4

2

There are two ways, I can think of, but one of them required some minor change to the classes:

1) Create an interface

interface IMyClass { 
    String a,b,c,d;
    int e, f, g, h;
}

Now make all of your classes implement this interface. Then the CopyTo will accept IMyClass and you are done.

2) Use reflection in CopyTo<T>(T target, ...) function to copy the values.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • I think interface cannot have fields. – Toris Jul 10 '14 at 23:57
  • @Toris, interfaces can't have fields, but they can have properties. This is because properties are effectively getter and/or setter function. – Petar Ivanov Jul 11 '14 at 00:44
  • If you want to use properties, it looks like this. interface IMyClass {String A { get; set; }} class OtherClass1 : IMyClass { String a; public String A { get { return a; } set { a = value; } }} – Toris Jul 11 '14 at 01:03
0

This may helps.

class MyClass
{
    public String a, b, c, d;
    public int e, f, g, h;

    // This function can be replaced with
    // public static void CopyTo(BaseClass target, MyClass source){...}
    public static void CopyTo<T>(T target, MyClass source) where T : BaseClass
    {
         target.a = source.a;
         target.b = source.b;
         target.c = source.c;
         target.d = source.d;
         target.e = source.e;
         target.f = source.f;
         target.g = source.g;
         target.h = source.h;
    }
}

class BaseClass
{
    public String a, b, c, d;
    public int e, f, g, h;

    public void CopyFrom(MyClass source)
    {
        a = source.a;
        b = source.b;
        c = source.c;
        d = source.d;
        e = source.e;
        f = source.f;
        g = source.g;
        h = source.h;
    }
}

class OtherClass1 : BaseClass
{
    //String a, b, c, d;
    //int e, f, g, h;
}
Toris
  • 2,348
  • 13
  • 16
0

Can suggest AutoMapper with it's DynamicMap() capability:

var otherClass1 = Mapper.DynamicMap<OtherClass1>(myClass);

This will keep you from writing your own object-to-object mappers, defining mappings, etc.

Further reading: http://lostechies.com/jimmybogard/2009/04/15/automapper-feature-interfaces-and-dynamic-mapping/

You may get similar behaviour with other object-object mapping framework like EmitMapper as well.

Dima
  • 6,721
  • 4
  • 24
  • 43
0

Will this help you?

 class genericClass<T,U>
 {
    public T a ;
    public U e;
 }

    static void Main(string[] args)
    {
        genericClass<string, int> gen1 = new genericClass<string, int>();
        genericClass<string, int> gen2 = new genericClass<string, int>();
        genericClass<string, int> source = new genericClass<string, int>();
        source.a = "test1";
        source.e = 1;


        Copy<string,int>(gen1, source);
        Copy<string, int>(gen2, source);

        Console.WriteLine(gen1.a + " " + gen1.e);
        Console.WriteLine(gen2.a + " " + gen2.e);

        Console.ReadLine();
    }

    static void Copy<T, U>(genericClass<T, U> dest, genericClass<T, U> source)
    {
        dest.a = source.a;
        dest.e = source.e;
    }
gmail user
  • 2,753
  • 4
  • 33
  • 42