How to decide a Type is a custom struct?

前端 未结 6 1409
谎友^
谎友^ 2020-12-18 20:14

For a Type, there is a property IsClass in C#, but how to decide a Type is a struct?

Although IsValueType is a ne

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 21:11

    Not a perfect solution, but you can always consider narrowing the search by known types within the assemblies you wish to search:

    System.Reflection.Assembly.GetAssembly(tyepof(OneOfMyTypes))
        .GetExportedTypes()
        .Where(t => t.IsValueType);
    

    The helps eliminate false positives (safer?), but it's less portable.

提交回复
热议问题