按类名获得总成 [英] get assembly by class name

查看:82
本文介绍了按类名获得总成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让包含有名称的类的TestClass 组装?
我只知道类名,所以我不能创建的一个实例。以及

Is there any way to get the assembly that contains a class with name TestClass? I just know the class name, so I can't create an instance of that. And

Type objectType = assembly.GetType("TestClass");



并没有为我工作。

didn't work for me.

推荐答案

中的问题类型的objectType ,我假设你的实际上<按名称类型后/ em>的(不是组装);这样的假设的程序集加载,并且类型名称是唯一,LINQ可以帮助:

From the Type objectType in the question, I assume you are actually after the type by name (not the assembly); so assuming the assembly is loaded and the type name is unique, LINQ may help:

Type objectType = (from asm in AppDomain.CurrentDomain.GetAssemblies()
                   from type in asm.GetTypes()
                   where type.IsClass && type.Name == "TestClass"
                   select type).Single();
object obj = Activator.CreateInstance(objectType);



然而,它可能是更好的组件限定名称代替类型名称的工作。

However, it may be better to work with the assembly-qualified name instead of the type name.

这篇关于按类名获得总成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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