Cannot CreateInstance of a string

G

Guest

Hello, I want to dynamically create an object based on it's type. The core if
it sould be to use Activator.CreateInstance. This works fin for its,
floats..., but not for strings. Here's a sample:
string s;
s= "1020";
Type t = s.GetType();
object o = Activator.CreateInstance(t);
An exceptin is thrown saying: "No parameterless constructor defined for this
object.".
Please help!
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello,

If you look at the list of overloaded constructors for System.String, you'll
notice that there is indeed no parameterless constructor.
You have to use another overload of CreateInstance allowing you to pass
argument values to the System.String constructor.
 
G

Guest

Which one? I did not Find anything I can use to create an instance of sting :-(
There seems to be only a private constructor called ".cctor" (which I found
out by calling GetConstructors, yet I don't know how to invoke this.
Any help your be apprechiated.!!

Dmitriy Lapshin said:
Hello,

If you look at the list of overloaded constructors for System.String, you'll
notice that there is indeed no parameterless constructor.
You have to use another overload of CreateInstance allowing you to pass
argument values to the System.String constructor.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

mra said:
Hello, I want to dynamically create an object based on it's type. The core
if
it sould be to use Activator.CreateInstance. This works fin for its,
floats..., but not for strings. Here's a sample:
string s;
s= "1020";
Type t = s.GetType();
object o = Activator.CreateInstance(t);
An exceptin is thrown saying: "No parameterless constructor defined for
this
object.".
Please help!
 
D

Dmitriy Lapshin [C# / .NET MVP]

Even skipping unsafe ones:

public String(char[]);
public String(char, int);
public String(char[], int, int);

Try something like this:

char[] c = new char[] {'a', 'b', 'c'};
string s= "1020";
Type t = s.GetType();
object o = Activator.CreateInstance(t, new object[] {c});

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

mra said:
Which one? I did not Find anything I can use to create an instance of
sting :-(
There seems to be only a private constructor called ".cctor" (which I
found
out by calling GetConstructors, yet I don't know how to invoke this.
Any help your be apprechiated.!!

Dmitriy Lapshin said:
Hello,

If you look at the list of overloaded constructors for System.String,
you'll
notice that there is indeed no parameterless constructor.
You have to use another overload of CreateInstance allowing you to pass
argument values to the System.String constructor.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

mra said:
Hello, I want to dynamically create an object based on it's type. The
core
if
it sould be to use Activator.CreateInstance. This works fin for its,
floats..., but not for strings. Here's a sample:
string s;
s= "1020";
Type t = s.GetType();
object o = Activator.CreateInstance(t);
An exceptin is thrown saying: "No parameterless constructor defined for
this
object.".
Please help!
 
W

Willy Denoyette [MVP]

mra said:
Which one? I did not Find anything I can use to create an instance of
sting :-(
There seems to be only a private constructor called ".cctor" (which I
found
out by calling GetConstructors, yet I don't know how to invoke this.
Any help your be apprechiated.!!


string s = "1020";
Type t = s.GetType();
object o = Activator.CreateInstance(t, s.ToCharArray());

or:
object o = Activator.CreateInstance(t, new char[]{ 'a','b', 'c', 'd',
'e', 'f' });
or:
object o = Activator.CreateInstance(t, new object[]{ 'a',20 });
....

Willy.
 

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