Convert “C# friendly type” name to actual type: “int” => typeof(int)

前端 未结 5 2111
旧时难觅i
旧时难觅i 2021-02-07 23:46

I want to get a System.Type given a string that specifies a (primitive) type\'s C# friendly name, basically the way the C# compiler d

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-08 00:32

    Alias names like 'int','bool' etc. are not part of .NET Framework. Internally they are converted into System.Int32, System.Boolean etc. Type.GetType("int") should return you null. Best way to appoach this is by having a dictionary to map alias with their type e.g.

            Dictionary PrimitiveTypes = new Dictionary();
            PrimitiveTypes.Add("int", typeof(int));
            PrimitiveTypes.Add("long", typeof(long));
            etc.etc..
    

提交回复
热议问题