【问题标题】:C# generic with similar classes具有相似类的 C# 泛型
【发布时间】:2014-09-01 11:47:00
【问题描述】:

假设我有一个像这样的 C# 类:

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

现在假设我也有:

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

并且定义相同的 OtherClass2、OtherClass3 等,直到 Otherclass50。所有这些类都具有相同的属性。但是它们是不同的类,因为它们是从 WSDL 自动生成的。

我需要一个类似的方法

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

其中 T 可能是 Otherclass1 或 Otherclass2 等。我怎样才能做到这一点?这在 C 宏中很容易做到,但这是 C#(特别是带有 Compact Framework 3.5 的 vs2008)。

谢谢

【问题讨论】:

  • 这些自动生成的类也是partial吗?
  • 您需要像automapper 这样的东西吗?
  • 如果您可以升级到 VS 2013,您可以为此使用 dynamic 数据类型。

标签: c# generics macros


【解决方案1】:

我能想到有两种方法,但其中一种需要对类进行一些小的改动:

1) 创建接口

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

现在让你所有的类都实现这个接口。然后CopyTo 将接受IMyClass,你就完成了。

2) 在CopyTo&lt;T&gt;(T target, ...) 函数中使用反射来复制值。

【讨论】:

  • 我认为界面不能有字段。
  • @Toris,接口不能有字段,但可以有属性。这是因为属性实际上是 getter 和/或 setter 函数。
  • 如果你想使用属性,它看起来像这样。接口 IMyClass {String A { 获取;放; }} 类 OtherClass1 : IMyClass { 字符串 a;公共字符串 A { 得到 { 返回一个; } 设置 { a = 值; } }}
【解决方案2】:

这可能会有所帮助。

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;
}

【讨论】:

    【解决方案3】:

    可以通过DynamicMap() 功能建议AutoMapper

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

    这将使您无需编写自己的对象到对象映射器、定义映射等。

    延伸阅读:http://lostechies.com/jimmybogard/2009/04/15/automapper-feature-interfaces-and-dynamic-mapping/

    您可能会在使用其他对象-对象映射框架(如 EmitMapper)时获得类似的行为。

    【讨论】:

      【解决方案4】:

      这对你有帮助吗?

       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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-15
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        相关资源
        最近更新 更多