C# 将事件处理程序添加到已在 运行 时间从程序集中加载的 class
C# add event handler to class that has been loaded from an assembly at run time
作为评估名为 "GemBox.document" 的第 3 方 DLL 的一部分,我希望能够在 运行 时间内 运行 此程序集。但是为了在试用模式下达到 运行,我需要使用这个:
ComponentInfo.FreeLimitReached +=
(sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
如果您在应用程序中直接引用 DLL,这是标准方法。但是,我希望能够通过在 运行 时间调用 DLL 来做到这一点。正确的语法是什么?
编辑:ComponentInfo
是 GemBox.Document
的 public 静态 class
为了将来参考,以下是我们如何在 运行 时加载 GemBox.Document 程序集并通过反射将其设置为试用模式:
using System;
using System.Reflection;
class Program
{
// Load GemBox.Document assembly.
static Assembly gemboxAssembly = Assembly.LoadFrom(@"C:\GemBox.Document.dll");
// Create method for handling FreeLimitReached event.
static void HandleFreeLimit(object sender, EventArgs e)
{
// Call: e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial
dynamic freeLimitArgs = e;
freeLimitArgs.FreeLimitReachedAction =
(dynamic)Enum.Parse(
gemboxAssembly.GetType("GemBox.Document.FreeLimitReachedAction"),
"ContinueAsTrial");
}
static void Main(string[] args)
{
// Call: ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Type componentInfo = gemboxAssembly.GetType("GemBox.Document.ComponentInfo");
componentInfo.GetMethod("SetLicense", BindingFlags.Public | BindingFlags.Static)
.Invoke(null, new object[] {"FREE-LIMITED-KEY"});
// Get HandleFreeLimit as MethodInfo.
MethodInfo handleFreeLimitMethod =
typeof(Program).GetMethod("HandleFreeLimit",
BindingFlags.NonPublic | BindingFlags.Static);
// Call: ComponentInfo.FreeLimitReached += HandleFreeLimit
EventInfo freeLimitReached = componentInfo.GetEvent("FreeLimitReached");
freeLimitReached.AddEventHandler(null,
Delegate.CreateDelegate(freeLimitReached.EventHandlerType,
handleFreeLimitMethod));
// Call: DocumentModel document = DocumentModel.Load(@"C:\Sample.docx")
Type documentModel = gemboxAssembly.GetType("GemBox.Document.DocumentModel");
dynamic document = documentModel.GetMethod("Load", new Type[]{ typeof(string)})
.Invoke(null, new object[] { @"C:\Sample.docx" });
// TODO: Use "document" object as needed ...
document.Save(@"C:\Sample.pdf");
}
}
作为评估名为 "GemBox.document" 的第 3 方 DLL 的一部分,我希望能够在 运行 时间内 运行 此程序集。但是为了在试用模式下达到 运行,我需要使用这个:
ComponentInfo.FreeLimitReached +=
(sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
如果您在应用程序中直接引用 DLL,这是标准方法。但是,我希望能够通过在 运行 时间调用 DLL 来做到这一点。正确的语法是什么?
编辑:ComponentInfo
是 GemBox.Document
为了将来参考,以下是我们如何在 运行 时加载 GemBox.Document 程序集并通过反射将其设置为试用模式:
using System;
using System.Reflection;
class Program
{
// Load GemBox.Document assembly.
static Assembly gemboxAssembly = Assembly.LoadFrom(@"C:\GemBox.Document.dll");
// Create method for handling FreeLimitReached event.
static void HandleFreeLimit(object sender, EventArgs e)
{
// Call: e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial
dynamic freeLimitArgs = e;
freeLimitArgs.FreeLimitReachedAction =
(dynamic)Enum.Parse(
gemboxAssembly.GetType("GemBox.Document.FreeLimitReachedAction"),
"ContinueAsTrial");
}
static void Main(string[] args)
{
// Call: ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Type componentInfo = gemboxAssembly.GetType("GemBox.Document.ComponentInfo");
componentInfo.GetMethod("SetLicense", BindingFlags.Public | BindingFlags.Static)
.Invoke(null, new object[] {"FREE-LIMITED-KEY"});
// Get HandleFreeLimit as MethodInfo.
MethodInfo handleFreeLimitMethod =
typeof(Program).GetMethod("HandleFreeLimit",
BindingFlags.NonPublic | BindingFlags.Static);
// Call: ComponentInfo.FreeLimitReached += HandleFreeLimit
EventInfo freeLimitReached = componentInfo.GetEvent("FreeLimitReached");
freeLimitReached.AddEventHandler(null,
Delegate.CreateDelegate(freeLimitReached.EventHandlerType,
handleFreeLimitMethod));
// Call: DocumentModel document = DocumentModel.Load(@"C:\Sample.docx")
Type documentModel = gemboxAssembly.GetType("GemBox.Document.DocumentModel");
dynamic document = documentModel.GetMethod("Load", new Type[]{ typeof(string)})
.Invoke(null, new object[] { @"C:\Sample.docx" });
// TODO: Use "document" object as needed ...
document.Save(@"C:\Sample.pdf");
}
}