Cannot CreateInstance of a string

  • Thread starter Thread starter Guest
  • Start date Start date
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!
 
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.
 
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!
 
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!
 
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.
 
Back
Top