简体   繁体   English

尝试分配 class 时.Net 转换错误 CS0029 和 CS0266

[英].Net Conversion error CS0029 and CS0266 when trying to assign class

This question was raised by my son, and I could not give a precise answer.这个问题是我儿子提出来的,我无法给出准确的答案。

We get an error when converting/mapping 2 identical classes but in a different namespace.当转换/映射 2 个相同的类但在不同的命名空间中时,我们得到一个错误。 I have created 1 class Astronaut and put the class in 2 different namespaces.我创建了 1 个 class 宇航员并将 class 放在 2 个不同的命名空间中。

When you try to assign c = a we get this error?当您尝试分配 c = a 我们得到这个错误? This is a very basic example, the real issue happens with 2 classes that have multiple classes inside and would be complex to write a mapper for.这是一个非常基本的示例,真正的问题发生在 2 个内部有多个类的类中,并且为其编写映射器会很复杂。

For the record I fixed this using Automapper, just wanted to know the actual reason why this is not working.作为记录,我使用 Automapper 修复了这个问题,只是想知道这不起作用的实际原因。

using Apollo;
using Challenger;
void Main()
{
    var a = new Apollo.Astronaut();
    var b = new Challenger.Astronaut();
    // Cannot implicitely convert type 'Challenger.Astronaut' to 'Apollo.Astronaut'     
    var c = a;
        
    c = b; 
}

namespace Apollo{
    public class Astronaut
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    } 
}  

namespace Challenger{
     public class Astronaut
     {
         public string FirstName { get; set; }
         public string LastName { get; set; }
     }
 }

If you need two different Astronaut types you need to either define a base class or make an interface that they both implement.如果您需要两种不同的 Astronaut 类型,您需要定义一个基本 class 或创建一个它们都实现的接口。

Base class:底座 class:

namespace Space
{
    public class Astronaut
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    } 
}

namespace Apollo
{
    public class Astronaut : Space.Astronaut
    {
    } 
}  

namespace Challenger
{
    public class Astronaut : Space.Astronaut
    {
    } 
}

void Main()
{
    Space.Astronaut a = new Apollo.Astronaut();
    Space.Astronaut b = new Challenger.Astronaut();
    var c = a;
    c = b; 
}

Interface界面

namespace Space
{
    public interface IAstronaut
    {
        string FirstName { get; set; }
        string LastName { get; set; }
    } 
}

namespace Apollo
{
    public class Astronaut : Space.IAstronaut
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    } 
}  

namespace Challenger
{
     public class Astronaut : Space.IAstronaut
     {
         public string FirstName { get; set; }
         public string LastName { get; set; }
     }
}

void Main()
{
    IAstronaut a = new Apollo.Astronaut();
    IAstronaut b = new Challenger.Astronaut();
    var c = a;
    c = b; 
}

But I don't think it's the right way to go.但我认为这不是 go 的正确方法。 An astronaut is an astronaut no matter the mission.无论执行什么任务,宇航员都是宇航员。 So you should define one Astronaut class and a Mission class that defines a mission and who went on it.所以你应该定义一个 Astronaut class 和一个 Mission class 定义一个任务和谁去。 And by the way, you are not trying to "assign class".顺便说一句,您不是在尝试“分配课程”。 You are trying to assign an instance of one class to an instance of another.您正在尝试将一个 class 的实例分配给另一个实例。 It very important to distinguish between classes and instances.区分类和实例非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM