C # failed to start

I have two different projects in my solution.

In one I have the class MyClass1 and in the other I have MyClass2 These classes are identical in everything except the name.

In one project I have a list of objects. At runtime, the list is filled with MyClass1 objects, which we throw in the object.

In the end, I want every object in the list to be a MyClass2 object.

It is currently throwing an exception saying "Cannot pass an 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've also tried:

 MyClass2 temp = myObjectInput as MyClass2; 

      

+3


source to share


8 answers


Casting doesn't work. When casting (an object statically known as) object

for another type, it must be an instance of that type for it to work. Perhaps you want to convert from one to another using AutoMapper , for example

Mapper.CreateMap<MyClass1, MyClass2>();

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

      

Or manually copy the properties, perhaps in the constructor:



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

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

      


Chances are, what you need to do is make share projectsMyClass

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 needs to link to another, do so; otherwise, create a third project as a library.

+11


source


What you are trying to do is not possible. Although the classes have the same content, they are still different types.

What can you do:



  • create an interface with all things that are shared between the two types
  • let your 2 types implement this interface
  • pass this interface when you take the object out of the list
+11


source


You cannot simply pass one type to another without using an explicit (or implicit) translation operator.

First, I would pose the question why you have two identical classes. But, if you need to do this, you have to declare an explicit conversion conversion between the same:

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 };
    }
}

      

+6


source


These classes are identical in all but the name.

Nope. This is not how static printing works. The classes may be intuitively similar when considering their implementation, but as far as the compiler is concerned, they are completely different. You cannot directly transfer to another.

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

      

This adds a third type in 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 it definitely cannot be applied to an instance of your class.

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

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

      

+1


source


You cannot throw objects just because they perform the same way. They must either inherit the same base class, or the same interface, or subclass each other (and then you can only cast from child to parent class).

0


source


In one I have the class MyClass1, and in the other I have MyClass2 These classes are identical in everything except the name.

But MyClass1

not MyClass2

, MyClass2

not MyClass1

. You cannot cast one upon the other. You need to match them. Basically for each property, set a value from one instance to another.

0


source


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

0


source


If MyClass1 and MyClass2 are supposed to be similar, you can inject an interface (let the call be IMyClass) and MyClass1 and MyClass2 implement the mentioned interface, and then your code (modified below) will work by casting into a common interface.

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

      

0


source







All Articles