C#-如何检查C#中是否存在命名空间、类或方法?

C#-如何检查C#中是否存在命名空间、类或方法?,c#,class,methods,namespaces,C#,Class,Methods,Namespaces,我有一个C#程序,如何在运行时检查名称空间、类或方法是否存在?另外,如何以字符串的形式使用类的名称来实例化类 伪代码: string @namespace = "MyNameSpace"; string @class = "MyClass"; string method= "MyMEthod"; var y = IsNamespaceExists(namespace); var x = IsClassExists(@class)? new @class : null; //Check if e

我有一个C#程序,如何在运行时检查名称空间、类或方法是否存在?另外,如何以字符串的形式使用类的名称来实例化类

伪代码:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var y = IsNamespaceExists(namespace);
var x = IsClassExists(@class)? new @class : null; //Check if exists, instantiate if so.
var z = x.IsMethodExists(method);
您可以使用一个类型<如果找不到类型,code>GetType将返回null。如果类型存在,则可以从返回的
类型
中使用
GetMethod
GetField
GetProperty
等来检查您感兴趣的成员是否存在

以你为例:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
var myMethodExists = myClassType.GetMethod(method) != null;

Console.WriteLine(myClassType); // MyNameSpace.MyClass
Console.WriteLine(myMethodExists); // True
Type myType = Type.GetType("MyNamespace.MyClass");
MethodInfo myMethod = myType.GetMethod("MyMethod");
object instance = Activator.CreateInstance(myType);
假设类型位于mscorlib中当前正在执行的程序集中(不确定.NET Core对此有何影响,可能是
System.Runtime
),或者您对该类型有一个定义,则这是最有效和首选的方法。如果传递给
GetType
的字符串参数不满足这三个要求,
GetType
将返回null(假设没有其他类型意外地与这些要求重叠,oops)


如果没有程序集限定名,则需要修正方法,或者执行搜索,后者可能会慢得多

如果我们假设您确实希望在所有加载的程序集中搜索该类型,则可以执行以下操作(使用LINQ):

当然,可能还有更多的内容,您需要反映可能尚未加载的过度引用的程序集,等等

至于确定名称空间,反射并没有明确地导出这些名称空间。相反,您必须执行以下操作:

var namespaceFound = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Namespace == namespace
select type).Any()
var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                from type in assembly.GetTypes()
                where type.Name == className && type.GetMethods().Any(m => m.Name == methodName)
                select type).FirstOrDefault();

if (type == null) throw new InvalidOperationException("Valid type not found.");

object instance = Activator.CreateInstance(type);
把所有这些放在一起,你会得到如下结果:

var namespaceFound = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Namespace == namespace
select type).Any()
var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                from type in assembly.GetTypes()
                where type.Name == className && type.GetMethods().Any(m => m.Name == methodName)
                select type).FirstOrDefault();

if (type == null) throw new InvalidOperationException("Valid type not found.");

object instance = Activator.CreateInstance(type);
一个字:。除了名称空间之外,您必须从类型名称中解析这些名称

编辑:删除该选项-对于名称空间,必须使用Type.Namespace属性来确定每个类属于哪个名称空间。(有关更多信息,请参阅)。

您可以使用该方法从字符串中解析。 例如:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
var myMethodExists = myClassType.GetMethod(method) != null;

Console.WriteLine(myClassType); // MyNameSpace.MyClass
Console.WriteLine(myMethodExists); // True
Type myType = Type.GetType("MyNamespace.MyClass");
MethodInfo myMethod = myType.GetMethod("MyMethod");
object instance = Activator.CreateInstance(myType);
然后,可以使用此类型实例通过调用方法来检查该类型上是否存在方法。 例如:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
var myMethodExists = myClassType.GetMethod(method) != null;

Console.WriteLine(myClassType); // MyNameSpace.MyClass
Console.WriteLine(myMethodExists); // True
Type myType = Type.GetType("MyNamespace.MyClass");
MethodInfo myMethod = myType.GetMethod("MyMethod");
object instance = Activator.CreateInstance(myType);
如果没有为给定名称找到类型或方法,则GetType和GetMethod都返回
null
,因此可以通过检查方法调用是否返回null来检查类型/方法是否存在

最后,您可以使用 例如:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
var myMethodExists = myClassType.GetMethod(method) != null;

Console.WriteLine(myClassType); // MyNameSpace.MyClass
Console.WriteLine(myMethodExists); // True
Type myType = Type.GetType("MyNamespace.MyClass");
MethodInfo myMethod = myType.GetMethod("MyMethod");
object instance = Activator.CreateInstance(myType);

您如何通过示例确定名称空间是否存在?更新了我的答案。检查第二个代码示例。我们枚举每个已加载的程序集和每个程序集中的每个类型,并查找具有所需命名空间名称的任何类型。如果要检查命名空间是否有效、类名是否有效以及类是否具有方法,可以使用第三个代码示例一次性完成这一切。只需将where type.Name==className更改为类似于
where type.FullName==string.Format(“{0}.{1}”,名称空间,className)&&type.GetMethods().Any(m=>m.Name==methodName)
。此外,我在这些示例中犯了一个错误,遗漏了语句的
select
部分。更正。+1个很好的例子。我甚至没有意识到有一个Type.Namespace属性……你可以用嵌套的
foreach
循环来实现这一点;我只是和林克一起去,因为这样比较容易。如果您愿意,我可以提供这样一个例子。请记住将最合适的回答标记为答案。这是一个比上面标记为正确的答案更快、更简洁的答案。在每个组件中旋转每种类型都非常慢。如果你包括所有的系统程序集,那可能是一个10000次的迭代循环。这就是我想要的,简短而简单。反射URL被关闭了,不幸的是,固定的反射链接被破坏了。