在通用 C# 类中链接隐式运算符
对于以下通用 C# 类,我想将 T 转换为 K:
public abstract class ValueType<T,K> : IValueType<T> where K : ValueType<T,K>,new()
{
public abstract T Value { get; set; }
public static implicit operator ValueType<T,K>(T val)
{
K k = new K();
k.Value = val;
return k;
}
}
如果我要实现直接运算符隐式运算符 K(T val)
它将导致编译时错误 (CS0556 )。
我想我可以尝试链接隐式运算符:
public static implicit operator K(ValueType<T,K> vt){
return (K)val;
}
但下面的示例仍然抱怨它无法转换:
public class Test : ValueType<int, Test>
{
public override int Value{ get; set; }
}
Test t = 6; //complains it's unable to be converted
Console.WriteLine(t.Value);
如果可能的话,我真的想避免显式转换。
这个问题扩展了我之前提出的另一个问题。
For the following generic c# class, I'd like to convert T to K:
public abstract class ValueType<T,K> : IValueType<T> where K : ValueType<T,K>,new()
{
public abstract T Value { get; set; }
public static implicit operator ValueType<T,K>(T val)
{
K k = new K();
k.Value = val;
return k;
}
}
If I were to implement a direct operator implicit operator K(T val)
it would result in a compile-time error (CS0556).
I thought I could try chaining implicit operators:
public static implicit operator K(ValueType<T,K> vt){
return (K)val;
}
but the following example still complains that it can't be converted:
public class Test : ValueType<int, Test>
{
public override int Value{ get; set; }
}
Test t = 6; //complains it's unable to be converted
Console.WriteLine(t.Value);
I really want to avoid explicitly casting if possible.
This question extends upon another SO question I've previously raised.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现您自己的隐式转换逻辑的规则非常严格,您可能应该非常熟悉第 6.4.4 节(转换:用户定义的隐式转换,对于 C# 8 第 10.2 节)和 10.10.3(类:转换运算符,对于如果您要做像这样特别复杂的事情,请参阅规范的 C# 8 第 15.10.4 节。
简而言之,您应该了解的一些关键规则是:
The rules for implementing your own implicit conversion logic are quite strict and you should probably become very familiar with sections 6.4.4 (Conversions:User-defined implicit conversions, for C# 8 section 10.2) and 10.10.3 (Classes:Conversion operators, for C# 8 section 15.10.4) of the specification if you're going to do particularly complicated ones like this.
Briefly, a few key rules you should know are:
编译器不会链接转换,因此解决问题的方法不起作用。
隐式强制转换在类型检查方面相当严格。如果编译器知道类型,您的第一个代码段和
Test
类就会工作:问题是您的
ValueType
- 从类型系统的角度来看 - 并不总是Test
,因此隐式转换不适用于此处。Eric Lippert 写了一篇关于这种通用自我的博客文章顺便说一下参考——值得一读!
Casts are not chained by the compiler, so that way of solving the issue doesn't work.
Implicit casts are quite strict in the type checking. Your very first snippet and the
Test
class do work if the compiler knows the type:The problem is that your
ValueType<int, Test>
- from the type system point of view - is not always aTest
, so that the implicit conversion doesn't apply there.Eric Lippert wrote a blog post on this kind of generic self-referencing by the way - worth a read!
据我所知,我不认为你可以将演员表链接在一起,对此感到抱歉。
我一直在研究如何创建解析器,如果可能的话,就必须有一个无限循环来找到从
T
到K
的连接。我不确定 C# 解析器会尝试这样做,但不幸的是,我的钱是不会的!As far as I know, I don't think that you can chain casts together, sorry about that.
I've been studying how to create parsers, and if this was possible, there would have to be an indefinite loop to find the connection from
T
toK
. I'm not sure that the C# parser would try doing that, but my money is on no, unfortunately!这就是我的想法。这不是 OP 问题的答案,但据我所知,从 C# 规则来看,无论如何这是不可能的。
因此,我所做的就是在具体类中实现依赖于抽象类中定义的转换算法的隐式运算符。
我的课程:
用法:
Here is what I came up to. Not an answer to the OP question, but as far as I look for, from C# rules it is not possible anyway.
So what I did it to implement the implicit operator in concrete class that rely on the conversion algorithm defined in the abstract class.
My classes :
Usage :