在提取运行时类型 [英] Extract type at runtime

查看:176
本文介绍了在提取运行时类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型的名字被作为输入传递。     MyApp.Modules.Common.contact。

I have a Type name being passed as an input . "MyApp.Modules.Common.contact".

使用 Activator.CreateInstance 如何构建这种类型的,我使用的方法。 如果我不喜欢这样

Using Activator.CreateInstance how do I construct this type in the method that I'm using. if I do it like this

Type typ = Type.GetType("MyApp.Modules.Common.contact")

的典型值总是空。我该如何解决这个问题。请大家帮帮忙。

the typ is always null. How do I fix this. Please help.

推荐答案

如果你只提供了类型名称, Type.GetType 将查找当前执行的程序集,和的mscorlib - 但仅此而已。如果您需要访问不同的装配类型,那么无论你需要得到的类型的程序集的名称为好,如: MyApp.Modules.Common.contact,MyApp.Modules.Common(如果程序集的名称是MyApp.Modules.Common - 或者,如果你有一个组装的参考,你可以使用的 Assembly.GetType(字符串)

If you provide just the type name, Type.GetType will look in the currently-executing assembly, and mscorlib - but that's all. If you need to access a type in a different assembly, then either you need to get the assembly name in the type as well, e.g. "MyApp.Modules.Common.contact, MyApp.Modules.Common" (if the assembly name is "MyApp.Modules.Common" - or if you have an Assembly reference, you can use Assembly.GetType(string).

如果你没有什么组件,你应该看看信息,但你相信,大会的的加载,你可能使用的 AppDomain.GetAssemblies() 找到组件,然后查看每那些反过来,调用的 Assembly.GetType ,直到找到一个匹配。

If you have no information about what assembly you should look in, but you're confident that the assembly has been loaded, you could potentially use AppDomain.GetAssemblies() to find the assemblies, and then look through each of those in turn, calling Assembly.GetType until you find a match.

我会鼓励你看看那里怎么样类型的信息被传来传去的设计虽然 - 理想情况下,确保了装配信息的可用

I would encourage you to look at the design of where how the type information is being passed around though - ideally make sure that the assembly information is available.

这篇关于在提取运行时类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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