如何从动态生成的程序集中引用GAC程序集
本文关键字:程序集 程序 集中 引用 GAC 动态 | 更新日期: 2023-09-27 18:01:12
我正在用C#自学中间语言(IL(生成,我一直在学习如何从动态程序集中引用System.Windows.Forms.dll
(例如(,我正在使用AppDomain.CurrentDomain.DefineDynamicAssembly
和System.Reflection.Emit
生成。。。基于http://olondono.blogspot.com/2008/02/creating-code-at-runtime.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
事件处理程序。那是一个骗局。我们只需要提供程序集的完全限定名称。。。其中一个具有程序集名称、命名空间、版本和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
或在.NET4中使用解析程序函数的新Type.GetType
重载是可行的。请注意,从GAC或winsxs路径手动解析通常是个坏主意:让框架来完成解析工作。