-1

I have a trouble with connecting DLL while running my application.

What I have?

  1. I have Interfaces.dll where I defined interfaces which will be implemented by plugins. I've made new folder Plugins and copied dll to this folder.

    public interface ITable
    {
        string ToString(List<int> _intArray);
        List<int> ToHex(string _stringArray);
    }
    public interface IAbout
    {
        string Description { get; }
        string Name { get; }
        string Author { get; }
        string Date { get; }
        string Link { get; }
        bool isDefault { get; }
        void About();
    }
    
  2. I have plugin TableBasic.dll which implements interfaces from Interfaces.dll. When I was making it, I've added Interfaces.dll to references from folder Plugins

    public class Table : ITable, IAbout
    {
        ...
    }
    
  3. I have application where I have static class Plugins with static method Load which loading specified dll imlemenitng specified interface. I've added Interfaces.dll from folder Plugins to application's references.

    public static object Load(string path, Type seekingInterface)
    {
        Type DLL = Assembly
            .LoadFrom(path)
            .GetTypes()
            .First(t => seekingInterface.IsAssignableFrom(t) &&
            typeof(IAbout).IsAssignableFrom(t));
        return Activator.CreateInstance(DLL);
    }
    

Error

When I try to call load function as

Plugins.Load(@"D:\Repositories\Crystal\Plugins\TableBasic.dll", typeof(ITable));

I get exception:

System.InvalidOperationException: 'Sequence contains no matching element'

But I don't know why, it looks like my application and my plugin use different Interfaces.dll and interfaces ITable and IAbout in my application and in plugin is different or... I do not know what to suggest.

Could you please to help me?

Why not a duplicate: I want to get type that implements specifyied interface, getting type by assembly.GetType("MyType"); is not acceptable because I don't know name of type and I will not able to cast it to any interface if I didn't check type implements interface or not.

  • It does look correct. Maybe you could check the result of `GetInterfaces` on the class from your plugin DLL and see if there are any obvious differences to the types that you want them to match – Daniel Hilgarth Apr 05 '18 at 07:07
  • Possible duplicate of [Can I load a .NET assembly at runtime and instantiate a type knowing only the name?](https://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na) – Simon Price Apr 05 '18 at 07:14
  • tihs question has been asked many time in the past, take a look at this answer https://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na – Simon Price Apr 05 '18 at 07:14
  • The code: `Type DLL = Assembly .LoadFrom(path) .GetTypes() .First() .GetInterface("ITable"); Console.WriteLine(DLL.FullName); Console.WriteLine(seekingInterface.FullName);` gets me: `Interfaces.ITable Interfaces.ITable` – Ace Lightning Apr 05 '18 at 07:21
  • And what is the output of `typeof(ITable).FullName` and `seekingInterface.FullName`? – Daniel Hilgarth Apr 05 '18 at 07:42
  • @SimonPrice: I disagree that it is a duplicate. The OP generally seems to know how to dynamically load a type from a DLL. However, it is not working, he has a concrete problem with his approach. Are you able to find an answer to his concrete problem in the question or answer linked by you? – Daniel Hilgarth Apr 05 '18 at 07:45
  • @Daniel Hilgarth, they both return Interfaces.ITable and it's correct, I tried to get GUID of both interfaces and GUID's of both are identical. – Ace Lightning Apr 05 '18 at 07:48
  • 1
    In that case, try to reduce the problem: Try to create a complete, small example of the problem. – Daniel Hilgarth Apr 05 '18 at 07:49
  • Do you mean to create new Interface, Plugin and Application projects? – Ace Lightning Apr 05 '18 at 07:51
  • Yes. Something that you can share with us – Daniel Hilgarth Apr 05 '18 at 08:01
  • I made test solution and... it works good. I'm confused. Why it doesn't work in my not test project... https://1drv.ms/u/s!ApdUzWbdZon2mSfakXpevw8K1z6U – Ace Lightning Apr 05 '18 at 08:11
  • Move part-by-part to your test solution until it does not work anymore... – Christoph Fink Apr 05 '18 at 08:20
  • Okay, I'm going to create new solution and will be move code from old. I will update this thread once it' completed. – Ace Lightning Apr 05 '18 at 08:35

1 Answers1

0

I've created new project and moved code from old project to new. And all works as I expected. I cannot explain why old project didn't work. I've compared files *.csproj of new and of old project and I didn't see any differents except ProjectGuid but it must be different in different projects.