Unable to cast C#

别等时光非礼了梦想. 提交于 2019-12-14 03:49:41

问题


I have 2 different projects in my solution.

In one I have a class called MyClass1, and in the other I have MyClass2 These classes are identical in all but name.

In one project I have a list of Objects. During runtime the list gets filled with MyClass1 objects that we casted to an object.

At the end I want to cast each object in the list to a MyClass2 object.

Currently it throws an exception saying Unable to cast object of type MyClass1 to MyClass2.

My Code:

List<Object> _tempObjects = new List<Objects>();
foreach(Object myObjectInput in _tempObjects)
{
  MyClass2 temp = (MyClass2) myObjectInput; // here is where it dies
}

The two classes are the same, just different names. I have also tried:

 MyClass2 temp = myObjectInput as MyClass2; 

回答1:


Casting doesn't work that way. When casting (an object statically known as) object to another type, it must already be an instance of that type for it to work. Maybe you'd like to convert from one to the other with AutoMapper, e.g.

Mapper.CreateMap<MyClass1, MyClass2>();

// later...
MyClass2 temp = Mapper.Map<MyClass2>(myObjectInput);

Or manually copy the properties, maybe in a constructor:

public MyClass2(MyClass1 other)
{
    this.SomeProperty = other.SomeProperty;
    // etc
}

MyClass2 temp = new MyClass2((MyClass1)myObjectInput);

More likely, what you should do is make the projects share MyClass in a way that .NET understands and supports natively: by putting it in a project that can be referenced by both projects. If one project should reference the other, do that; otherwise, create a third project as a library.




回答2:


What you are trying to do is not possible. Even though the classes have the same contents, they are still different types.

What you can do is:

  • create an interface with all the things that are shared among the 2 types
  • let your 2 types implement that interface
  • cast to this interface when you get the object out of the list



回答3:


You can't simply cast one type to another without an explicit (or implicit) cast operator being implemented.

First, i'd question why you have two identical classes at all. But, if this must be done, you'll have to declare an explicit cast conversion between one to the other:

public class Class1
{
    public string Name { get; set; }

    public static explicit operator Class1(Class2 cls)
    {
        return new Class1 { Name = cls.Name };
    }
}

public class Class2
{
    public string Name { get; set; }

    public static explicit operator Class2(Class1 cls)
    {
        return new Class2 { Name = cls.Name };
    }
}



回答4:


These classes are identical in all but name.

Nope. That's not how static typing works. The classes may be intuitively similar when looking at their implementation, but as far as the compiler is concerned they are entirely different. One can not be directly cast to another.

List<Object> _tempObjects = new List<Objects>();

This adds a third type to the mix, Object. A List<Object> can contain anything as far as the compiler is concerned, it's not limited to your two classes. So that definitely can't be cast to an instance of your class.

If your two projects need to use the same type, extract that type into a common project and your two projects can reference that common project. Then you can just use that type everywhere:

List<MyClass1> _tempObjects = new List<MyClass1>();



回答5:


You can't cast objects just because their implemented identically. They must either inherit from the same baseclass or from the same interface, or be a subclass of each other (and then you can only cast from child to parent class).




回答6:


In one I have a class called MyClass1, and in the other I have MyClass2 These classes are identical in all but name.

But MyClass1 is not MyClass2 and MyClass2 is not MyClass1. You cannot cast one to the other. You need to map them. Basically for each property, set the value from one instance to another.




回答7:


You can not cast these two objects, even if they have same implementation, before you implement explicit cast operator or or type is the one ur current type is derivative from/to. Here is a cheat sheet of casting in .NET, look please http://www.codeproject.com/Articles/5044/Cheat-Sheet-Casting-in-VB-NET-and-C




回答8:


If MyClass1 and MyClass2 are meant to be similar, you could introduce an interface (let's call is IMyClass) and have MyClass1 and MyClass2 implement said interface and then your code (modified below) would work by casting to the shared interface.

foreach(Object myObjectInput in _tempObjects)
{
  IMyClass temp = (IMyClass) myObjectInput;
}


来源:https://stackoverflow.com/questions/26103256/unable-to-cast-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!