Runtime Object creation

G

Guest

When the Application is started, it has to read a config file. The config
file gives details of the objects that need to be created. Based on the info
from the config file, the objects need to be instantiated at run time and its
methods have to be invoked. How do I do this?

Is reflection the only way to do it? Is there any other way?
 
J

Joanna Carter [TeamB]

"Tim" <[email protected]> a écrit dans le message de (e-mail address removed)...

| When the Application is started, it has to read a config file. The config
| file gives details of the objects that need to be created. Based on the
info
| from the config file, the objects need to be instantiated at run time and
its
| methods have to be invoked. How do I do this?
|
| Is reflection the only way to do it? Is there any other way?

Look at the Class Factory design pattern.

Joanna
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi

Do they provide a common interface?

If so you can create them using several ways depending if you know the
assembly, if it's loaded and several others criterias, you can use one of
these methods:

AppDomain.CreateInstance
Assembly.CreateInstance
Activator.CreateInstance


cheers,
 
G

Guest

Ignacio,

Thank you for the response.

All objects share a common interface. According to your suggestion I
can use Activator.CreateInstance or Reflection. But, Performance is an
important aspect for my application. Can you tell me, Performance-wise, which
option would be better - Activator.CreateInstance or using Reflection to
create the types?
 
J

Joanna Carter [TeamB]

"Tim" <[email protected]> a écrit dans le message de (e-mail address removed)...

| All objects share a common interface. According to your suggestion I
| can use Activator.CreateInstance or Reflection. But, Performance is an
| important aspect for my application. Can you tell me, Performance-wise,
which
| option would be better - Activator.CreateInstance or using Reflection to
| create the types?

A Class Factory is more efficient because it doesn't use either reflection
or the Activator class.

Joanna
 
J

Joanna Carter [TeamB]

"Tim" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Can you give me a sample of how to create a class factory?

public abstract class MyBaseClass
{
}

public class MyDerivedClassA : MyBaseClass
{
}

public class MyDerivedClassB : MyBaseClass
{
}


public static class MyClassFactory
{
public MyBaseClass Create(string className)
{
switch (className)
{
case "ClassA" :
MyDerivedClassA result = new MyDerivedClassA(...)
... // call setup properties and methods
return result;
case "ClassB" :
MyDerivedClassB result = new MyDerivedClassB(...)
... // call setup properties and methods
return result;
...
}
}

I'm sorry it's not complete, but I have to rush out now; I hope you get the
idea :))

Joanna
 
I

Ignacio Machin \( .NET/ C# MVP \)

G

Guest

Hi,

Thanks for the sample Joanna and the links - Ignacio . From the sample,
it appears that I have to add a switch statement for every new type of object
that I would like to create.

But, according to the requirements of the project, I shouldn't have to
make any code change in order to create a new type of object. The client or
anybody will just add the assembly and type details in the config file. I
wouldn't even know what kind of object would be created runtime.

Is it still possible to use the factory pattern?
 
J

Joanna Carter [TeamB]

"Tim" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Thanks for the sample Joanna and the links - Ignacio . From the sample,
| it appears that I have to add a switch statement for every new type of
object
| that I would like to create.
|
| But, according to the requirements of the project, I shouldn't have to
| make any code change in order to create a new type of object. The client
or
| anybody will just add the assembly and type details in the config file.
I
| wouldn't even know what kind of object would be created runtime.
|
| Is it still possible to use the factory pattern?

Yes, using reflection you certainly can create an extendable factory by
changing the factory method's signature like this :

public static class MyClassFactory
{
public static BaseType Create(Type classType)
{
if (! typeof(BaseType).IsAssignableFrom(classType))
throw new Exception("Can't create instance of this type");
return (BaseType) Activator.CreateInstance(classType);
}
}

As you can see, you would have to ensure that all the anticipated types
would have to have the same constructor signature (in this example a
default, noargs constructor).

As to a completely open Class Factory, I don't think that this is a good
idea as you would have to cater for possible differing constructor
parameters and, to cater for types that don't inherit from a fixed type
(apart from Object), you would have to resort to determining what parameters
were required by the constructor and you would have to return an Object, so
you might as well just use the Activator.

Are you really saying that you want a means of creating *any* type or just
those that derive from a particular base type ?

Joanna
 
G

Guest

Hi Joanne,

Thank you for the sample code detailing Class factory. Regarding my
requirement, I need to create types that implement a specific interface, but
are not necessarily types that derive from a particular base type.
 
J

Joanna Carter [TeamB]

"Tim" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Thank you for the sample code detailing Class factory. Regarding my
| requirement, I need to create types that implement a specific interface,
but
| are not necessarily types that derive from a particular base type.

Then you would need to change the factory method like this :

public static class MyClassFactory
{
public static IMyInterface Create(Type classType)
{
if (! typeof(IMyInterface).IsAssignableFrom(classType))
throw new Exception("Can't create instance of this type");
return (IMyInterface) Activator.CreateInstance(classType);
}
}

But the same provision that all classes would have to implement the same
constructor signature would still apply.

Joanna
 

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