简体   繁体   English

如何在两种相同名称和内部结构之间进行转换,但是来自不同的程序集?

[英]How to cast between 2 types of the same name and internal sturcture but from different assemblies?

I got assembly 1 and assembly 2. which contain 2 types of the same name and identical internal structure. 我得到了程序集1和程序集2.它包含两个相同名称和相同内部结构的类型。

Assembly 3 reference 1 and 2, and create a object of one of them. 程序集3引用1和2,并创建其中一个对象。 Now I want to cast it to the other from assembly 2. 现在我想把它从汇编2中转移到另一个。

Below is the fake code: 以下是假代码:

assembly1.namespace1.typeXXX obj1 = new assembly1.type1();
assembly2.namespace2.typeXXX obj2 = obj1;   <=== error here

How to do it? 怎么做?

Thanks! 谢谢!

You could use AutoMapper . 您可以使用AutoMapper
If the properties are named the same, it is as simple as this: 如果属性的名称相同,则它就像这样简单:

Mapper.CreateMap<assembly1.type1, assembly2.type1>();
var obj2 = Mapper.Map<assembly1.type1, assembly2.type1>(obj1);

这两种类型不同,因为它们位于不同的程序集中,您必须将Property复制到Property。

You could define custom casting operators , but I think in this case that would only add confusion. 可以定义自定义转换运算符 ,但我认为在这种情况下只会增加混淆。 My question is, why have you defined two identical (but separate) classes? 我的问题是,为什么你定义了两个相同(但是分开)的类? Why not just use one of them? 为什么不直接使用其中一个呢?

Another alternative is to map between the types. 另一种方法是在类型之间进行映射。 If you don't have control over their source code, you could do this with an extension method: 如果您无法控制其源代码,可以使用扩展方法执行此操作:

public static class TypeOneExtensions
{ 
    public static TypeTwo AsTypeTwo(this TypeOne typeOne)
    {
        return new TypeTwo
        {
            PropertyA = typeOne.PropertyA,
            PropertyB = typeOne.PropertyB,
            ...
        };
    }
}

Note though that this creates a new instance of TypeTwo , so changes you make to it won't be reflected in the original TypeOne instance. 请注意,这会创建一个新的TypeTwo实例,因此您对其所做的更改将不会反映在原始TypeOne实例中。

I got one dirty solution: 我有一个脏的解决方案:

Use XML serialize the obj1, hack into the XML and replace "namespace1" with "namespace2". 使用XML序列化obj1,攻击XML并将“namespace1”替换为“namespace2”。 And then deserialize the XML into obj2. 然后将XML反序列化为obj2。

The same question happens to me. 同样的问题发生在我身上。

This code maybe can help you! 这段代码也许可以帮到你!

using type1=assembly1.namespace1.typeXXX;
using type2=assembly2.namespace2.typeXXX;

Or, You can read this 或者,你可以读这个

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

相关问题 如何从2个不同的程序集加载相同的插件,并在两者之间序列化和反序列化类型? C#.net - How to load same plugins from 2 different assemblies, and serializing and deserializing types between the two? C# .net 内部类具有相同的命名空间但在不同的程序集中? - Internal classes having the same namespaces but in different assemblies? 在2个.NET程序集中使用相同名称和命名空间的类型 - Use types of same name & namespace in 2 .NET assemblies 如何使用不同类型进行转换 - How to cast with different types 反序列化来自不同程序集的匹配类型 - Deserialize matching types from different assemblies 如何使用Dependency.OnValue区分名称相同但类型不同的2个参数 - How to use Dependency.OnValue to differentiate between 2 parameters with the same name but different types 从同一装配体在不同位置加载的对象之间的转换异常 - Cast Exception between objects loaded from the same assembly in different locations 可以在一个项目中引用两个具有相同名称的不同程序集吗? 不同的二进制文件 - Possible to reference two different assemblies with the same name in a project? different binaries 来自不同程序集的相同本地数据库不起作用 - Same local database from different assemblies not working 转换不同类但相同类型的对象 - Cast objects of different classes but same types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM