By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Dynamically creating an object (Reflection?)

P: n/a
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";
(CreateInstanceOfPerson) _person = new (CreateInstanceOfPerson);
_person.FirstName = "fred";
_person.LastName = "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
=======================
googlenews2006markusj

Oct 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<Ma*******@gmail.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";
(CreateInstanceOfPerson) _person = new (CreateInstanceOfPerson);
_person.FirstName = "fred";
_person.LastName = "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.CreateInstance to create the instance - or use
Type.GetType to get the type, and then fetch and invoke a constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 2 '06 #2

P: n/a
Thanks Jon
Regards
Markus
Jon wrote:
<Ma*******@gmail.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";
(CreateInstanceOfPerson) _person = new (CreateInstanceOfPerson);
_person.FirstName = "fred";
_person.LastName = "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.CreateInstance to create the instance - or use
Type.GetType to get the type, and then fetch and invoke a constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 8 '06 #3

P: n/a
You also won't be able to get this kind of strong typing in your code:

_person.FirstName = "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

Oct 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.