Possible to get type of an aliased type in c#?

和自甴很熟 提交于 2019-12-28 06:46:38

问题


Type.GetType("System.String")

Is there a lookup for the aliases available somewhere?

Type.GetType("string")

returns null.


回答1:


This is not possible programmatically, since the 'aliases' are in fact keywords introduced in C#, and Type.GetType (like every other framework method) is part of the language-independent framework.

You could create a dictionary with the following values:

    bool      System.Boolean
    byte      System.Byte
    sbyte     System.SByte
    char      System.Char
    decimal   System.Decimal
    double    System.Double
    float     System.Single
    int       System.Int32
    uint      System.UInt32
    long      System.Int64
    ulong     System.UInt64
    object    System.Object
    short     System.Int16
    ushort    System.UInt16
    string    System.String



回答2:


The "aliases" are part of the language definition. You need to look them up in the language spec in question. They are compiled away, and don't exist at runtime - string becomes System.String, int becomes System.Int32, etc.




回答3:


Aliases (int, bool etc.) are not defined in CLS. They're just compile-time constants, that are replaced at runtime. This means that by the time your program is running, and you can do things "programmatically, aliases are already gone. You can only do something like this:

Type.GetType(typeof (string).ToString())


来源:https://stackoverflow.com/questions/320948/possible-to-get-type-of-an-aliased-type-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!