作为评估名为“GemBox.document”的第三方DLL的一部分,我希望能够在运行时运行此程序集。但是为了让它在试用模式下运行,我需要使用它:
ComponentInfo.FreeLimitReached +=
(sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
如果您直接在应用程序中引用DLL,这是标准方法。但是,我希望能够通过在运行时调用DLL来实现此目的。这个的正确语法是什么?
修改:ComponentInfo
是GemBox.Document
答案 0 :(得分:7)
为了将来参考,以下是我们如何在运行时加载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");
}
}