关于.net:C#隐式转换运算符,是/作为运算符

C# Implicit Conversion Operator and is/as operator

我正在创建一个复合对象,我希望它可以在任何可以使用它的包装对象的地方使用。因此,考虑到以下对象:

1
2
3
4
5
6
7
8
9
10
public class Foo{}

public class FooWrapper{
    private Foo _foo = new Foo();

    public static implicit operator Foo(FooWrapper wrapper)
    {
       return wrapper._foo;
    }
}

我想通过以下测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ConversionTests
{
     private FooWrapper _uat = new FooWrapper();

     [Test]
     public void Can_Use_Is_Operator()
     {
          Assert.IsTrue(_uat is Foo);
     }

     [Test]
     public void Can_Use_As_Operator()
     {
          Assert.IsTrue(null != _uat as Foo);
     }
}

我看了一看msdn文档:是:http://msdn.microsoft.com/en-us/library/scekt9xw.aspx地址:http://msdn.microsoft.com/en-us/library/cscssdfbt%28v=vs.110%29.aspx

IS文件暗示这是不可能的,

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.

有人知道是否有一种方法来构建FoowRapper,那么IS/AS转换将起作用吗?实现像IConvertible这样的接口?

额外问题:有人知道为什么as/is不考虑用户定义的转换吗?

(如果这个问题是骗人的话,我很抱歉。)因为"和"似乎是stackoverflow中的停止词(与大多数搜索引擎一样),所以很难搜索包含这些关键字的问题。)


布尔奇1(P)The documentation is correct.(p)布尔奇1(P)我知道There is not.(Aside from,Obviously,Making EDOCX1 plus 3 a derived class of EDOCX1 punishment 4.)(p)(P)The EDOCX1 Oric 0 and EDOCX1 Plenifical 1 Americano operators are there to tell you what an object really is.Don't try to make them lie.(p)布尔奇1(P)没有。(p)布尔奇1(P)首先,因为我只是说,The purpose of EDOCX1 penal 0 is to tell you what the object really is.(p)(P)第二,因为它支持向C.3535 Argument的Sake提供服务;Compiler Team wished to add a new operator,say EDOCX1 big 12,that has the seminatics of an EDOCX1 singinal 1/operator that used-defined conventions.EDOCX1 23(音译)给你回EDOCX1(音译)4(音译)描述一下,请看一代人的表达。I think by engaging in this exercise you'll see why it is a hard problem.(p)


(P)In order for EDOCX1 penal 3 communal to be considered a EDOCX1 penalty 4 communal,EDOCX1 penalty 3 communal 3 would have to inherit from EDOCX1 penal 4.这是:(p)字母名称(P)The problem,though,is that if you wanted your wrapper to also wrap EDOCX1 universal objects,you can't.because C′35;does not support multiple inheritance.(p)(P)Typically if you need a wrapper that will act like multiple objects,you have it implement interfaces.So,for example,you would have:(p)字母名称(P)and of course EDOCX1 penography 8 communal would have to implement all the methods of EDOCX1 penographic 9 communal and EDOCX1 commercial 10,passing the calls on the proper contained object.因此,如果EDOCX1(英文)9宣布一个EDOCX1(英文)12(英文)方法,你将有(在EDOCX1(英文)8(英文)级:(p)字母名称(P)That's implicait interface implementation.You might also have to look into explicit implementation of interface methods.(p)(P)创造一个错误的人(p)字母名称(P)你的测试,他们,将使用DOCX1的字母0和DOCX1的英文1检查界面Rather than concrete classes。(p)字母名称


(P)The only option for EDOCX1 pental 0/EDOCX1 sil文1 to work with the wrapper class is if wrapping class is indeed wrapped class.如果你想要的是错误的,不是你可以产生于它…(p)(P)Now what you are trying to actually achieve is not possible in general case--统计方法甚至很难。(p)字母名称(P)如果你的法典使用的接口与目标互动,你的任务是作为替代接口与替代执行是完全支持的。Either manual wrapper or automatically created via reflection/emi would work.(p)字母名称(P)我对为什么不考虑其他的转化的想法:(p)

  • Expectation of the operation is to be very quick and light weight check-other conventions may need to create objects/unredicatable in how long it would take,
  • It significantly increases number of options that can be considered by compiler so slowing it down
  • In many other cases where object are used conversions will not be considered,so may lead to inconsistent code behavior where e EDOCX1 indicational reports success but object fails to be used correctly(like instance added to list).