How do i use Activator.CreateInstance with strings?

前端 未结 5 2022
礼貌的吻别
礼貌的吻别 2021-02-05 01:48

In my reflection code i hit a problem with my generic section of code. Specifically when i use a string.

var oVal = (object)\"Test\";
var oType = oVal.GetType();         


        
5条回答
  •  长发绾君心
    2021-02-05 02:31

    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...

提交回复
热议问题