[英]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...有什么办法可以解决这个问题...
NP1.AAA
and NP2.AAA
implement a common interface?NP1.AAA
和NP2.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.