How do I use Activator.CreateInstance with strings?

Keep in mind that the string class is immutable. It cannot be changed after it is created. That explains why it doesn't have a parameterless constructor, it could never generate a useful string object other than an empty string.

That's already available in the C# language, it is Same reasoning applies for a string(String) constructor. There is no point in duplicating a string, the string you'd pass to the constructor is already a perfectly good instance of the string So fix your problem by testing for the string case: var oType = oVal.GetType(); if (oType == typeof(string)) return oVal as string; else return Activator. CreateInstance(oType, oVal).

Keep in mind that the string class is immutable. It cannot be changed after it is created. That explains why it doesn't have a parameterless constructor, it could never generate a useful string object other than an empty string.

That's already available in the C# language, it is "". Same reasoning applies for a string(String) constructor. There is no point in duplicating a string, the string you'd pass to the constructor is already a perfectly good instance of the string.So fix your problem by testing for the string case: var oType = oVal.GetType(); if (oType == typeof(string)) return oVal as string; else return Activator.

CreateInstance(oType, oVal).

I understand now. I was hoping not to do an if statement or any special checks. It looks like I must.

– acidzombie24 Jan 19 '10 at 20:09.

It looks like you're trying to call a constructor which just takes a string - and there isn't such a constructor. If you've already got a string, why are you trying to create a new one? (When you didn't provide any further arguments, you were trying to call a parameterless constructor - which again, doesn't exist.) Note that typeof(string) is a simpler way to get a reference to the string type.

Could you give us more information about the bigger picture of what you're trying to do?

I am reading objects via reflection and sqlitedatareader. I don't know if oval is a string unless I write an if statement which I wanted to avoid. Also I am using fieldtype and I may be creating an object that takes in a string.

Nobugz mention strings are immutable so I guess I must write an if statement to check if fieldtype is a string or not. – acidzombie24 Jan 19 '10 at 20:12 1 Compared with the other hackery involved in reconstructing objects from strings, I think a simple "is this already a string" check is likely to be the least of your worries :) – Jon Skeet Jan 19 '10 at 20:49.

You are trying to do this : var sz = new string(); Try to compile it, you will understand your error. You may try : var sz = Activator. CreateInstance(typeof(string), new object {"value".ToCharArray()}); But it looks useless, you should directly use value...

Actually he is trying var s = new string("Test") – leppie Jan 19 '10 at 9:55 I was talking about this line : var sz = Activator. CreateInstance("".GetType()); – Guillaume Jan 19 '10 at 9:57.

String actually has no constructor that takes a string as input. There is a constructor that takes a char array so this should work: var sz = Activator. CreateInstance ("".

GetType (), "Test". ToCharArray ()).

Var sz = Activator. Var sz = Activator. Additional information: No parameterless constructor defined for this object.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions