473,471 Members | 4,095 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create an object on the fly knowing just an object type

All,

Do we have a simple way to Create an object on the fly knowing just an
object type? The usual design-time way is to write a code something like
this:

CObjectType obj = new CObjectType();

That's simple. But to create any object knowing its object type on the fly
is looking like a problem. I'll try to explain the idea.

We have some base class with the method:

Deserialize(string sXML, ref obj, System.Type systemtype)
{
....
}

The idea is to create any object of required type, the type should be
defined through the interface, then to do some appropriate steps to get the
data from the XML string and assign all required properties of the created
object. The idea is simple, but realization... The worst scenario is to use
a switch by systemtype and create all required objects inside an appropriate
switch case. The method is terrible because it's not nice, looking terribly
and absolutely not flexible. But to do the method universal we need to
create a new object on the fly using something like:

systemtype obj = new systemtype();

where "systemtype" is the variable received as a method parameter.

The problem that writing this we're getting a non-workable code.

Any idea or solution? Maybe we could use a virtual method to do this thing
or some other trick?

Thanks,
Just D.
Nov 19 '05 #1
2 2111
Ye
Just D,

I would look into System.Reflection.Assembly.CreateInstance.

Hope this helps.
"Just D." wrote:
All,

Do we have a simple way to Create an object on the fly knowing just an
object type? The usual design-time way is to write a code something like
this:

CObjectType obj = new CObjectType();

That's simple. But to create any object knowing its object type on the fly
is looking like a problem. I'll try to explain the idea.

We have some base class with the method:

Deserialize(string sXML, ref obj, System.Type systemtype)
{
....
}

The idea is to create any object of required type, the type should be
defined through the interface, then to do some appropriate steps to get the
data from the XML string and assign all required properties of the created
object. The idea is simple, but realization... The worst scenario is to use
a switch by systemtype and create all required objects inside an appropriate
switch case. The method is terrible because it's not nice, looking terribly
and absolutely not flexible. But to do the method universal we need to
create a new object on the fly using something like:

systemtype obj = new systemtype();

where "systemtype" is the variable received as a method parameter.

The problem that writing this we're getting a non-workable code.

Any idea or solution? Maybe we could use a virtual method to do this thing
or some other trick?

Thanks,
Just D.

Nov 19 '05 #2
System.Reflection.Assembly.CreateInstance is one way, notice that you need
to know the assembly in which the object resides.

If your Deserialize method allows for external objects, you will need to
save the name of the assembly as well as the name of the type, you can then
use Assembly.Load to load the assembly after which you can use
Assembly.CreateInstance.

There are other ways to create an object given its type, you can use
Assembly.GetType() which return you a Type object, from this Type object you
can obtain a constructor by using Type.GetConstructor, you can also use the
Type object to call any method after the instance is created.

Check out the System.Reflection namespace, you will find tons of stuff
there.

--
Francisco Padron
www.chartfx.com
"Ye" <Ye@discussions.microsoft.com> wrote in message
news:F6**********************************@microsof t.com...
Just D,

I would look into System.Reflection.Assembly.CreateInstance.

Hope this helps.
"Just D." wrote:
All,

Do we have a simple way to Create an object on the fly knowing just an
object type? The usual design-time way is to write a code something like
this:

CObjectType obj = new CObjectType();

That's simple. But to create any object knowing its object type on the
fly
is looking like a problem. I'll try to explain the idea.

We have some base class with the method:

Deserialize(string sXML, ref obj, System.Type systemtype)
{
....
}

The idea is to create any object of required type, the type should be
defined through the interface, then to do some appropriate steps to get
the
data from the XML string and assign all required properties of the
created
object. The idea is simple, but realization... The worst scenario is to
use
a switch by systemtype and create all required objects inside an
appropriate
switch case. The method is terrible because it's not nice, looking
terribly
and absolutely not flexible. But to do the method universal we need to
create a new object on the fly using something like:

systemtype obj = new systemtype();

where "systemtype" is the variable received as a method parameter.

The problem that writing this we're getting a non-workable code.

Any idea or solution? Maybe we could use a virtual method to do this
thing
or some other trick?

Thanks,
Just D.

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Saranjit Singh | last post by:
Is there any way I can enumerate the properties of an object at runtime and assign values to each property of the object depending on its data type? For primitive data types, this is easily done...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
7
by: Dave Taylor | last post by:
I have a DataTable with three string columns, "Value", "Format" and "Type" However, some of the rows contain numbers and dates in the Value field. So I would like to be able to format the output...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
13
by: docschnipp | last post by:
Hi, I have a bunch of object derived from the same base class. They all share the same constructor with some parameters. Now, instead of using a large switch() statement where I call every...
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.