2

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

Mario Z
  • 4,328
  • 2
  • 24
  • 38
Notaras
  • 611
  • 1
  • 14
  • 44
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1121441/addeventhandler-using-reflection – Jan Köhler Jun 01 '15 at 05:01
  • Plus one to the previous commentor. I think reflection is the only standard way. – ilyabreev Jun 01 '15 at 05:37
  • 1
    ok thats cool. But the above code is very complicated to re-write in terms of reflection (the use of += and "sender, e" etc). Is there another way to write this line so i can easily re-produce it in reflection? – Notaras Jun 01 '15 at 05:45
  • You would generally use reflection when some aspects of the object model/interaction won't be known until runtime - but in this case, it appears you know *exactly* the classes you want to interact with. Why is "at runtime" a requirement here? Perhaps there's just some misunderstanding that's clouding things here. – Damien_The_Unbeliever Jun 01 '15 at 07:37

1 Answers1

9

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");
    }
}
Mario Z
  • 4,328
  • 2
  • 24
  • 38