C#中 - 如何检查是否命名空间,类或方法在C#中的存在? [英] C# - How to check if namespace, class or method exists in C#?

查看:766
本文介绍了C#中 - 如何检查是否命名空间,类或方法在C#中的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序,我怎么能在运行时检查一个命名空间,类或方法的存在? ?此外,如何使用它的字符串形式的名称实例化一个类



伪代码:

 字符串@namespace =myNameSpace对象; 
串@class =MyClass的;
字符串的方法=myMethod的;

变种Y = IsNamespaceExists空间(namespace);
变种X = IsClassExists(@class)?新@class:空; //检查是否存在,如果实例化如此。
变种Z = x.IsMethodExists(法);


解决方案

您可以使用的Type.GetType(string)反映一类。 键入有一些方法来发现其他成员,包括方法,可用该类型。



一招,然而,就是的GetType 想要的程序集限定名称。如果你只使用类名称本身,它会假设你引用当前装配。



所以,如果你想找到的所有加载的程序集的类型,你可以做这样的事情(使用LINQ):

  VAR类型=(从装配在AppDomain.CurrentDomain.GetAssemblies()从类型
在assembly.GetTypes()
,其中type.Name ==的className
选择型);



当然,也有可能比的是,在那里你会想反映过来引用更给它这可能尚未加载等组件。



作为确定的命名空间,反思不出口的明显。相反,你必须做一些事情,如:

  VAR namespaceFound =(从装配在AppDomain.CurrentDomain.GetAssemblies()$从类型b $ b在assembly.GetTypes()
,其中type.Namespace ==命名空间
选择型)。任何()

全部放在一起,你会碰到这样的:

  VAR类型=(从装配在AppDomain.CurrentDomain.GetAssemblies()从类型
在assembly.GetTypes()
,其中type.Name ==&的className功放;&安培; type.GetMethods()任何(M = GT ; m.Name ==方法名)
选择型).FirstOrDefault();

如果(类型== NULL)抛出新的InvalidOperationException异常(有效的类型没有找到。);

对象实例= Activator.CreateInstance(类型);


I have a C# program, how can I check at runtime if a namespace, class, or method exists? Also, how to instantiate a class by using it's name in the form of string?

Pseudocode:

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);

解决方案

You can use Type.GetType(string) to reflect a class. Type has methods to discover other members, including a method, that are available to that type.

One trick, however, is that GetType wants an assembly-qualified name. If you use just the class name itself, it will assume you are referencing the current assembly.

So, if you wanted to find the type in all loaded assemblies, you can do something like this (using LINQ):

var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
            from type in assembly.GetTypes()
            where type.Name == className
            select type);

Of course, there may be more to it than that, where you'll want to reflect over referenced assemblies that may not be loaded yet, etc.

As for determining the namespaces, reflection doesn't export those distinctly. Instead, you'd have to do something like:

var namespaceFound = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Namespace == namespace
select type).Any()

Putting it all together, you'd have something like:

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);

这篇关于C#中 - 如何检查是否命名空间,类或方法在C#中的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆