是的,你需要同时测试IsAbstract
和IsSealed
。非静态类永远不能同时具备这两个特性。虽然不太理想,但它能正常工作。
在IL级别上,任何静态类都是抽象和封闭的。因此,您可以像这样操作:
Type myType = typeof(Form1);
if (myType.GetConstructor(Type.EmptyTypes) == null && myType.IsAbstract && myType.IsSealed)
{
// class is static
}
if (typeof(C).Attributes.HasFlag(System.Reflection.TypeAttributes.Abstract) &&
typeof(C).Attributes.HasFlag(System.Reflection.TypeAttributes.Sealed) &&
typeof(C).Attributes.HasFlag(System.Reflection.TypeAttributes.Class) )
{
}
但是可能存在一个具有这些属性的类,但它不是静态的。