简体   繁体   English

C# 如何在不修改它们的情况下使用具有相同属性的 2 个类

[英]C# How to use 2 classes with the same properties without modifying them

I have two identical classes in different namespaces:我在不同的命名空间中有两个相同的类:

namespace NP1 {
   public class AAA {
      public int A {get; set;}
      public int B {get; set;}
   }
}

namespace NP2 {
   public class AAA {
      public int A {get; set;}
      public int B {get; set;}
   }
}

They are in different files and they are autogenerated.它们位于不同的文件中,并且是自动生成的。 I can't modify them.我无法修改它们。

Then I have two other files:然后我还有另外两个文件:

using NP1;

public class N1Helper {
   (...)
   var sth = new AAA(A: some_value, B: some_other_value);
   (...)
}

and

using NP2;

public class N2Helper {
   (...)
   var sth = new AAA(A: some_value, B: some_other_value);
   (...)
}

The skipped parts of these helpers are identical.这些助手的跳过部分是相同的。

I'd like to simplify these two files and write the code only once.我想简化这两个文件并只编写一次代码。 If the classes in these namespaces would implement an interface, I could do it.如果这些命名空间中的类可以实现一个接口,我可以做到。

Is there a way I can solve this problem...有什么办法可以解决这个问题...

  • Using generics?使用 generics?
  • Telling somewhere a posteriori that NP1.AAA and NP2.AAA implement a common interface?后验告诉某个地方NP1.AAANP2.AAA实现了一个通用接口? Something like using partial classes and appending the interface information in a latter stage, but I can't modify the autogenerated files.类似于使用部分类并在后期附加接口信息,但我无法修改自动生成的文件。
  • ...? ...?

Using generics使用 generics

Sure, just take the type as a generic argument.当然,只需将类型作为通用参数。 You just won't be able to actually use the generic argument, because it doesn't implement any interfaces:您将无法实际使用泛型参数,因为它没有实现任何接口:

public class NHelper<TAAA> {
   (...)
   // this is not going to work
   // var sth = new TAAA(A: some_value, B: some_other_value);
   (...)
}

using partial classes [...] I can't modify the autogenerated files使用部分类 [...] 我无法修改自动生成的文件

Partial classes have to have the partial keyword, and your auto-generated classes don't.部分类必须有partial关键字,而您的自动生成的类没有。 So no.所以不行。

...? ...?

Same no.同样没有。 C# isn't a duck-typing language like C++, it's a strongly typed language at every layer. C# 不是像 C++ 那样的鸭式语言,它是每一层的强类型语言。 You get exactly what you code, and if you don't feel like coding, you won't get anything.你得到的正是你编写的代码,如果你不想编写代码,你将一无所获。

Fix your auto-generated files.修复自动生成的文件。

As Blindy mentioned you wont be able to make use of the generic type because it doesn't implement any interfaces.正如 Blindy 提到的,您将无法使用泛型类型,因为它不实现任何接口。 Although you could use the type to identify the context while taking advantage of dynamic to access/return the properties and methods in a common manner.尽管您可以使用类型来识别上下文,同时利用dynamic以通用方式访问/返回属性和方法。

public class NHelper<TAAA>
{
    public dynamic GetSth(int some_value, int some_other_value)
    {
        dynamic someclass = typeof(TAAA) == typeof(NP1.AAA) ? new NP1.AAA() : new NP2.AAA();

        someclass.A = some_value;
        someclass.B = some_other_value;

        return someclass;
    }
}

I'm not sure what 'sth' is I just followed your naming convention.我不确定“某事”是什么,我只是遵循了您的命名约定。

NHelper<NP1.AAA> helper = new NHelper<NP1.AAA>();
dynamic test = helper.GetSth(1, 2);
int A = test.A;
int B = test.B;
//int calc = test.Calculate();


NHelper<NP2.AAA> helper2 = new NHelper<NP2.AAA>();
dynamic test2 = helper2.GetSth(1, 2);
A = test2.A;
B = test2.B;
//calc = test2.Calculate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现相同属性的C#类 - C# classes that implement the same properties C#在内部传递具有相同属性的ref不同类型的对象,并在没有接口的情况下填充它们 - C# pass by ref different types of object that have the same properties inside and populate them without interface 如何在运行时创建C#类,并在同一运行时访问它们? - How to create C# Classes during runtime, and access them during the same runtime? 如何在C#中使用属性 - How to use properties in C# c# 如果两个属性相同,则将两个类合并到一个列表中 - c# combine two classes in one list if two properties are the same C#-如何使以下类成为基类,以便子类可以具有相同的属性和方法? - C# - How to make the following class a base class so that child classes can have the same properties, methods? 如何将xml字符串反序列化为一组C#类,而不必只为xml属性声明很多属性? - How to deserialize a xml string to a set of C# classes without having to declare a lot properties just for xml attributes? 如何在C#类中使用变量,同时在每次调用时重置变量? - How do you use variables across C# classes while resetting them every call? 如何使用GetType访问C#中派生类的方法/属性? - How can I use GetType to access methods/properties of derived classes in C#? 修改/添加到C#局部类 - Modifying / Adding to C# Partial Classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM