如何在具有值类型约束的泛型声明类型之间进行复制
我有一个通用方法,可以在值类型之间复制值。 即使有结构约束,以下方法也会出现设计时错误。 知道如何在值之间复制或转换吗?
private Ttgt MyMethod<Tsrc,Ttgt>(Tsrc SourceObject)
where Tsrc : struct
where Ttgt : struct
{
//Error:cannot implictly convert type 'Tsrc' to 'Ttgt'
Ttgt returnObject = SourceObject;
//Error:Cannot convert type 'Tsrc' to 'Ttgt'
Ttgt returnObject = (Ttgt)SourceObject;
return returnObject;
}
I have a generic method that copies values between value types. The following approaches give a design time error, even with the struct constraint. Any idea how I can copy or cast between the values?
private Ttgt MyMethod<Tsrc,Ttgt>(Tsrc SourceObject)
where Tsrc : struct
where Ttgt : struct
{
//Error:cannot implictly convert type 'Tsrc' to 'Ttgt'
Ttgt returnObject = SourceObject;
//Error:Cannot convert type 'Tsrc' to 'Ttgt'
Ttgt returnObject = (Ttgt)SourceObject;
return returnObject;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设有一个注册的类型转换器,用于您尝试在一些反射魔法之间进行转换的类型,则可以解决这个问题:
但是开箱即用,它的用途非常有限,因为 bool 和 int 之间没有转换器例子。 你想解决什么问题?
我还发现了另一个问题,其中包含一些疯狂的转换代码在里面。
编辑:
您的评论清楚地表明您正在尝试在域对象和某种视图/契约模型之间执行对象到对象的映射。 您看过AutoMapper吗?
Given that there is a registered type converter for the types that you're trying to convert between a little reflection magic could do the trick:
But out of the box it would be of very limited use since there is no converter between bool and int for example. What problem are you trying to solve?
I also discovered another question with some crazy conversion code in it.
Edit:
Your comment makes it clear that you are trying to perform object to object mapping between domain objects and some kind of view/contract model. Have you looked at AutoMapper?
您无法在任意类型之间进行转换,除非有可访问的转换运算符。
You cannot convert between arbitrary types, unless there is an accessible conversion operator.
两者被定义为不同的类型。 尽管它们都是结构体,但它们不是同一类型。
将源和目标定义为相同类型:
The two are defined as different types. Even though they are both structs, they are not the same types.
Define both souce and target as the same type:
这就是“设计”。 您正尝试在两个不相关的值类型之间进行转换。 这永远不会成功,因此它被标记为错误。
这对于所有值类型都是如此,因为它们是隐式密封的。 为了在 TSrc -> 之间进行强制转换 要成功,两种类型之间必须存在类层次关系。 这不可能是因为所有值类型都是密封的,因此一个值类型无法从另一个值类型派生。
成功的唯一方法是在类型之间有一个自定义转换运算符。 情况可能是这样。 但是,在处理泛型类型时,将不会处理自定义转换运算符。
This is "By Design." You are attempting to cast between two unrelated value types. This will never succeed and therefore it is flagged as an error.
This will be true of all value typse because they are implicitly sealed. In order for a cast between TSrc -> Ttgt to succeed, there must be a class hierarchy relationship between the two types. This cannot be because all value types are sealed hence there is no way one can derive from the other.
The only way this could succeed is if one had a custom conversion operator between the types. This may be the case. However when dealing with generic types, custom conversion operators will not be processed.
Convert 类就是为了这个目的而存在的。
另请注意,您可以执行以下操作:
The Convert class exists for this exact purpose.
Also note that you could do this: