Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将事件处理程序添加到在运行时从程序集加载的类中_C#_Event Handling_Gembox Document - Fatal编程技术网

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

C#将事件处理程序添加到在运行时从程序集加载的类中,c#,event-handling,gembox-document,C#,Event Handling,Gembox Document,作为评估名为“GemBox.document”的第三方DLL的一部分,我希望能够在运行时运行此程序集。但是,为了使其在试用模式下运行,我需要使用以下命令: ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial; 这是在应用程序中直接引用DLL的标准方法。但是,我希望能够通过在运行时调用DLL来实现这一点。

作为评估名为“GemBox.document”的第三方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");
    }
}

可能与上一个评论员重复一次。我认为反射是唯一的标准方式。好的,那很酷。但是上面的代码在反射方面很难重写(使用+=和“sender,e”等)。有没有其他方法来编写这行代码,这样我就可以在反射中轻松地重新生成它?当对象模型/交互的某些方面直到运行时才知道时,您通常会使用反射-但在这种情况下,您似乎确切地知道要与之交互的类。为什么“在运行时”在这里是一个要求?也许只是一些误会让事情变得不明朗。