mardi 11 octobre 2016

How to use dynamically linked dll in c#

I imported the taglib dll in my C# application and then used types and methods from the library in the following way:

using TagLib;

private void method()
{
    TagLib.File file = TagLib.File.Create("c:\temp\some.mp3")
}

Now I want to link the dll dynamically. How can I implement the same functional in this case?

That, what I've tried:

using System.Reflection

private void method()
{
    Assembly TagLib = Assembly.Load("taglib");

    Type TagLibFile = TagLib.GetType("File");
    dynamic LibFile = Activator.CreateInstance(TagLibFile);
    MethodInfo FileCreate = TagLibFile.GetMethod("Create");

    TagLibFile file = LibFile.Create(fi.FullName);
}

In this implementation, VisualStudio says that I can't use the tagLibFile variable as a type. I supposed that when I get a type from dll, I will be able to create variables of this type.

By the way, Is this approach correct?





Aucun commentaire:

Enregistrer un commentaire