Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

c# - 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 :)"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...