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

Create an instance from type as string

P: n/a
Hi all,

Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have
a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will
be loaded.

Obviously i know this createinstance method takes a System.Type parameter,
not a string representation.

any ideas?

TIA

Sam Martin
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
boxim <sa*********@yahoo.co.uk> wrote:
Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have
a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will
be loaded.

Obviously i know this createinstance method takes a System.Type parameter,
not a string representation.


No it doesn't - or rather, not all overloads of it do. There's a perfectly
usable Activator.CreateInstance(string, string) where you give the
assembly name and the type name.

Alternatively, use Type.GetType to get the type if it's in either
mscorlib or your own assembly. Otherwise, load the relevant assembly
and then use Assembly.GetType.

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

P: n/a
ok, thanks

got it working, now having to determine the constructor to use (if no
paramterless one available), and pass corrent values, etc.

however....

how do i compare to types

(Runtime Type) System.Char* and System.String?

Maybe if you can answer why if can't do this it would help....

object myobj =
System.Activator.CreateInstance(Type.GetType("Syst em.String"),new object[]
{"string value"});

It's throwing a missing method exception, I'm guessing because there is no
contructor that takes a string - only takes a System.Char*.
There is no default (parameterless contructor) .

To use System.Char* I'd have to unsafe it, but surely there's a way of doing
it passing a string?

TIA

Sam Martin
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
boxim <sa*********@yahoo.co.uk> wrote:
Real easy one I think, just can't find example,

I need to create an instance of a type, but i don't have a type var, i have a string
e.g.
string t = "MyNamespace.Class1";

I wan't to do something like
System.Activator.CreateInstance(t); // i know the relevant assemblies will be loaded.

Obviously i know this createinstance method takes a System.Type parameter, not a string representation.


No it doesn't - or rather, not all overloads of it do. There's a perfectly
usable Activator.CreateInstance(string, string) where you give the
assembly name and the type name.

Alternatively, use Type.GetType to get the type if it's in either
mscorlib or your own assembly. Otherwise, load the relevant assembly
and then use Assembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3

P: n/a
boxim <sa*********@yahoo.co.uk> wrote:
got it working, now having to determine the constructor to use (if no
paramterless one available), and pass corrent values, etc.

however....

how do i compare to types

(Runtime Type) System.Char* and System.String?
They're not the same, basically.
Maybe if you can answer why if can't do this it would help....

object myobj =
System.Activator.CreateInstance(Type.GetType("Syst em.String"),new object[]
{"string value"});
You can't do that because there's no constructor for String which takes
another String.
It's throwing a missing method exception, I'm guessing because there is no
contructor that takes a string - only takes a System.Char*.
Yup. And char[], amongst others.
There is no default (parameterless contructor) .

To use System.Char* I'd have to unsafe it, but surely there's a way of doing
it passing a string?


If you've already *got* the string value, why do you want to create
another string with the same data? Just use the original string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.