通过程序集的字符串表示形式收集程序集类型 [英] Gathering types from assemblies by it's string representation

查看:53
本文介绍了通过程序集的字符串表示形式收集程序集类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方法必须执行下一个操作:
我需要从Xml节点提供其他类型的模块,以便在Windows环境(Console,WPF)中的WCF中进一步托管.

Hi, my method must perform next action:
I need to provide additional module with types from Xml node for further hosting in WCF in windows enviroment(Console, WPF).

public Dictionary<Type, Type> GetServicesToBeHostedFromConfig(XElement configuration)
		{
			try
			{
				_Locker.EnterReadLock();
				Dictionary<Type, Type> returnTypes = new Dictionary<Type, Type>();

				foreach (var item in configuration.Descendants("Items"))
				{
		                  returnTypes[Type.GetType(item.Attribute("service").Value)] = Type.GetType(item.Attribute("contract").Value);
				}

				return returnTypes;
			}
			finally
			{
				_Locker.ExitReadLock();
			}
		}



一切正常,直到我尝试获取位于同一程序集中的类型,即我的方法所在的位置,
但是,当我尝试获取包含在另一个中的类型时,我会失败! (引用了我的主组件!)

我想我必须实现某种程序集解析机制,但是老实说,我不知道该怎么做...



everything work fine until i try to get types which located in the same assembly , where my method located ,
but when i try to get types which containt in another i get fail! (my main assembly referenced to it !)

I suppose i must to implement some assembly resolving mechanism , but if be honest, i dont know how to do it...

Maybe someone can help me with such issue?

推荐答案

首先,让我说服您,使用程序集的字符串表示形式从程序集中获取某些类型或成员是坏事.问题是:在哪里可以得到那些名字?如果这些名称来自代码,那么您就不需要它们了,因为这不是Reflection可以使用的唯一样式(是的,例如,在序列化中使用).但是,当确实需要名称的字符串表示形式时,它来自可能会拼写错误的数据.而且编译器不会给您任何警告.因此,这种技术非常不可靠,首先,从维护的角度来看是不可接受的.

因此,我只是希望我可以向您解释正确的想法,可以改变您的整体方法,然后您可能无需再次解决问题. (或者您稍后会问其他问题.)

正确的方法是通过它们实现的接口识别在动态加载的程序集中声明的某些类型.这样,您完全可以避免依赖任何名称.您可以在由主机"和动态加载的程序集使用的程序集中声明一些接口(让我称呼为插件程序集",而主机"是动态加载其他程序集的那个)在主机和主机引用的单独程序集中插件程序集,但是您可以简单地在主机程序集中声明那些插件接口,并通过所有插件程序集引用主机程序集.如果您考虑一下,您会发现它不会创建任何循环引用,因为程序集之间的引用依赖性是静态的,并且与运行时依赖性无关.

您可以使用System.Type.GetInterfaces:
识别某种类型的插件程序集是否实现了某些接口 http://msdn.microsoft.com/en-us/library/system.type. getinterfaces.aspx [ ^ ].

你有主意吗?

请查看我过去对相关问题的回答:
创建使用可重载插件的WPF应用程序... [^ ],
AppDomain拒绝加载程序集 [使用CodeDom生成代码 [创建使用可重载插件的WPF应用程序... [ ^ ],
动态加载用户控件 [在现有实例上的C#反射InvokeMember [项目和DLL:如何使其可管理? [ ^ ].

—SA
First of all, let me try to convince you that getting some type or members from an assembly using its string representation is bad thing. The question is: where can you get those names? If such names come from code, you don''t really need them, as this is not the only style Reflection can use (yes, this is used -- in serialization, for example). But when a string representation of a name is really needed, it comes from data where it can be misspelled. And the compiler won''t give you a single warning. So, such techniques are very unreliable, first of all, unacceptable from the maintenance perspective.

So, I simply hope that I can explain you the right idea, you can change you whole approach, and then you might not need to solve your problem again. (Or you will ask a different question(s) later.)

The the right approach is to recognize certain types declared in a dynamically loaded assembly by interfaces they implement. This way, you fully avoid dependency on any names. You can declare some interfaces in an assembly used by both "host" and dynamically loaded assemblies (let me call then "plug-in assemblies", and "host" is the one loading other assemblies dynamically) in separate assembly referenced by both host and plug-in assemblies, but you can simply declare those plug-in interfaces in a host assembly and reference a host assembly by all plug-in assemblies. If you think about it, you will see that it does not create any circular references, because referential dependencies between assemblies are static and has nothing to do with run-time dependencies.

You can recognize if a type of a plug-in assembly implements some interface using System.Type.GetInterfaces:
http://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx[^].

Are you getting the idea?

Please see my past answers to related questions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL''s: How to keep them managable?[^].

—SA


这篇关于通过程序集的字符串表示形式收集程序集类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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