简体   繁体   中英

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

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.

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;

Unfortunately even try/catch won't help you in this scenario for arbitrary objects such as System.Object and interfaces. The C# compiler only considers users defined conversions if they are visible at the point of the compilation. Hence for System.Object and interfaces they are not considered.

However in the sample code above you're dealing with only concrete types. Actual class and struct types (not interfaces or System.Object). If your scenario only includes concrete types then the easiest way to check is to do the cast. The compiler will error if there is no available conversion between 2 concrete types

var dog = new Dog();
var cat1 = (Cat)dog; // Compiles 
var str = "example";
var cat2 = (Cat)dog; // Does not compile

The conversion operators in C# are designed to be used at compile-time, so that the compiler will invoke them at the times where it makes sense. The type of test that you want is a runtime test, and so you'll be forced to use reflection and look for the compiler-generated conversion methods and invoking them yourself.

If you can control the types that you need this conversion operator ( Dog in the example), then I think you should depart from the compile-time conversion operators and create a more runtime-friendly option (you can also use both of course):

interface IConvertible<out T> {
  T Convert();
}
class Cat { }
class Dog : IConvertible<Cat> {
  public Cat Convert() {
    return new Cat();
  }
}

You check it like this:

object o; 
// ... 
if (o is IConvertible<Cat>) { // you should also check if o is Cat ...
  Cat c = ((IConvertible<Cat>)o).Convert();
  // ...
}

Or this:

object o; 
// ... 
var cc = o as IConvertible<Cat>;
if (cc != null) {
  Cat c = cc.Convert();
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM