Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading explicit CAST operator

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
}
like image 953
manuellt Avatar asked Jan 09 '12 17:01

manuellt


1 Answers

You can implement custom type casting using the conversion operators implicit or explicit.

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.

struct Digit
{
    byte value;

    public Digit(byte value)  //constructor
    {
        if (value > 9)
        {
            throw new System.ArgumentException();
        }
        this.value = value;
    }

    public static explicit operator Digit(byte b)  // explicit byte to digit conversion operator
    {
        Digit d = new Digit(b);  // explicit conversion

        System.Console.WriteLine("Conversion occurred.");
        return d;
    }
}

class TestExplicitConversion
{
    static void Main()
    {
        try
        {
            byte b = 3;
            Digit d = (Digit)b;  // explicit conversion
        }
        catch (System.Exception e)
        {
            System.Console.WriteLine("{0} Exception caught.", e);
        }
    }
}
// Output: Conversion occurred.

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.

struct Digit
{
    byte value;

    public Digit(byte value)  //constructor
    {
        if (value > 9)
        {
            throw new System.ArgumentException();
        }
        this.value = value;
    }

    public static implicit operator byte(Digit d)  // implicit digit to byte conversion operator
    {
        System.Console.WriteLine("conversion occurred");
        return d.value;  // implicit conversion
    }
}

class TestImplicitConversion
{
    static void Main()
    {
        Digit d = new Digit(3);
        byte b = d;  // implicit conversion -- no cast needed
    }
}
// Output: Conversion occurred.

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.

like image 59
Adam Houldsworth Avatar answered Oct 19 '22 11:10

Adam Houldsworth