C# 如何在.NET运行时检测类的存在?
在.NET应用程序(C#)中,是否可以有条件地检测类是否在运行时定义C# 如何在.NET运行时检测类的存在?,c#,reflection,class,runtime,C#,Reflection,Class,Runtime,在.NET应用程序(C#)中,是否可以有条件地检测类是否在运行时定义 示例实现-假设您要基于配置选项创建类对象?Activator.CreateInstance可能适合以下条件: 当然,如果不能实例化该类,它会抛出一个异常,这与该类是否“存在”并不完全相同。但是,如果您不能实例化它,并且不希望只调用静态成员,那么它应该可以为您提供帮助 您可能正在寻找具有字符串参数的重载,第一个参数应该是程序集的名称,第二个参数是类的名称(完全限定命名空间) 关于问题的第二部分:- 示例实现-说出您想要的 基于
示例实现-假设您要基于配置选项创建类对象?Activator.CreateInstance可能适合以下条件: 当然,如果不能实例化该类,它会抛出一个异常,这与该类是否“存在”并不完全相同。但是,如果您不能实例化它,并且不希望只调用静态成员,那么它应该可以为您提供帮助 您可能正在寻找具有字符串参数的重载,第一个参数应该是程序集的名称,第二个参数是类的名称(完全限定命名空间) 关于问题的第二部分:- 示例实现-说出您想要的 基于类创建类对象的步骤 配置选项 我不知道你为什么要那样做。但是,如果您的类实现了一个接口,并且希望根据配置文件动态创建这些类的对象,那么我认为您可以查看Unity IoC container。如果它适合你的场景,那么它真的很酷而且很容易使用。关于如何做到这一点的一个例子是 ,从配置中加载一个类并实例化它。在本例中,我需要确保配置中指定的类继承自一个名为NinjectModule的类,但我想您已经明白了
protected override IKernel CreateKernel()
{
// The name of the class, e.g. retrieved from a config
string moduleName = "MyApp.MyAppTestNinjectModule";
// Type.GetType takes a string and tries to find a Type with
// the *fully qualified name* - which includes the Namespace
// and possibly also the Assembly if it's in another assembly
Type moduleType = Type.GetType(moduleName);
// If Type.GetType can't find the type, it returns Null
NinjectModule module;
if (moduleType != null)
{
// Activator.CreateInstance calls the parameterless constructor
// of the given Type to create an instace. As this returns object
// you need to cast it to the desired type, NinjectModule
module = Activator.CreateInstance(moduleType) as NinjectModule;
}
else
{
// If the Type was not found, you need to handle that. You could instead
// initialize Module through some default type, for example
// module = new MyAppDefaultNinjectModule();
// or error out - whatever suits your needs
throw new MyAppConfigException(
string.Format("Could not find Type: '{0}'", moduleName),
"injectModule");
}
// As module is an instance of a NinjectModule (or derived) class, we
// can use it to create Ninject's StandardKernel
return new StandardKernel(module);
}
我的静态函数版本:
/// <summary>
/// returns if the class exists in the current context
/// </summary>
/// <param name="className">Class Name</param>
/// <returns>class status</returns>
public static bool ClassExist(string className)
{
Type type = Type.GetType(className);
if (type != null)
{
return true;
}
return false;
}
//
///如果类在当前上下文中存在,则返回
///
///类名
///阶级地位
公共静态bool类存在(字符串类名称)
{
Type Type=Type.GetType(类名);
if(type!=null)
{
返回true;
}
返回false;
}
Type.GetType(“someType”)有什么问题?MSDN声明Actiator.CreateInstance()是“代价高昂的函数”之一。另外,我不确定您为什么要为此抛出一个异常,因为抛出一个异常可能也很昂贵。“昂贵”是相对的。如果你只让co做几次,那就没那么贵了。如果你做了数百万次,你会想找到更好的解决方案。@Ashish:从配置创建实例是Unity所做的事情之一。@John,没错。我知道它主要用于依赖注入。我只是建议他看看。这可能不是问题的答案。无论如何,谢谢你。
/// <summary>
/// returns if the class exists in the current context
/// </summary>
/// <param name="className">Class Name</param>
/// <returns>class status</returns>
public static bool ClassExist(string className)
{
Type type = Type.GetType(className);
if (type != null)
{
return true;
}
return false;
}