在不同名称空间中的相同结构之间转换值. C# [英] Converting values between same structure within different namespace. C#
问题描述
我在不同的命名空间中有两个相似的[所有属性都相同.]结构.
I have two similar[all attributes are same.] structures within different namespaces.
现在,当我尝试在这些结构的对象之间复制值时,我遇到了错误.
Now when i try to copy values between the objects of these structures i am getting errors.
如何使位于不同名称空间中的两个相似结构的对象之间复制值成为可能?
How can i make it possible to copy values between objects of two similar structures residing in different namespaces?
提前谢谢.
此致
约翰
推荐答案
您不能自动使用框架的内置转换.
You can't, automatically, just using the framework's built-in conversions.
短名称(即名称空间中的名称)在这里完全不相关-就CLR而言,A.B.C.SomeType
和A.B.C1.SomeType
与X.Y.Foo
和A.B.Bar
一样.
The short names (i.e. within the namespace) are entirely irrelevant here - as far as the CLR is concerned, A.B.C.SomeType
and A.B.C1.SomeType
are as different as X.Y.Foo
and A.B.Bar
.
您应该编写自己的转换例程,或者(最好)首先避免两种不同的类型(如果它们执行相同的操作).或者,您可以使用基于反射的方法来执行转换...但是仍然不能让 runtime 来完成转换.
You should either write your own conversion routines, or (preferrably) avoid having two different types in the first place, if they do the same thing. Alternatively you could use a reflection-based approach to perform the conversion... but that's still not getting the runtime to do it.
这篇关于在不同名称空间中的相同结构之间转换值. C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!