Hi, given a name of an class e.g. "MyClass" which is stored as a string
is it possible to create a new instance of this type dynamically and
then reference the attributes by name in code??
e.g.
If my class is called Person and it has attributes FirstName and
LastName is it possible to do something like this;
public void GeneratePerson( )
{
string className = "Person";
(CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
_person.FirstNa me = "fred";
_person.LastNam e = "smith";
_person.AddName ();
}
I did look at using reflection but can't seem to create an instance of
an object just given a string and then populate the object with
attrbutes.
Any help appreciated
Thanks
Markus
=============== ========
googlenews2006m arkusj 3 1906
<Ma*******@gmai l.comwrote:
Hi, given a name of an class e.g. "MyClass" which is stored as a string
is it possible to create a new instance of this type dynamically and
then reference the attributes by name in code??
e.g.
If my class is called Person and it has attributes FirstName and
LastName is it possible to do something like this;
public void GeneratePerson( )
{
string className = "Person";
(CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
_person.FirstNa me = "fred";
_person.LastNam e = "smith";
_person.AddName ();
}
I did look at using reflection but can't seem to create an instance of
an object just given a string and then populate the object with
attrbutes.
You want Activator.Creat eInstance to create the instance - or use
Type.GetType to get the type, and then fetch and invoke a constructor.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks Jon
Regards
Markus
Jon wrote:
<Ma*******@gmai l.comwrote:
Hi, given a name of an class e.g. "MyClass" which is stored as a string
is it possible to create a new instance of this type dynamically and
then reference the attributes by name in code??
e.g.
If my class is called Person and it has attributes FirstName and
LastName is it possible to do something like this;
public void GeneratePerson( )
{
string className = "Person";
(CreateInstance OfPerson) _person = new (CreateInstance OfPerson);
_person.FirstNa me = "fred";
_person.LastNam e = "smith";
_person.AddName ();
}
I did look at using reflection but can't seem to create an instance of
an object just given a string and then populate the object with
attrbutes.
You want Activator.Creat eInstance to create the instance - or use
Type.GetType to get the type, and then fetch and invoke a constructor.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
You also won't be able to get this kind of strong typing in your code:
_person.FirstNa me = "fred";
Objects created using reflection can have their properties and methods
referenced through a collection however. You can once again use the
string name of the field in order to find it. This code should be
pretty well documented in MSDN.
However, if at design time you know for sure the names of the
properties and methods (but not the class name), you might be able to
extract your common class aspects into an interface, then cast the
dynamically created object as an interface instance, and then use the
strongly typed names.
Hope this makes sense.
Steven This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: markoueis |
last post by:
Our .NET application, let's call it A, needs to load an assembly of
another .NET application, let's call it B. Preferably I would like to
early bind and early load the B assembly. However, in a deployment
scenario, it is possible that B is not installed, in which case, the
dependencies for its assembly won't be there. When I try to load that
assembly (which is copied) regardless, my application crashes saying
that its dependencies cannot...
|
by: Simon Verona |
last post by:
I would normally use code such as :
Dim Customer as new Customer
Dim t as new threading.thread(AddressOf Customer.DisplayCustomer)
Customer.CustomerId=MyCustomerId
t.start
Which would create a new thread to display a customer on the screen for example.
However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :
|
by: pmclinn |
last post by:
I was wondering if it is possible to dynamically create a structure.
Something like this:
public sub main
sql = "Select Col1, Col2 from Table a"
dim al as new arraylist
al = LoadOracleData(sql)
'____Do amazing things
|
by: RSS |
last post by:
Hi everyone,
I have an app that uses some DLL's that are shared across couple applications. There are reasons existing that prevent me from putting them in to the GAC.
Each of these app's at some point in time loads a DLL dynamically in this fashion:
System.Reflection.Assembly assembly = null;
assembly = System.Reflection.Assembly.LoadFrom(_assemblyPath);
return assembly.CreateInstance(_typeName, false,...
|
by: |
last post by:
I have made some user controls with custom properties. I can set those
properties on instances of my user controls, and I have programmed my user
control to do useful visual things in response to how those properties are
set.
I want to be able to do two other things:
a) add User control instances to my page, filling in the place of
placeholder controls, and
b) programmatically setting custom properties on those dynamically spawned...
| |
by: Smithers |
last post by:
I have a Windows Forms application that implements a plug-in architecture
whereby required assemblies are identified and loaded dynamically.
Here are the relevant classes:
A = application = Windows Forms class
B = a singleton hosted within A. B is responsible for dynamically loading
classes X, Y, and Z.
|
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= |
last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's
C# Calling Code Dynamically, that calling a method dynamically through
Reflection was much slower than through a Delegate. My testing showed that
actually it was six times faster: 0.5 seconds for 100,000 iterations versus
3.1 seconds.
Can anyone explain why? Something in the way I coded it? I'd appreciate
any insights.
Here's the code (in a Windows Form...
|
by: =?Utf-8?B?QWJoaQ==?= |
last post by:
I am using Reflection to invoke methods dynamically. I have got a special
requirement where I need to pass a value to method by setting the custom
method attribute. As I cannot change the signature of method to pass a new
parameter, I am setting the custom attribute of a given method and then
accessing the attribute from method. Since attribute value is decided at
runtime I want to change the attribute of particular method at runtime....
|
by: =?Utf-8?B?U2hhd24=?= |
last post by:
Hi;
i just started research reflection and i'm wondering if i have an empty
class file can i use reflection to add member variables and attributes
dynamically and then instantiate the class?
What i would like to be able to do is start with and empty class, then
depending on the data provided to me by a config file, add the member
variables and attributes to the class temporarily. when the app is shutdown
all changes would be gone.
can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |