How do i use Activator.CreateInstance with strings?

前端 未结 5 1970
礼貌的吻别
礼貌的吻别 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:27

    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?

提交回复
热议问题