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(str ing 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. 2 2133
Just D,
I would look into System.Reflecti on.Assembly.Cre ateInstance.
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(str ing 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.
System.Reflecti on.Assembly.Cre ateInstance 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.Create Instance.
There are other ways to create an object given its type, you can use
Assembly.GetTyp e() which return you a Type object, from this Type object you
can obtain a constructor by using Type.GetConstru ctor, you can also use the
Type object to call any method after the instance is created.
Check out the System.Reflecti on namespace, you will find tons of stuff
there.
--
Francisco Padron www.chartfx.com
"Ye" <Ye@discussions .microsoft.com> wrote in message
news:F6******** *************** ***********@mic rosoft.com... Just D,
I would look into System.Reflecti on.Assembly.Cre ateInstance.
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(str ing 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 using reflection since they are
basically numeric data. But for structures (or for that matter classes), I
am not sure how to create a new structure at runtime by just knowing its
System.Type and assign random values to its members. Any help would...
|
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
|
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 based on the format specifier
in the Format field. The problem is, since the Value field is a string, it
doesnt properly "format".
So I need to convert to an object of the type specified by "Type"...ok, so
Activator.CreateInstance() BUT the...
|
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?
Thanks very much in advance,
Daniel :)
|
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){}
I then determine what object I should call the Persist method on using a
switch statement (not very OO).
switch (otype)
| |
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 single Object by itself, I'd like to
assign a reference to the class type and call it later without knowing the
derived type.
Example:
|
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 generic interface :
*note : in reality I implement the IComparable and IEquatable generic
interfaces and associated overriden methods, but I've cut everything
down to the bare minimum for this example.
|
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
|
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'
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |