Passing Type as a parameter

Here’s a useful bit of C# code for creating a method that takes a Type

as a parameter from the microsoft.public.dotnet.languages.csharp group.

Calling code:

MyComponent component;object myobject;MyMethod(myobject, typeof(MyComponent));

Method:

public static void MyMethod(object myobject, Type type) { if(myobject.GetType() == type) { // do something }}

Useful references:

MSDN typeof documentation

MSDN using typeof reference

Posted on 27 Oct 04 by Helen Emerson (last updated on 27 Oct 04).
Filed under .NET