65.9K
CodeProject 正在变化。 阅读更多。
Home

获取对象唯一的类型

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.85/5 (6投票s)

2011年10月6日

CPOL

1分钟阅读

viewsIcon

25819

如何在 C# 和 VB 中检查对象的类型,忽略所有子类型。

上周,我遇到了类型和派生类型的问题。我想在单击 SomeButton 类型时执行一些代码。不幸的是,这段代码非常通用,基本上任何类型的对象都可以通过这段代码。结果是,当单击 SomeButton 时,这段代码不仅会执行,而且当单击任何 SomeButton 的派生类时也会执行。这时我才了解到 instance is Type (C#)、TypeOf instance Is Type (VB) 和 GetType 之间的微妙区别。以下是 C# 和 VB 中检查对象确切类型的代码。这是一个控制台应用程序,您可以简单地将完整的示例复制/粘贴到新的控制台应用程序项目中,并亲眼看到差异。C#
using System;

namespace TypeTipCSharp
{
	class Base { }
	class Derived : Base { }

	class Program
	{
		static void Main(string[] args)
		{
			Derived d = new Derived();
			Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.");

			if (d is Base)
			{ Console.WriteLine("The type of d is Base"); }
			else
			{ Console.WriteLine("The type of d is not Base"); }

			if (d.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of d is Base"); }
			else
			{ Console.WriteLine("The GetType of d is not Base"); }

			Base b = new Derived();
			Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine);

			if (b is Base)
			{ Console.WriteLine("The type of b is Base"); }
			else
			{ Console.WriteLine("The type of b is not Base"); }

			if (b.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of b is Base"); }
			else
			{ Console.WriteLine("The GetType of b is not Base"); }

			if (b is Derived)
			{ Console.WriteLine("The type of b is Derived"); }
			else
			{ Console.WriteLine("The type of b is not Derived"); }

			if (b.GetType() == typeof(Derived))
			{ Console.WriteLine("The GetType of b is Derived"); }
			else
			{ Console.WriteLine("The GetType of b is not Derived"); }

			Console.WriteLine("{0}Press a key to close.", Environment.NewLine);
			Console.ReadKey();

		}
	}
}
VB
Public Class Base
End Class

Public Class Derived
	Inherits Base
End Class

Module Module1

	Sub Main()
		Dim d As New Derived
		Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.")

		If TypeOf d Is Base Then
			Console.WriteLine("The type of d is Base")
		Else
			Console.WriteLine("The type of d is not Base")
		End If

		If d.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of d is Base")
		Else
			Console.WriteLine("The GetType of d is not Base")
		End If

		Dim b As Base = New Derived
		Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine)

		If TypeOf b Is Base Then
			Console.WriteLine("The type of b is Base")
		Else
			Console.WriteLine("The type of b is not Base")
		End If

		If b.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of b is Base")
		Else
			Console.WriteLine("The GetType of b is not Base")
		End If

		If TypeOf b Is Derived Then
			Console.WriteLine("The type of b is Derived")
		Else
			Console.WriteLine("The type of b is not Derived")
		End If

		If b.GetType = GetType(Derived) Then
			Console.WriteLine("The GetType of b is Derived")
		Else
			Console.WriteLine("The GetType of b is not Derived")
		End If

		Console.WriteLine("{0}Press a key to close.", Environment.NewLine)
		Console.ReadKey()
	End Sub

End Module
结果
Variable d is an instance of Derived, which inherits from Base.
The type of d is Base
The GetType of d is not Base

Variable b is of Type Base, but has an instance of Derived assigned to it.
The type of b is Base
The GetType of b is not Base
The type of b is Derived
The GetType of b is Derived
因此,GetType 总是会返回一个 Object 的确切类型,该类型可以与 Type 进行比较。只有当两个类型完全匹配时,它们才会真正匹配。正如您所看到的,DerivedBase 的一种类型,但 Derived.GetTypeBase 不匹配。instance is TypeTypeOf instance Is Type 只要实例是类型 Type 或任何派生类型,就会返回 True! 这种区别很微妙,但非常重要。:)
© . All rights reserved.