C# 确定类型是否为静态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1175888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if a type is static
提问by Beaker
Let's say I have a Type
called type
.
假设我有一个Type
电话type
。
I want to determine if I can do this with my type (without actually doing this to each type):
我想确定我是否可以用我的类型做到这一点(实际上不对每种类型都这样做):
If type
is System.Windows.Point
then I could do this:
如果type
是,System.Windows.Point
那么我可以这样做:
Point point1 = new Point();
However if type
is System.Environment
then this will not fly:
但是,如果type
是System.Environment
那么这将不会飞:
Environment environment1 = new Environment(); //wrong
So if I am iterating through every visible type in an assembly how do I skip all the types that will fail to create an instance like the second one? I'm kind of new to reflection so I'm not that great with the terminology yet. Hopefully what I'm trying to do here is pretty clear.
因此,如果我遍历程序集中的每个可见类型,我该如何跳过所有无法创建像第二个实例这样的实例的类型?我对反射有点陌生,所以我对术语还不是很熟悉。希望我在这里要做的事情很清楚。
采纳答案by Mehrdad Afshari
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).
static
类被声明abstract
并sealed
处于 IL 级别。因此,您可以检查IsAbstract
属性以一次性处理abstract
类和static
类(针对您的用例)。
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.
但是,abstract
类并不是您不能直接实例化的唯一类型。您应该检查接口(没有CoClass
属性)和没有调用代码可访问的构造函数的类型。
回答by Paul van Brenk
This is a way to get all public contstuctors of all types in an assembly.
这是一种在程序集中获取所有类型的所有公共构造函数的方法。
var assembly = AppDomain.CurrentDomain.GetAssemblies()[0]; // first assembly for demo purposes
var types = assembly.GetTypes();
foreach (var type in types)
{
var constructors = type.GetConstructors();
}
回答by Arsen Mkrtchyan
you can search for public contructors like this,
你可以像这样搜索公共建设者,
Type t = typeof(Environment);
var c = t.GetConstructors(BindingFlags.Public);
if (!t.IsAbstract && c.Length > 0)
{
//You can create instance
}
Or if you only interested in parameterless constructor you can use
或者,如果您只对无参数构造函数感兴趣,则可以使用
Type t = typeof(Environment);
var c = t.GetConstructor(Type.EmptyTypes);
if (c != null && c.IsPublic && !t.IsAbstract )
{
//You can create instance
}
回答by Joe Chung
type.IsAbstract && type.IsSealed
This would be a sufficient check for C# since an abstract class cannot be sealed or static in C#. However, you'll need to be careful when dealing with CLR types from other languages.
这对 C# 来说是一个足够的检查,因为抽象类不能在 C# 中被密封或静态。但是,在处理来自其他语言的 CLR 类型时需要小心。
回答by shahkalpesh
Type t = typeof(System.GC);
Console.WriteLine(t.Attributes);
TypeAttributes attribForStaticClass = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class |
TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit;
Console.WriteLine((t.Attributes == attribForStaticClass));
I guess, this should work.
我想,这应该有效。