0

So i try to create a PluginService which loads assemblys from a folder and checks if it implements a certain interface. I work with the Interface

public interface IService
    {
        void Start();
        void Stop();
        void Restart();
    }

And the Class

public class Service : IService
    {

        public Service()
        {

        }

        public void Restart()
        {

        }

        public void Start()
        {

        }

        public void Stop()
        {

        }
    }

And i found this Question from pogorman which helped me a lot. EDIT: Sorry for the confussion i mean this code snippet not the DoesTypeImplementInterface

currentAssembly.GetTypes()
                .Where(t => t != typeof(T) && typeof(T).IsAssignableFrom(t))
                .ToList()
                .ForEach(x => implementors.Add((T)Activator.CreateInstance(x)));
KRyTeX
  • 48
  • 7
  • Can you please add the code, where you use the method? – Nikolaus Mar 03 '19 at 13:25
  • What is the question? – Martin Zikmund Mar 03 '19 at 16:45
  • Are you sure, you have an assembly with this Interface implemented? – Nikolaus Mar 03 '19 at 16:56
  • @MartinZikmund The code returns no Class but there is one Class that implements the IService Interface. – KRyTeX Mar 03 '19 at 17:25
  • @Nikolaus yeah if i debug it the `TypeInfo.ImplementedInterfaces` Property of the Class Service is `IService` – KRyTeX Mar 03 '19 at 17:28
  • I‘ll do further research, because I had a similar issue. – Nikolaus Mar 04 '19 at 09:24
  • Try:` t.GetInterfaces().Contains(typeof (T))` instead of the IsAssignableFrom-part. Does it work then? – Nikolaus Mar 04 '19 at 09:42
  • Is it possible, that you work with .net Core? – Nikolaus Mar 04 '19 at 09:44
  • My suspicion is that the `IService` that's being implemented by the services is not the same one (doesn't have the same runtime identity) as the one that the host is trying to check. As a test, get the `IService` type instance from the type you know implements it and check that it's reference-equal to `typeof(IService)` in the host. If not, look at the types' `Assembly` properties. Chances are that they're different types in the same namespace, compiled into both host and plugin assemblies. – madreflection Mar 05 '19 at 00:34
  • @Nikolaus Unfortunately not :\ – KRyTeX Mar 05 '19 at 10:05
  • Then I think madreflections comment looks quite realistic. – Nikolaus Mar 05 '19 at 14:47

0 Answers0