Create class from name

  • Thread starter Peter Larsen [CPH]
  • Start date
P

Peter Larsen [CPH]

Hi,

I can't remember how to write the code (in designtime) that constructs a
"unknown" class. I only have its base class - in designtime - not the exact
class.

The following code shows a sample of what i want to do:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

C1 c2 = CreateClass("C2");
C1 c3 = CreateClass("C3");
}

private C1 CreateClass(string classname)
{

return CreateTheClass(classname);
}
}

class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

Please tell me how the method CreateClass() should look like.
Thank you in advance.

BR
Peter
 
J

Jeroen Mostert

Peter said:
I can't remember how to write the code (in designtime) that constructs a
"unknown" class. I only have its base class - in designtime - not the exact
class.
Activator.CreateInstance() should serve (almost) all your needs for dynamic
creation.
 
I

Ilya Albrekht

C1 c1 = new C1();

Hi,

I can't remember how to write the code (in designtime) that constructs a
"unknown" class. I only have its base class - in designtime - not the
exact
class.

The following code shows a sample of what i want to do:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

C1 c2 = CreateClass("C2");
C1 c3 = CreateClass("C3");
}

private C1 CreateClass(string classname)
{

return CreateTheClass(classname);
}
}

class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

Please tell me how the method CreateClass() should look like.
Thank you in advance.

BR
Peter
 
I

Ilya Albrekht

Or you can use Activator.CreateInstance

Hi,

I can't remember how to write the code (in designtime) that constructs a
"unknown" class. I only have its base class - in designtime - not the
exact
class.

The following code shows a sample of what i want to do:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

C1 c2 = CreateClass("C2");
C1 c3 = CreateClass("C3");
}

private C1 CreateClass(string classname)
{

return CreateTheClass(classname);
}
}

class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

Please tell me how the method CreateClass() should look like.
Thank you in advance.

BR
Peter
 
L

Linda Liu[MSFT]

Thanks Ilya and Jeroen for your replies!

Hi Peter,

As Ilya and Jeroen have suggested, a solution to create an instance from a
given class name is to call Activator.CreateInstance method. For example:

namespace WindowsApplication1
{
class C1
{ }
class C2 : C1
{ }
class C3 : C1
{ }

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// pass the full name of the class
C1 c2 = CreateClass("WindowsApplication1.C2");
C1 c3 = CreateClass("WindowsApplication1.C3");
}

private C1 CreateClass(string classname)
{
ObjectHandle objh = Activator.CreateInstance(null, classname);
C1 obj = objh.Unwrap() as C1;
return obj;
}
}

Alternatively, you can get the Type from the given class name first and
then call the TypeDescriptor.CreateInstance method to create an instance of
the type. For example:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// pass the full name of the class
C1 c2 = CreateClass("WindowsApplication1.C2");
C1 c3 = CreateClass("WindowsApplication1.C3");
}

private C1 CreateClass(string classname)
{
C1 obj = null;
Type t = Type.GetType(classname, false);
if (t != null)
{
obj = TypeDescriptor.CreateInstance(null, t, null, null) as
C1;
}
return obj;
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Larsen [CPH]

Thank you all for your comments and thank you to you, Linda, for your great
samples.
You did it again :)

BR
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top