How can you cast to a type using the type name as a string?

后端 未结 4 1049
悲&欢浪女
悲&欢浪女 2021-01-12 12:00

Ok, I\'ve thumped on this idea all day now, and I have reached the part where I admit I just flat out don\'t know. It\'s possible that what I\'m doing is just stupid and the

4条回答
  •  醉梦人生
    2021-01-12 12:24

    You'd be better off with the other overload that takes a Type and using e.g. Type.GetType(string).

    FormWithWorker formToLoad = (FormWithWorker)fi.GetValue(this);
    if (formToLoad == null)
    {
        formToLoad =
          (FormWithWorker)System.Activator.CreateInstance(Type.GetType("MyNamespace.MyFormType"));
    }
    

提交回复
热议问题