Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell which interface is returned by a method

Given this code snippet that can be readily pasted into Linqpad (or slightly modified in a Visual Studio console solution):

void Main()
{
    var cat = this.GetCat();
    var dog = this.GetDog();
    cat.Think();
    cat.ThinkHarder();
    //dog.Think(); // Does not compile.
    //dog.ThinkHarder(); // Does not compile.

    //if ([dog is returned as ISmartAnimal]) // What to put here?
        ((ISmartAnimal)dog).Think(); // Compiles, runs, but shouldn't.

    reportTypeProperties(cat);
    reportTypeProperties(dog);
}

interface IAnimal
{
    string Name { get; set; }
}

interface ISmartAnimal : IAnimal
{
    void Think();
}

class Animal : IAnimal, ISmartAnimal
{
    public string Name { get; set; }
    public void Think() { }
}

ISmartAnimal GetCat()
{
    return new Animal();
}

IAnimal GetDog()
{
    return new Animal();
}

static void reportTypeProperties(object obj)
{
    var type = obj.GetType();
    Console.WriteLine("Type: {0}", type.Name);
    Console.WriteLine("Is smart? {0}", obj is ISmartAnimal);
}

static class ext
{
    public static void ThinkHarder(this ISmartAnimal animal)
    { }
}

The output of reportTypeProperties shows that dog, although returned as IAnimal, "is" an ISmartAnimal. (Same for both objects)

Type: Animal
Is smart? True

This is because GetType() returns the concrete type of the object, not its current interface.

My question. Is there a way to tell that dog is returned as IAnimal? (see pseudocode). The compiler knows (so does quickview). Suppose I had some animal object and I wanted to inspect in runtime code whether or not I can make it Think().

Background:
This may seem an academic exercise. And it may seem strange to have a class (Animal) implement an interface (ISmartAnimal) that you don't want to expose always. But I ask because I encountered something similar in Entity Framework. If you want you can read about it here, but it diverts to EF-specific features. If you don't want to delve into that it suffices to say that it's imperative that Animal implement both interfaces.


Disclaimer:
"Any resemblance to real animals is purely coincidental :)"

like image 668
Gert Arnold Avatar asked Jan 30 '12 11:01

Gert Arnold


1 Answers

It sounds like you're interested in the compile-time type of the dog variable. You can sort of get this, by making ReportTypeProperties generic and letting the compiler infer the type based on the variable's type:

static void ReportTypeProperties<T>(T obj)
{
    Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
    Console.WriteLine("Actual type: {0}", obj.GetType().Name);
    Console.WriteLine("Is smart? {0}", obj is ISmartAnimal);
}

Note that this can be gamed in various ways, e.g.

object dog = GetDog();
ReportTypeProperties(dog); // Would show as object

or

IAnimal dog = GetDog();
ReportTypeProperties<object>(dog); // Would show as object

It's not really clear what the bigger picture is here - it feels unlikely to me that going in this direction is going to lead to a good design.

like image 186
Jon Skeet Avatar answered Sep 21 '22 19:09

Jon Skeet