我正在自学 C# 中的中间语言 (IL) 生成,并且我一直被困了几个小时——这似乎是几天来如何System.Windows.Forms.dll
从动态程序集中引用(例如) , 我正在使用AppDomain.CurrentDomain.DefineDynamicAssembly
and 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
事件处理程序。那是一个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