Type.GetInterface和嵌套类型 [英] Type.GetInterface and nested types

查看:107
本文介绍了Type.GetInterface和嵌套类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了一个很奇怪的行为与Type.GetInterface和嵌套类型。

I just discovered a very strange behavior with Type.GetInterface and nested Types.

下面的示例code将显示这个问题,我使用的是接口的Type.FullName检查一个给定的类型是否从该接口派生:

The following sample code will show the problem, I am using the Type.FullName of an interface to check whether a given type derives from that interface:

public interface IStandardInterface {}
public class StandardClass : IStandardInterface {}

class Program
{
	public interface INestedInterface {}
	public class NestedClass : INestedInterface { }

	static void Main()
	{
		var stdIfName = typeof (IStandardInterface).FullName;
		var nestedIfName = typeof (INestedInterface).FullName;

		var std = typeof(StandardClass).GetInterface(stdIfName);
		var nested = typeof(NestedClass).GetInterface(nestedIfName);
	}
}

如果我执行code以上它的工作原理StandardClass的,但不是NestedClass。

If I execute the code above it works for StandardClass but not for NestedClass.

  • 性病具有的typeof值(IStandardInterface)
  • 在嵌套具有null值


这是行为预期或一个错误?如果预计你能解释一下为什么吗?

Is this behavior expected or a bug? If it is expected could you explain why?

我使用.NET Framework 3.5版SP1。

I use .net Framework version 3.5 SP1.

推荐答案

扩大对马克的回答。

虽然它没有记录,该GetInterface API将分手你通过在基于上次的位置,名称为。在名称。一切都来的的权利。将被认为是有问题的接口的短名称。

Although it's not documented, the GetInterface API will break up the name you pass in based on the position of the last "." in the name. Everything to the right of the "." will be assumed to be the short name of the interface in question.

这提出了一个问题嵌套类型,因为他们将有哪些是ContainingTypeName + NestedTypeName的名称。所以,当你传递的全名实际GetInterface它最终寻找一个名为计划+ INestedInterface它不会找到的接口。

This poses a problem for nested types as they will have a name which is "ContainingTypeName+NestedTypeName". So when you pass in the full name to GetInterface it actually ends up looking for an interface named "Program+INestedInterface" which it won't find.

这篇关于Type.GetInterface和嵌套类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆