开发者

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

开发者 https://www.devze.com 2023-01-24 04:55 出处:网络
This is best illustrated with an example: class Cat { } class Dog { public static implicit operator Cat(Dog d)

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();
  // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消