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

前端 未结 8 979
醉酒成梦
醉酒成梦 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;
    

提交回复
热议问题