两个不同类的隐式运算符

Leo*_*kan 2 .net c# oop

如何从2个不同的类执行隐式运算符?

public class A
{
    public int one { get; set; }
    public int two { get; set; }

    public static implicit operator A(B v)
    {
        \\one = v.one; \\ static Error.
        \\this.one = v.one; \\ Error
    }
}

public class B
{
    public int one { get; set; }
    public int two { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}
static void Main(string[] args)
{
    A a = new A();
    B b = new B();

    a = b;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dav*_*idG 6

您需要A在隐式转换中返回一个新实例:

public class A
{
    public int one { get; set; }
    public int two { get; set; }

    public static implicit operator A(B v)
    {
        return new A
        {
            one = v.one,
            two = v.two
        };
    }
}
Run Code Online (Sandbox Code Playgroud)