是否有可能在C#中的以下方式重载一个通用的类型转换操作符? [英] Is it possible in C# to overload a generic cast operator in the following way?
问题描述
只是想知道是否有无论如何代表在C#3.5以下代码:
公共结构美孚< T> {
公共美孚(T项目){
this.Item =项目;
}
公共牛逼项{搞定;组; }
公共静态明确经营者的Foo< U> (富< T>一种)
,其中U:T已{
返回新的Foo< U>((U)a.Item)
}
}
感谢
转换运算符不能通用。从规范部分10.10,这里有一个转换操作符说明符的格式为:
的转换操作符声明符:
隐含的运营商类型(键入 标识)
明确运营商的类型(键入 标识)
块引用>
通过比较这一点,说,方法头:
法头:
属性<子>选择 法修饰语选择 部分的 <子>选择 返回型
成员名称 类型参数列表选择 (正式的参数列表<子>选择 )
类型参数约束子句选择
块引用>
(很抱歉的格式 - 不知道怎么做的更好)
请注意,运营商格式不包括类型参数列表或类型参数约束。
Just wondering if there is anyway to represent the following code in C# 3.5:
public struct Foo<T> { public Foo(T item) { this.Item = item; } public T Item { get; set; } public static explicit operator Foo<U> ( Foo<T> a ) where U : T { return new Foo<U>((U)a.Item) } }
Thanks
解决方案Conversion operators can't be generic. From the spec section 10.10, here's the format of a conversion-operator-declarator:
conversion-operator-declarator: implicit operator type ( type identifier ) explicit operator type ( type identifier )
Compare this with, say, a method-header:
method-header: attributesopt method-modifiersopt partialopt return-type member-name type-parameter-listopt ( formal-parameter-listopt ) type-parameter-constraints-clausesopt
(Sorry about the formatting - not sure how to do it better.)
Note that the operator format doesn't include a type parameter list or type parameter constraints.
这篇关于是否有可能在C#中的以下方式重载一个通用的类型转换操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!