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
implicitorexplicit.Conversion operators can be explicit or implicit. Implicit conversion operators are easier to use, but explicit operators are useful when you want users of the operator to be aware that a conversion is taking place. This topic demonstrates both types.
Example
This is an example of an explicit conversion operator. This operator converts from the type Byte to a value type called Digit. Because not all bytes can be converted to a digit, the conversion is explicit, meaning that a cast must be used, as shown in the Main method.
This example demonstrates an implicit conversion operator by defining a conversion operator that undoes what the previous example did: it converts from a value class called Digit to the integral Byte type. Because any digit can be converted to a Byte, there’s no need to force users to be explicit about the conversion.
from: 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.