0

Suppose I have an interface called IVerifier

public interface IVerifier
{
  bool Validate(byte[]x, byte[]y);
}

and I had to load an assembly by reflection, and that assembly holds the same signature, how possible it is to do this:

IVerifier c = GetValidations();
c.Validate(x,y);

And inside GetValidations() is the reflection resides!

I've been thinking about this a lot, and all I get is that invoking the reflected method is going to be inside GetValidations(), but it must be away to do it like above.

SVI
  • 921
  • 4
  • 11
  • 23
  • assembly has class implements interface? – cuongle Jan 31 '13 at 15:27
  • Use the Assembly.CreateInstance() method to create an object of the class that implements IVerifier. You'll need a good guess at the name of the class that implements it. Or iterate the types in the assembly to find one that implements the interface. Or use a factory method. Or use a plugin framework like MEF. Etcetera. – Hans Passant Jan 31 '13 at 15:47
  • What exactly does the assembly contain? Type that implements the interface? Or just type that has a method with the same signature? Do you know the name of the type (and possibly the method) you want to use? – svick Jan 31 '13 at 16:09

1 Answers1

1

Assuming that you don't know the type you want to instantiate in another assembly, you just know it implements IVerifier you can use a method like this:

static TInterface GetImplementation<TInterface>( Assembly assembly)
{
    var types = assembly.GetTypes();
    Type implementationType = types.SingleOrDefault(t => typeof (TInterface).IsAssignableFrom(t) && t.IsClass);


    if (implementationType != null)
    {
        TInterface implementation = (TInterface)Activator.CreateInstance(implementationType);
        return implementation;
    }

    throw new Exception("No Type implements interface.");       
}

Sample use:

using System;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            IHelloWorld x = GetImplementation<IHelloWorld>(Assembly.GetExecutingAssembly());

            x.SayHello();
            Console.ReadKey();

        }
        static TInterface GetImplementation<TInterface>( Assembly assembly)
        {
            var types = assembly.GetTypes();
            Type implementationType = types.SingleOrDefault(t => typeof (TInterface).IsAssignableFrom(t) && t.IsClass);


            if (implementationType != null)
            {
                TInterface implementation = (TInterface)Activator.CreateInstance(implementationType);
                return implementation;
            }

            throw new Exception("No Type implements interface.");

        }
    }
    interface IHelloWorld
    {
        void SayHello();
    }
    class MyImplementation : IHelloWorld
    {
        public void SayHello()
        {
            Console.WriteLine("Hello world from MyImplementation!");
        }
    }

}
James
  • 2,445
  • 2
  • 25
  • 35
  • Great answer, wonderful explanation, deserved to be marked as answered and to have a +1 – SVI Feb 02 '13 at 10:34