Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create objects at runtime when class is unknown at designtime 1

Status
Not open for further replies.

Dachyon

Programmer
Nov 4, 2002
32
0
0
NZ
Hi Gurus,
Using C#.Net VS2005.

I am wondering if there is a was to instantiate an object at runtime when the class of the object is not known at designtime?

I have a list of classes that all inherit from BaseClass, and override BaseClass.someMethod();

But I don't know which descendant classes will be required at runtime, and I want to to call the overridden method someMethod().

Other than having a switch statement that compares the classname string with a string constant :-

//create a class called "classname"
switch (classname)
{
case "DescendentClass1":
return new DescedentClass1();
break;
etc...
}

The problem with this is that if I want to add new classes, I need to recompile the code with the new class name.
The descendent class implemetations are likely to be in a DLL.

In Delphi I could Register(classname:String) and call GetClass(classname:String)

Can this be done in C#, or am I on the wrong track architecturally ?

Many thanks in advance.
 
This looks like a job for either an interface or registering of a baseclass.

You can say

public void RegisterClass(MyBaseClass c)
{
c.SomeMethod();
}

As long as whatever classes you are registering inherit from MyBaseClass, you can call the methods.
 
2 thoughts:
1. use the factory pattern to instanciate objects. You would have an interface, or abstract base class which all other classes relate to.
your factory could be a simple static class w/ static Create function. pass the parameteters to determine what type of object to create and send it back. example
Code:
public interface IMyClass
{
  ...properties and functions
}

public class myClass1 : IMyClass
{
 ...implement interface and other needed attributes
}
public class myClass2 : IMyClass
{
 ...implement interface and other needed attributes
}

public static class MyFactory
{
    public static IMyClassCreate(string myType)
    {
         switch(myType)
         {
             case "...":
                 return new myClass1();
             case "...":
                 return new myClass2();
             default:
                 return null;
         }
    }
}

//in your code
IMyClass myClass = MyFactory.Create("...");
....

2. The model you mention in the second half of your post relates to Direct Injection (DI) and Inversion of Control (IoC).
These are much more complicated frameworks. here are the main frameworks I have seen
Srping.Net []
Castle Windsor []
StructureMap []
MS Enterprise Library 3.0 []

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks all for your advice.
In the end, the factory pattern solved my problem.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top