Search code examples
c#event-handlinggembox-document

C# add event handler to class that has been loaded from an assembly at run time


As part of evaluating a 3rd party DLL called "GemBox.document", i want to be able to run this assembly during run time. However in order to get it to run in trial mode, i need to use this:

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

This is the standard way if you directly reference the DLL in the application. However, i want to be able to do this by calling the DLL at runtime. What is the correct syntax for this?

Edit: ComponentInfo is a public static class of GemBox.Document


Solution

  • For future reference, here is how we can load GemBox.Document assembly at run-time and set it in a Trial mode through reflection:

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