Alex Rivera | Logout

Is there a "cheap and easy" way to tell if an object implements an explicit/implicit cast operator to a particular type?

Asked 2010-11-08T17:15:55.797
9

This is best illustrated with an example:

class Cat
{
}

class Dog
{
    public static implicit operator Cat(Dog d)
    {
        return new Cat();
    }
}

I want to tell, for an arbitrary object, if I can cast it to Cat. Sadly I cannot seem to use the is/as operator.

void Main()
{
    var d = new Dog();
    if (d is Cat) throw new Exception("d is Cat");
    var c1 = (Cat)d; // yes
    //var c2 = d as Cat; // Won't compile: Cannot convert type 'Dog' to 'Cat' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

}

I'm hoping to avoid a try/catch(InvalidCastException) as I may be doing this a lot and this would be quite expensive.

Is there a way to do this cheaply and easily?

edit: Thanks for the answers guys - votes for all, wish I could give you all the Tick, but it's going to Marc for the most general solution (bonus vote for punching it out on an ipod). However Jordao's solution managed to hone in on what I need and not what I asked for so it is probably what I'm going with.

Edit
Report

1 Answer

7

Conversion operators aren't checked by is/as. You would needyo use reflection both to check for, and to invoke, said operators. I would write a generic static class

static class Convert<TFrom,TTo> {}

And in the static constructor check for the method, an use Delegate.CreateDelegate to create a

Func<TFrom,TTo>

And store that in a static field. Then you have fast access to the typed method:

public static TTo Convert(TFrom obj) { return del(obj); }
public static bool CanConvert { get { return del != null; } }
private static readonly Func<TFrom,TTo> del;
answered 2010-11-08T17:20:48.583

Your Answer