从类名称在不同的装配类型解析 [英] Resolve Type from Class Name in a Different Assembly

查看:118
本文介绍了从类名称在不同的装配类型解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我需要解决一个类的类型的方法。这个类存在于具有类似名称空间的另一个组件:

I have a method where I need to resolve the Type of a class. This class exists in another assembly with the namespace similar to:

MyProject.Domain.Model

我在尝试执行以下操作:

I am attempting to perform the following:

Type.GetType("MyProject.Domain.Model." + myClassName);

这个伟大的工程,如果执行此操作的code是在相同的组件,其类型我试图解析类,但是,如果我的课是在一个不同的组件,这code失败

This works great if the code that is performing this action is in the same assembly as the class whose type I am trying to resolve, however, if my class is in a different assembly, this code fails.

我肯定有一个更好的方式来完成这项任务,但我还没有很多经验与解决组件和内遍历命名空间来解决我要找的类的类型。任何意见或提示,以更优雅完成这项任务?

I am sure there is a far better way to accomplish this task, but I have not had a lot of experience with resolving assemblies and traversing namespaces within to resolve the type of the class I am looking for. Any advice or tips to accomplish this task more gracefully?

推荐答案

您必须添加程序集名称是这样的:

You'll have to add the assembly name like this:

Type.GetType("MyProject.Domain.Model." + myClassName + ", AssemblyName");

要避免含糊不清,或者如果组件位于GAC,你应该提供一个完全合格的程序集名称等一样:

To avoid ambiguity or if the assembly is located in the GAC, you should provide a fully qualified assembly name like such:

Type.GetType("System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

这篇关于从类名称在不同的装配类型解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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