Create an instance from type as string

B

boxim

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
 
J

Jon Skeet [C# MVP]

boxim said:
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.
 
B

boxim

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("System.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
 
J

Jon Skeet [C# MVP]

boxim said:
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("System.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top