You can use the System.Activator class, or reflection to get the
particular constructor you want, then invoke that constructor.

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Henrik Enemark Rasmussen
Sent: Tuesday, May 07, 2002 9:21 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] HOWTO: Create object from type?

Hello all,

In C#, how can I create an object whoose type is not known until
run-time?
I need something similar to MFC's "RUNTIME_CLASS(CXxx).CreateObject()".

I expect it to look something like this, where the method will then
create an instance of the class and work with it.

public void ShowDialog(Type dialogClass)
{
   if (!dialogClass.IsSubclassOf(typeof(Dialog)))
      throw new ApplicationException("Invalid type");
   .
   .
   .
}

Regards
Henrik

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to