为什么这种隐式转换是非法的? [英] Why is this implicit conversion illegal?
问题描述
Hello Code Project Guru。
我之前写过一个类,它是C#decimal类型的包装器。这允许我添加我需要的各种自定义功能。我现在正试图使这个包装类通用,但我遇到了一个我不太明白的编译错误...
超简化的例子我的代码如下。我得到的编译器错误是在最后一行代码:
Hello Code Project gurus.
I had previously written a class that was a wrapper around the C# decimal type. This allowed me to add all kinds of custom functionality that I needed. I am now trying to make this wrapper class generic, but I am encountering a compiler error that I don't quite understand...
The super-simplified example of my code is below. The compiler error I get is on the last line of code:
Cannot implicitly convert type 'int' to 'ConsoleApplication1.testing.WrapperForFloat'. An explicit conversion exists (are you missing a cast?)<
我不明白为什么,当
int y = testImplied
遇到
,编译器知道WrapperForFloat继承了
隐式运算符int(Wrapper< T> val)
来自Wrapper的
并编译转换,但它不知道
testImplied = x
应该使用WrapperFloat继承的
隐式运算符Wrapper< T>( int val)
任何解释都将不胜感激。
< b>我尝试了什么:
What I don't understand is why, when
int y = testImplied
is encountered, the compiler knows that WrapperForFloat is inheriting the
implicit operator int(Wrapper<T> val)
from Wrapper and compiles the conversion, but it does not know that
testImplied = x
should use WrapperFloat's inherited
implicit operator Wrapper<T>(int val)
Any explanation would be greatly appreciated.
What I have tried:
namespace ConsoleApplication1
{
using System;
namespace testing
{
public class Wrapper<T>where T: IConvertible
{
private T _value;
public Wrapper()
{
_value = default(T);
}
public Wrapper(T intialValue)
{
_value = intialValue;
}
public static implicit operator Wrapper<T>(int val)
{
var x = (T)Convert.ChangeType(val, typeof(T));
return new Wrapper<T>(x);
}
public static implicit operator int(Wrapper<T> val)
{
return (int)Convert.ChangeType(val._value, typeof(int));
}
}
public class WrapperForFloat : Wrapper<float>
{
public WrapperForFloat() : base() { }
public WrapperForFloat(float initialval) : base(initialval) { }
}
class Program
{
static void Main(string[] args)
{
var testExplicit = new Wrapper<float>(10f);
var testImplied = new WrapperForFloat(11f);
int x = testExplicit;
int y = testImplied;
testExplicit = y;
testImplied = x;
}
}
}
}
推荐答案
它不起作用,因为返回类型是Wrapper< t>,而不是WrapperForFloat。
基本上,你这样做:
It doesn't work because the return type is a Wrapper<t>, not a WrapperForFloat.
Essentially, you're doing this:
WrapperForFloat wff = new Wrapper<float>(x);
那是行不通的。编译器不会从Wrapper< float>进行隐式向下转换。到你的WrapperForFloat。
[edit]
它不起作用,因为Wrapper类可能无法实现所有暴露的方法WrapperForFloat类,因此您无法将生成的强制转换对象视为真正的WrapperForFloat实例。
[/ edit]
没有任何好办法解决这个问题。请参阅此讨论 [ ^ ]以及一些可能的解决方法。
That isn't going to work. The compiler won't do an implicit downcast from Wrapper<float> to WrapperForFloat for you.
[edit]
It doesn't work because the Wrapper class may not implement all of the methods exposed by the WrapperForFloat class, so you can't treat the resulting cast object as a real WrapperForFloat instance.
[/edit]
There isn't any nice way around this. See this discussion[^] and some possible work arounds.
testImplied = x
should use WrapperFloat's inherited
implicit operator Wrapper<T>(int val)
为什么会这样?该运算符从int转换为Wrapper< t>,您要求从int转换为WrapperForFloat。即使这确实有效,你也无法从Wrapper< t>转发。 to WrapperForFloat
将此添加到WrapperForFloat
Why would it? That operator converts from int to Wrapper<t>, you are asking for a conversion from int to WrapperForFloat. Even if that did work you can't upcast from Wrapper<t> to WrapperForFloat
Add this to WrapperForFloat
public static implicit operator WrapperForFloat(int val)
{
return new WrapperForFloat(val);
}
这篇关于为什么这种隐式转换是非法的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!