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);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)