如何从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)
谢谢.
您需要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)
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |