Instantiating an object dynamically in C#?

D

DotNetJunkies User

Hi,

Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very handy to be able to instantiate them based on a string variable representing their class name.

Here's an example of what I'd be looking to do.

public object DynamicInstantiation(string className)
{
return new [dynamic intantiation sytax here using className varibable value];
}

I rember doing something like this once in a Java class long ago and I figured that since C# has so many similarities that there must be a way but I've not been able to find a working example in any MS documentation.

Any help would be much appreciated!
 
B

Bruce Wood

What you may be looking for is Reflection, which is the catch-all name
for a set of classes and methods in C# that allow you to examine the
class hierarchy and get information about methods, properties, and
other things. Take a look at documentation for: Type, MethodInfo,
PropertyInfo, ConstructorInfo, among others.

However, it sounds to me as though what you want to produce is a
Factory. Do some research on the Factory Method and Abstract Factory
design patterns, made famous by Gamma, Helm Johnson, and Vlissides in
their book "Design Patterns". (There are also plenty of on-line
references, including ones that give code examples in C#.)
 
J

Jeff Louie

You can dynamically load a plug in that implements an interface without
knowing the name of the class.
http://www.geocities.com/jeff_louie/OOP/oop13.htm

You can dynamically instantiate an instance of a class that is loaded in
an assembly if you know the fully qualified class name. Here is a few
snippets of code that I wrote today.

namespace TestClassFactory
{
interface IDrawable
{
void DrawYourself();
}
interface IConstructDrawable
{
string Name();
IDrawable Instantiate();
}
class DrawableConstructor : IConstructDrawable
{
private Type t;
private string name="Drawable";
public string Name()
{
return name;
}
public DrawableConstructor(Type t, string name)
{
this.t= t;
this.name= name;
}
public IDrawable Instantiate()
{
if (typeof(IDrawable).IsAssignableFrom(t)) // safer
{
// dynamically load this class
return (IDrawable)Activator.CreateInstance(t);
}
else return null;
}
}
Main stuff...
Module[] moduleArray;
moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
//In a simple project with only one module, the module at index
// 0 will be the module containing this class.
Module myModule = moduleArray[0];
Type myType;
myType = myModule.GetType("TestClassFactory.Triangle", false, false);
factory.Add(new DrawableConstructor(myType,myType.Name));
}


Regards,
Jeff
Does anyone know how/if you can instantiate a C# reference type object
dynamically? More specifically, my project has a number of classes that
I've created and in some cases it would be very handy to be able to
instantiate them based on a string variable representing their class
name.<
 
M

Marcin Hoppe

DotNetJunkies User napisa³(a):
Here's an example of what I'd be looking to do.

public object DynamicInstantiation(string className)
{
return new [dynamic intantiation sytax here using className varibable value];
}

Any help would be much appreciated!

I am surprise that System.Activator class wasn't mentioned yet. It can
do exactly what you want:

using System;

public class A
{

}

public class B
{

}

public class App
{
public static void Main()
{
object a = Activator.CreateInstance(null, "A").Unwrap();
object b = Activator.CreateInstance(null, "B").Unwrap();

Console.WriteLine(a is A);
Console.WriteLine(b is B);
}
}

Regards,
Marcin
 

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