How to determine if a object type is a built in system type

前端 未结 8 972
醉酒成梦
醉酒成梦 2020-12-14 00:20

I am writing a simple List to CSV converter. My converter checks the all the t\'s in List and grabs all public properties and places them

相关标签:
8条回答
  • 2020-12-14 01:01

    Namespace-based methods potentially may cause collisions.

    There is an another reliable and simple way:

    static bool IsSystemType(this Type type) => type.Assembly == typeof(object).Assembly;
    

    Or a little bit more optimal, caching the system assembly:

    static readonly Assembly SystemAssembly = typeof(object).Assembly;
    
    static bool IsSystemType(this Type type) => type.Assembly == SystemAssembly;
    
    0 讨论(0)
  • 2020-12-14 01:12

    I prefer

    colType.FullName.StartsWith("System")
    

    rather then

    colType.Namespace.StartsWith("System")
    

    becose Namespace may be null.

    0 讨论(0)
提交回复
热议问题