473,373 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 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 3995
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven T. Hatton | last post by:
As I was reading through some of the stuff in the Standard, I started to wonder if there might be reason to define classes in response to runtime conditions, say, to match some kind of new data...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
9
by: Charisma | last post by:
I have a client who is requesting the following: Description: -database should be a runtime version of MS Access so that it would not be necessary for the user to have any Microsoft products in...
4
by: murali pannala via .NET 247 | last post by:
(Type your message here) -------------------------------- From: murali pannala i would want to create an object dynamically. i.e i hold my class name in a variable only.and by passing this...
10
by: Lauren Wilson | last post by:
I have a desperate short term need for a way to install Access 2003 runtime on client computers. I have the proper license to do so but I cannot seem to find the files to do it like we did with...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
4
by: ThunderMusic | last post by:
Hi, I have a custom form that works fine when I debug it or run it in release mode but cannot be loaded in the designer... Actually, it can be loaded in the designer when no control is on it, but...
0
by: qazwsx | last post by:
Hey, Ive got this list of transactions, all having an ID and a number of Items. I have a transaction class... how can i create a new "Transaction Object" at runtime, but also assign the object...
0
by: rhyspatto | last post by:
Hi, I have a problem but cannot figure out a good solution to it. I am trying to create a form that creates a bunch of panels during runtime, and I want to be able to send commands (such as false...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.