我试图写一个转换器类, 用于自定义类型, 将一种类型( 及其所有属性) 转换为具有匹配属性的另一种类型 。
The problem comes in when the property is also a custom type, rather that a simple type.
The custom types are all identical, except they live in different namespaces in the same solution.
TypeTwo objects are Webservice references.
例如,例如
public TypeOne ConvertToTypeTwo (TypeTwo typeTwo)
{
var typeOne = new TypeOne();
typeOne.property1 = typeTwo.property1; //no problem on simple types
typeOne.SubType = typeTwo.SubType; //problem!
...
}
上面一行的错误是:
错误166 无法默认转换类型 Type 2. SubType 到 Type One.SubType
我试过这样投
typeOne.SubType = (TypeOne)typeTwo.SubType;
但获取 :
167 错误 167 无法转换类型 Type 2.SubType 到 Type One.SubType
像这样
typeOne.SubType = typeTwo.SubType as TypeOne;
但获取 :
Error 168 Cannot convert type TypeTwo.SubType to TypeOne.SubType via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
I m not sure what other options I have, or if I m just doing something fundamentally wrong. Any thoughts?