none
How to use an Interface of external dll in c# RRS feed

  • Question

  • Hi!

    I need to use some methods that in an external dll, I have already done something like that, but this time that external dll is an Interface.

    I do like this:

    Assembly Assembly = Assembly.LoadFrom("fullpathofdll"); 
    Type Type = Assembly.GetType("mytype"); 
    dynamic Instance = Activator.CreateInstance(Type);

    But I got this error: Cannot create an instance of an interface

    I understand that's not possible instaciate an interface, but I don't know how to do this to work.

    Someone can help?



    MCTS .NET Framework 4, Web Applications

    Friday, August 29, 2014 2:59 PM

Answers

  • Hello Duan,

    1. >> But I got this error: Cannot create an instance of an interface

    1.1 In C# and other object-oriented programming languages, an interface is a programming construct that indicates a type.

    1.2 An interface declaration contains public properties and methods which is meant to be implemented by a class.

    1.3 An interface declaration is not a class declaration and so it cannot be instantiated the way a class can, e.g. :

    public class MyClass {...}
    public interface MyInterface {...}
    
    MyClass my_class = new MyClass(); // This is OK.
    MyInterface m_type = new MyInterface();  // Error.
    

    1.3 Rather, an interface is meant to be derived from a class. The class that derives from the interface must implement the public properties and methods declared in that interface. The implementing class is also known as a concrete class.

    1.4 Note the following example :

    namespace DuanBritoInterfaces
    {
        public interface MyType
        {
            int IntegerProperty
            {
                get;
                set;
            }
    
            string StringProperty
            {
                get;
                set;
            }
    
            void Method01();
        }
    }
    

    Here, MyType is declared as an interface.

    Note the next example :

    public class MyClass : MyType
    {
        public MyClass()
        {
        }
    
        public int IntegerProperty
        {
            get
            {
                return m_IntegerProperty;
            }
    
            set
            {
                m_IntegerProperty = value;
            }
        }
    
        public string StringProperty
        {
            get
            {
                return m_StringProperty;
            }
    
            set
            {
                m_StringProperty = value;
            }
        }
    
        public void Method01()
        {
            Console.WriteLine("MyClass instance Method01().");
        }
    
        private int m_IntegerProperty = 0;
        private string m_StringProperty = "MyClass string";
    }
    

    Here, MyClass is a concrete class which implements MyType.

    Note the next example :

    public class MyOtherClass : MyType
    {
        public MyOtherClass()
        {
        }
    
        public int IntegerProperty
        {
            get
            {
                return m_IntegerProperty;
            }
    
            set
            {
                m_IntegerProperty = value;
            }
        }
    
        public string StringProperty
        {
            get
            {
                return m_StringProperty;
            }
    
            set
            {
                m_StringProperty = value;
            }
        }
    
        public void Method01()
        {
            Console.WriteLine("MyOtherClass instance Method01().");
        }
    
        private int m_IntegerProperty = 0;
        private string m_StringProperty = "MyOtherClass string";
    }

    Here MyOtherClass is another implementation of MyType.

    In short, there can be any number of concrete class which implement a specific interface and the bottom line is that an interface itself is not an implementation and it cannot be instantiated.

    2. Note the following example :

    static void DoTest()
    {
        // Load the assemby which defines the MyType interface.
        Assembly InterfaceAssembly = Assembly.LoadFrom("DuanBritoInterfaces.dll");
        // Obtain the MyType interface.
        Type InterfaceType = InterfaceAssembly.GetType("DuanBritoInterfaces.MyType");
    
        // Load the assembly which contains a class which implements MyType.
        Assembly ImplementationAssembly = Assembly.LoadFrom("DuanBritoImplementations.dll");
        // Obtain the MyClass type (it implements MyType).
        Type ImplementationType = ImplementationAssembly.GetType("DuanBritoImplementations.MyClass");
    
        // Create an instance of MyClass.
        var Instance = Activator.CreateInstance(ImplementationType);
    
        // Invoke the Method01() method. Note that we only call methods of the MyType interface.
        ImplementationType.InvokeMember("Method01", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, Instance, null);
    
        // Obtain the MyOtherClass type (it also implements MyType).
        ImplementationType = ImplementationAssembly.GetType("DuanBritoImplementations.MyOtherClass");
    
        // Create an instance of MyOtherClass.
        Instance = Activator.CreateInstance(ImplementationType);
    
        // Invoke the Method01() method. Note that we only call methods of the MyType interface.
        ImplementationType.InvokeMember("Method01", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, Instance, null);
    }

    2.1 In the above example, I create 2 assemblies : DuanBritoInterfaces.dll and DuanBritoImplementations.dll.

    2.2 In DuanBritoInterfaces.dll, I define only the MyType interface. In DuanBritoImplementations.dll, I defined 2 concrete classes MyClass and MyOtherClass both of which implements MyType.

    2.3 Notice that when I call Activator.CreateInstance() I use not the type of the interface but the types of the concrete classes.

    2.4 The other thing to note is that I only call methods of the MyType interface. MyClass and MyOtherClass may actually contain other methods but these are not part of MyType.

    2.5 Note finally that MyClass::Method01() and MyOtherClass::Method02() are different. And this is the whole point of why we have interfaces and implementations in the first place : to separate them so that a client (which uses an interface) is coded without prior knowledge of how an interface's methods will be implemented.

    2.6 Actual implementation should be decided at runtime and behavior can be different depending on which implementation eventually gets used.

    3. I hope this rather long post has been helpful.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by chriga Monday, September 1, 2014 6:32 AM
    • Marked as answer by Duan Brito Monday, September 1, 2014 5:56 PM
    Monday, September 1, 2014 5:11 AM

All replies

  • If you're dealing with Office Automation, you'll find that lots of Interface points to something like <InterfaceName>Class. (For example, Excel.Application can be instantiated through Excel.ApplicationClass.

    Now you only need to figure out how is the interface expected to be instantiated from.

    If you have the DLL, it may help if you use "Object Browser" of VS to skim through the contents and see if you have an idea.


    Monday, September 1, 2014 2:26 AM
    Answerer
  • Hello Duan,

    1. >> But I got this error: Cannot create an instance of an interface

    1.1 In C# and other object-oriented programming languages, an interface is a programming construct that indicates a type.

    1.2 An interface declaration contains public properties and methods which is meant to be implemented by a class.

    1.3 An interface declaration is not a class declaration and so it cannot be instantiated the way a class can, e.g. :

    public class MyClass {...}
    public interface MyInterface {...}
    
    MyClass my_class = new MyClass(); // This is OK.
    MyInterface m_type = new MyInterface();  // Error.
    

    1.3 Rather, an interface is meant to be derived from a class. The class that derives from the interface must implement the public properties and methods declared in that interface. The implementing class is also known as a concrete class.

    1.4 Note the following example :

    namespace DuanBritoInterfaces
    {
        public interface MyType
        {
            int IntegerProperty
            {
                get;
                set;
            }
    
            string StringProperty
            {
                get;
                set;
            }
    
            void Method01();
        }
    }
    

    Here, MyType is declared as an interface.

    Note the next example :

    public class MyClass : MyType
    {
        public MyClass()
        {
        }
    
        public int IntegerProperty
        {
            get
            {
                return m_IntegerProperty;
            }
    
            set
            {
                m_IntegerProperty = value;
            }
        }
    
        public string StringProperty
        {
            get
            {
                return m_StringProperty;
            }
    
            set
            {
                m_StringProperty = value;
            }
        }
    
        public void Method01()
        {
            Console.WriteLine("MyClass instance Method01().");
        }
    
        private int m_IntegerProperty = 0;
        private string m_StringProperty = "MyClass string";
    }
    

    Here, MyClass is a concrete class which implements MyType.

    Note the next example :

    public class MyOtherClass : MyType
    {
        public MyOtherClass()
        {
        }
    
        public int IntegerProperty
        {
            get
            {
                return m_IntegerProperty;
            }
    
            set
            {
                m_IntegerProperty = value;
            }
        }
    
        public string StringProperty
        {
            get
            {
                return m_StringProperty;
            }
    
            set
            {
                m_StringProperty = value;
            }
        }
    
        public void Method01()
        {
            Console.WriteLine("MyOtherClass instance Method01().");
        }
    
        private int m_IntegerProperty = 0;
        private string m_StringProperty = "MyOtherClass string";
    }

    Here MyOtherClass is another implementation of MyType.

    In short, there can be any number of concrete class which implement a specific interface and the bottom line is that an interface itself is not an implementation and it cannot be instantiated.

    2. Note the following example :

    static void DoTest()
    {
        // Load the assemby which defines the MyType interface.
        Assembly InterfaceAssembly = Assembly.LoadFrom("DuanBritoInterfaces.dll");
        // Obtain the MyType interface.
        Type InterfaceType = InterfaceAssembly.GetType("DuanBritoInterfaces.MyType");
    
        // Load the assembly which contains a class which implements MyType.
        Assembly ImplementationAssembly = Assembly.LoadFrom("DuanBritoImplementations.dll");
        // Obtain the MyClass type (it implements MyType).
        Type ImplementationType = ImplementationAssembly.GetType("DuanBritoImplementations.MyClass");
    
        // Create an instance of MyClass.
        var Instance = Activator.CreateInstance(ImplementationType);
    
        // Invoke the Method01() method. Note that we only call methods of the MyType interface.
        ImplementationType.InvokeMember("Method01", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, Instance, null);
    
        // Obtain the MyOtherClass type (it also implements MyType).
        ImplementationType = ImplementationAssembly.GetType("DuanBritoImplementations.MyOtherClass");
    
        // Create an instance of MyOtherClass.
        Instance = Activator.CreateInstance(ImplementationType);
    
        // Invoke the Method01() method. Note that we only call methods of the MyType interface.
        ImplementationType.InvokeMember("Method01", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, Instance, null);
    }

    2.1 In the above example, I create 2 assemblies : DuanBritoInterfaces.dll and DuanBritoImplementations.dll.

    2.2 In DuanBritoInterfaces.dll, I define only the MyType interface. In DuanBritoImplementations.dll, I defined 2 concrete classes MyClass and MyOtherClass both of which implements MyType.

    2.3 Notice that when I call Activator.CreateInstance() I use not the type of the interface but the types of the concrete classes.

    2.4 The other thing to note is that I only call methods of the MyType interface. MyClass and MyOtherClass may actually contain other methods but these are not part of MyType.

    2.5 Note finally that MyClass::Method01() and MyOtherClass::Method02() are different. And this is the whole point of why we have interfaces and implementations in the first place : to separate them so that a client (which uses an interface) is coded without prior knowledge of how an interface's methods will be implemented.

    2.6 Actual implementation should be decided at runtime and behavior can be different depending on which implementation eventually gets used.

    3. I hope this rather long post has been helpful.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by chriga Monday, September 1, 2014 6:32 AM
    • Marked as answer by Duan Brito Monday, September 1, 2014 5:56 PM
    Monday, September 1, 2014 5:11 AM
  • Hi,

    you cannot create an instance of an interface. You need some implementation of the interface to create an interface.

    Best regards


    Chris

    Code Samples: Code Samples
    Chrigas Blog: Chrigas Blog

    Monday, September 1, 2014 6:32 AM
  • I am unable to understand one thing that everyone is talking of interface implementation. But what is required to execute the original method of interface; as whenever I try to implement the method its seems local object calling local method. 

    Tuesday, August 8, 2017 6:41 AM