开发者

Force type cast between classes of different namespaces

How Force type cast between classes of different namespaces.

Both n开发者_运维知识库amespaces have same class.


You can't cast an object to a type it is not. If it belongs to a different namespace then it is not the same class. You will have to create a converter:

public static Namespace1.SomeClass Convert(Namespace2.SomeClass someClass) {
    Namespace1.SomeClass rtn = new Namespace1.SomeClass();
    rtn.SomeProp = someClass.SomeProp;
    rtn.SomeOtherProp = someClass.SomeOtherProp;
    return rtn;
}

you could even use reflection to set all the properties on Namespace1.SomeClass that have the same name as Namespace2.SomeClass.

Also, if you own the code to one of the classes, you can check into overloading explicit and implicit on your class.


You can create generic Converter so you don't have to do this each time you need to cast a different type of objects,

T ConvertObject<T>(object M) where T : class
{
    // Serialize the original object to json
   // Desarialize the json object to the new type 
 var obj = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(M));
 return obj;
} 
// Test ObjectToCast is type Namespace1.Class, obj is Namespace2 
 Namespace2.Class obj = ConvertObject<Namespace2.Class>(ObjectToCast);

Assuming that both classes are the same this will work.


You can't cast from a Type to a different Type, even if the code of the class is exactly the same.

You can create a Converter capable to convert between the two types, or provide implicit/explicit casts inside both classes implementations or eventually you can try Automapper.


You need to qualify the type:

namespace Foo
{
   class Bar {}
}

namespace Baz
{
   class Bar {}
}

Foo.Bar x = new Foo.Bar();
Baz.Bar y = (Baz.Bar)x;

Of course, this will fail unless there is a conversion defined.


This is not possible. A type include its namespace as part of its full name.

Its like the town of Springfield: same name but from different states. They are all different.

A possible approach would be to overload the cast operator of one of the type so that they can be cast into another type. It won't be a real cast, as the result will be to create a new object with the same value.

public static explicit operator Massachusetts.Springfield(Illinois.Springfield town)
{
     return new Massachusetts.Springfield(town); // or any other code to copy the fields from one type to the other
}


If both classes are serializable, you can serialize the first object to XML, change the "namespace" in the xml and deserialize it again.


The fact that the two classes have the same name doesn't mean anything to the compiler. You may have Foo.Orange and Bar.Orange, but to the compiler it may as well be Apple and Orange. To convert:

namespace Foo
{
   public class Orange{}
   public static explicit operator Foo.Orange(Bar.Orange) { // conversion code }

}
namespace Bar
{
   public class Orange{}
   public static explicit operator Bar.Orange(Foo.Orange) { // conversion code }
}

// somewhere else
Foo.Orange o = new Foo.Orange();
Bar.Orange bar = (Bar.Orange)o; // and vice-versa
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜