C#隐式转换操作符是/ as运算 [英] C# Implicit Conversion Operator and is/as operator
问题描述
我创造,我想能够使用任何地方可以用来包裹它的对象之一的复合对象。因此,考虑以下对象:
公共类Foo {}
公共类FooWrapper {
私人美孚_foo =新的Foo();
公共静态隐运营商美孚(FooWrapper包装)
{
返回wrapper._foo;
}
}
我想作以下测试通过:
公共类ConversionTests
{
私人FooWrapper _uat =新FooWrapper();
[测试]
公共无效Can_Use_Is_Operator()
{
Assert.IsTrue(_uat是美孚);
}
[测试]
公共无效Can_Use_As_Operator()
{
Assert.IsTrue(空= _uat为Foo!);
}
}
我已经采取了看MSDN文档:结果
为: http://msdn.microsoft.com/ EN-US /库/ scekt9xw.aspx 结果
为:的 http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.110%29.aspx
的
的是的文件意味着它是不可能的,
请注意,是运营商只考虑引用转换,装箱转换,和拆箱转换。其他转换,如用户定义的转换,不考虑。
块引用>
有谁知道是否有构建FooWrapper这样一种方法/为转换是否行得通呢?实施像IConvertible也许是一个接口?
奖金的问题:任何人都知道为什么AS /是不考虑用户定义的转换
(和对不起,如果这个问题是一个DUP。'为'和'被'似乎是停止词在这里计算器(与大多数搜索引擎),使得它来搜索包含这些关键字的问题相当困难的。)
解决方案
的是文档意味着它是不可能的。
块引用>
该文档是正确的。
有谁知道是否有构建FooWrapper所以/为转换的方式是否行得通呢?
块引用>
我知道了。那没有。 (除了明显,使得
FooWrapper
派生类富
)的
的
的是什么对象的真的是
和为
运营商那里告诉你。不要试图让他们撒谎。
实施像IConvertible也许是一个接口?
块引用>
不。
谁知道为什么AS /是不考虑用户定义的转换?
块引用>
首先,因为像我刚才说的,目的
是
是要告诉你什么是对象的真的是的
二是因为想为求参数中的C#编译器团队希望增加一个新的运营商,比如
FROB
,有一个为
运算符的语义采用用户定义的转换。X FROB富
给你回一个富
在X
终止一个FooWrapper
。描述一下取悦IL您想查看该表达式生成。我认为在这个练习搞你会看到为什么它是一个困难的问题。I'm creating a composite object that I want to be able to be used anywhere one of its wrapped objects could be used. So given the following objects:
public class Foo{} public class FooWrapper{ private Foo _foo = new Foo(); public static implicit operator Foo(FooWrapper wrapper) { return wrapper._foo; } }
I want to make the following tests pass:
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); } }
I've taken a look at the MSDN documentation:
is: http://msdn.microsoft.com/en-us/library/scekt9xw.aspx
as: http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.110%29.aspxThe is documentation implies it is not possible,
Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.
Does anyone know if there's a way to construct FooWrapper so is/as conversions will work? Implementing an interface like IConvertible perhaps?
Bonus question: Anyone know why as/is doesn't consider user-defined conversions?
(And sorry if this question is a dup. 'as' and 'is' appear to be stop words here on stackoverflow (as with most search engines), making it quite difficult to search for questions that contain these keywords.)
解决方案The is documentation implies it is not possible
The documentation is correct.
Does anyone know if there's a way to construct FooWrapper so is/as conversions will work?
I know. There is not. (Aside from, obviously, making
FooWrapper
a derived class ofFoo
.)The
is
andas
operators are there to tell you what an object really is. Don't try to make them lie.Implementing an interface like IConvertible perhaps?
Nope.
Anyone know why as/is doesn't consider user-defined conversions?
First, because like I just said, the purpose of
is
is to tell you what the object really is.Second, because suppose for the sake of argument the C# compiler team wished to add a new operator, say
frob
, that has the semantics of anas
operator that uses user-defined conversions.x frob Foo
gives you back aFoo
whenx
is aFooWrapper
. Describe please the IL you would like to see generated for that expression. I think by engaging in this exercise you'll see why it is a hard problem.这篇关于C#隐式转换操作符是/ as运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!