[英]How to reference a GAC-assembly from a dynamically generated assembly?
I'm teaching myself Intermediate Language (IL) generation in C#, and I've been stuck-for-a-couple-of-hours-which-seem-like-days on how to reference System.Windows.Forms.dll
(例如)来自动态程序集,我使用AppDomain.CurrentDomain.DefineDynamicAssembly
和System.Reflection.Emit
生成...基于http://olondono.blogspot.com/2008/02的最优秀示例/在运行时创建代码。html
我有一个基本的 TransferObjectDllGenerator 工作,但现在我想(仅)在生成的程序集中引用现有库,但不知道如何。
这个 SO 问题将我引向AppDomain.CurrentDomain.AssemblyResolve
事件。 我尝试实现一个事件处理程序,但它从未被触发,所以我想我做了一些基本愚蠢的事情,比如把事件处理程序放在完全错误的地方?
任何指向正确方向的指针将不胜感激。
这是我的主线...有趣的部分是// <<-- Commented thus
using System;
using System.Reflection;
//using System.Windows.Forms; (and removed the project's Reference to System.Windows.Forms)
namespace ILGen
{
/// <summary>
/// Generates .\bin\$whatever\PersonLibrary.dll containing MSIL equivalent to:
/// namespace PersonLibrary {
/// public class Person {
/// public string FirstName { get; set; }
/// public string LastName { get; set; }
/// public Person() { }
/// public Person(string firstname, string lastname) {
/// FirstName = firstname;
/// LastName = lastname;
/// }
/// } //end-class
/// } //end-namespace
/// </summary>
public class Program
{
public static void Main(String[] args) {
AppDomain.CurrentDomain.AssemblyResolve += MyAssemblyResolver; // <<-- Hook the "resolve this assembly" event.
try {
var dll = new TransferObjectDllGenerator("PersonLibrary");
dll.AddClass("Person", new[] {
new Property("FirstName", typeof(string))
, new Property("LastName", typeof(string))
, new Property("OK", Type.GetType("System.Windows.Forms.Button")) // <<-- References System.Windows.Forms.dll; Type.GetType returns null.
});
Console.WriteLine("Generated: " + dll.Save());
} finally {
AppDomain.CurrentDomain.AssemblyResolve -= MyAssemblyResolver; // <<-- Unhook the "resolve this assembly" event.
}
}
static Assembly MyAssemblyResolver(object sender, ResolveEventArgs args) // <<-- Handle the "resolve this assembly" event.
{
if ( args.Name.StartsWith("System.Windows.Forms.") ) // <<-- Breakpoint here, which is never reached.
return Assembly.LoadFrom(@"C:\Windows\winsxs\msil_system.windows.forms_b77a5c561934e089_6.0.6001.22230_none_1a2132e45d2f30fc\System.Windows.Forms.dll");
return null;
}
} // end-class
} // end-namespace
如果您需要/想要 TransferObjectDllGenerator 代码,请大喊...我没有发布它,因为它对于论坛帖子来说有点太大(恕我直言)。
在此先感谢您的时间。
干杯。 基思。
编辑:为将来找到此线程的任何人提供一个工作示例。
无需提供自定义AssemblyResolve
事件处理程序。 那是一个purphy 。 我们只需要提供程序集的完全限定名称......其中包含程序集名称、命名空间、版本和 GUID。
using System;
using System.Reflection;
namespace ILGen
{
public class Program
{
public static void Main(String[] args) {
var dll = new TransferObjectDllGenerator("PersonLibrary");
// We need to provide the fully-qualified-assembly-name to
// make the standard assembly-resolver search the GAC.
// Thanks to Julien Lebosquain for pointing this out.
Type buttonType = Type.GetType(
"System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
dll.AddClass("Person", new[] {
new Property("FirstName", typeof(string))
, new Property("LastName", typeof(string))
, new Property("OK", buttonType)
});
Console.WriteLine("Generated: " + dll.Save());
}
} // end-class
} // end-namespace
未指定程序集名称的Type.GetType
将仅在当前程序集和 mscorlib 中搜索类型名称。
尝试使用该类型的AssemblyQualifiedName
。 此处为 .NET 4:
Type.GetType("System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
默认解析器将在 GAC 和应用程序域私有路径中进行搜索,因此您无需执行任何操作即可获得所需的行为。 如果您必须自定义分辨率, AssemblyResolve
或新的Type.GetType
重载采用 .NET 4 中的解析器 function 是 go 的方法。 请注意,从 GAC 或 winsxs 路径手动解析通常是一个坏主意:让框架完成其解析工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.