Determine if a type is static

前端 未结 5 766
梦谈多话
梦谈多话 2020-11-29 08:49

Let\'s say I have a Type called type.

I want to determine if I can do this with my type (without actually doing this to each type):

5条回答
  •  心在旅途
    2020-11-29 09:29

    static classes are declared abstract and sealed at the IL level. So, you can check IsAbstract property to handle both abstract classes and static classes in one go (for your use case).

    However, abstract classes are not the only types you can't instantiate directly. You should check for things like interfaces (without the CoClass attribute) and types that don't have a constructor accessible by the calling code.

提交回复
热议问题