重载明确的类型转换操作符 [英] overloading explicit CAST operator
本文介绍了重载明确的类型转换操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这块code:
公共类腿:ProxiestChild
{
公共虚拟名称{;组; }
}
问题是:
VAR腿=新的腿(); //腿不是腿,而是ProxiedLeg
VAR trueleg =(腿)腿; //例外腿是ProxiedLeg
我需要这样的事情
公共类ProxiestChild
{
//一些方法重载明确的CAST
//那里接收代理对象我返回的对象非代理
//被铸造
}
解决方案
您可以使用转换操作符隐
或明确实现自定义类型转换
这里详细介绍:
<一个href="http://msdn.microsoft.com/en-us/library/85w54y0a(v=VS.100).aspx">http://msdn.microsoft.com/en-us/library/85w54y0a(v=VS.100).aspx
千万要小心,为了便于阅读它往往被混淆地看到一种神奇强制转换为另 - 人并不总是先想到有在游戏转换操作符
I have this piece of code:
public class Leg : ProxiestChild
{
public virtual Name { get; set; }
}
the problem is:
var leg = new Leg(); // leg is not Leg, instead ProxiedLeg
var trueleg = (Leg)leg; // exception leg is a ProxiedLeg
i need something like this
public class ProxiestChild
{
// some method that overloads explicit CAST
// where receiving the proxied object i returns the unproxied object
// to be casted
}
解决方案
You can implement custom type casting using the conversion operators implicit
or explicit
as detailed here:
http://msdn.microsoft.com/en-us/library/85w54y0a(v=VS.100).aspx
Do be careful with this, for readability it can often be confusing to see one type magically cast to another - people don't always first think that there are conversion operators in play.
这篇关于重载明确的类型转换操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文