471,325 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Runtime Object creation

Tim
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?
Dec 14 '05 #1
13 3931
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
FF**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 14 '05 #2
use Activator.CreateInstance()

"Tim" wrote:
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?

Dec 14 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
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?

Dec 14 '05 #4
Tim
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?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
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?


Dec 15 '05 #5
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
AB**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 15 '05 #6
Tim
Hi Joanna,

Can you give me a sample of how to create a class factory?
"Joanna Carter [TeamB]" wrote:
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
AB**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 15 '05 #7
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
92**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 15 '05 #8
Hi,

I agree with Joanna, but beware, you can being creating another layer of
code, if you do not know where to get your classes from until runtime (they
reside in a dll that you load manually ) you are just doing the same, for a
sample take a look at http://en.wikipedia.org/wiki/Factory_method_pattern

Also take a look at abstract factory :
http://en.wikipedia.org/wiki/Abstract_factory_pattern
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Hi Joanna,

Can you give me a sample of how to create a class factory?
"Joanna Carter [TeamB]" wrote:
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
AB**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 15 '05 #9
Tim
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?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

I agree with Joanna, but beware, you can being creating another layer of
code, if you do not know where to get your classes from until runtime (they
reside in a dll that you load manually ) you are just doing the same, for a
sample take a look at http://en.wikipedia.org/wiki/Factory_method_pattern

Also take a look at abstract factory :
http://en.wikipedia.org/wiki/Abstract_factory_pattern
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Hi Joanna,

Can you give me a sample of how to create a class factory?
"Joanna Carter [TeamB]" wrote:
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
AB**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


Dec 16 '05 #10
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
A1**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 16 '05 #11
Tim
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.


"Joanna Carter [TeamB]" wrote:
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
A1**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 16 '05 #12
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
22**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 16 '05 #13
Tim
Thanks Joanne. That was very informative and helpful.

"Joanna Carter [TeamB]" wrote:
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
22**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 19 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Steven T. Hatton | last post: by
8 posts views Thread by Steve Neill | last post: by
9 posts views Thread by Charisma | last post: by
4 posts views Thread by murali pannala via .NET 247 | last post: by
13 posts views Thread by DaTurk | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.