[英]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): 鉴于此代码段可以轻松粘贴到Linqpad中(或在Visual Studio控制台解决方案中稍作修改):
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. reportTypeProperties
的输出显示dog
虽然作为IAnimal返回,但“is”是一个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. 这是因为
GetType()
返回对象的具体类型,而不是其当前接口。
My question. 我的问题。 Is there a way to tell that
dog
is returned as IAnimal? 有没有办法告诉
dog
被归还为IAnimal? (see pseudocode). (见伪代码)。 The compiler knows (so does quickview).
编译器知道(quickview也是如此)。 Suppose I had some animal object and I wanted to inspect in runtime code whether or not I can make it
Think()
. 假设我有一些动物对象,我想在运行时代码中检查是否可以使它成为
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.
让一个类(Animal)实现一个你不想总是暴露的接口(ISmartAnimal)似乎很奇怪。 But I ask because I encountered something similar in Entity Framework.
但我问,因为我在Entity Framework中遇到过类似的东西。 If you want you can read about it here , but it diverts to EF-specific features.
如果您需要,可以在此处阅读,但它会转向EF特定功能。 If you don't want to delve into that it suffices to say that it's imperative that
Animal
implement both interfaces. 如果您不想深入研究,那么就足以说
Animal
必须实现这两个接口。
Disclaimer: 免责声明:
"Any resemblance to real animals is purely coincidental :)" “任何与真实动物的相似之处纯属巧合:)”
It sounds like you're interested in the compile-time type of the dog
variable . 听起来你对
dog
变量的编译时类型感兴趣。 You can sort of get this, by making ReportTypeProperties
generic and letting the compiler infer the type based on the variable's type: 你可以排序得到这个,通过使
ReportTypeProperties
通用的,让编译器推断基于变量的类型类型:
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, eg 请注意,这可以通过各种方式进行游戏,例如
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. 现在还不是很清楚这里的大局是什么 - 我不太可能朝着这个方向前进,这将导致一个好的设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.