Passing a "class" reference instead of an "object"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to be able to register a class that a factory can build for me at a
later time.

How can I pass the class's type to a method?

public class MyClass{
static public void Register(Type type)
{
// do something with: type
}
}

....
MyClass.Register(System.Windows.Forms.Form);
....

I would get the error: "X" is a 'type' but is used like a 'variable'.
 
Hi LeiFord,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to pass a type object to a
static method. If there is any misunderstanding, please feel free to let me
know.

In C#, you need to use typeof() keywork to get the type of that class. For
example:

MyClass.Register(typeof(System.Windows.Forms.Form));

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top