c#在运行时从程序集加载的类中添加事件处理程序



作为评估名为"GemBox "的第三方DLL的一部分。文档",我希望能够在运行时运行此程序集。然而,为了让它在试用模式下运行,我需要使用这个:

ComponentInfo.FreeLimitReached += 
    (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

如果您在应用程序中直接引用DLL,这是标准的方式。然而,我希望能够通过在运行时调用DLL来做到这一点。正确的语法是什么?

编辑:ComponentInfoGemBox.Document的一个公共静态类

为了将来的参考,下面是我们如何加载GemBox。运行时的文档程序集,并通过反射将其设置为试用模式:

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");
    }
}

相关内容

  • 没有找到相关文章

最新更新