user2176493
user2176493

Reputation: 31

Cast a class object to an interface object using reflection C#

There have two dll's namely a) lib1 b) lib2 These two library are loaded using reflection( as against to adding a direct reference in visual studio). I'm creating an object of a class , then want to type cast that object to the type of the interface (interface being in the dll loaded in the main program). I get an error saying type mismatch. Any possible solution to this problem.

Here is my code block:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Interfaceconversion
{
    class Program
    {
        public static object classobj;
        public static object interfaceobj;
        static void Main(string[] args)
        {

            // Loading assembley 1
            Assembly assembly1 = Assembly.LoadFrom(@"D:\WCFService\Aug9\Interfaceconversion\Lib1\bin\Debug\Lib1.dll");
            Type[] type1 = assembly1.GetTypes();          
            foreach (Type item in type1)
            {
                if (item.FullName.ToString() == "Lib1.Class1")
                {
                    classobj = Activator.CreateInstance(item);

                }
            }

            // Loading assembly 2
            Assembly assembly2 = Assembly.LoadFrom(@"D:\WCFService\Aug9\Interfaceconversion\Lib2\bin\Debug\Lib2.dll");
            Type[] type2 = assembly2.GetTypes();
            Type libtype = type2[1];

            foreach (Type item in type2)
            {

                if (item.FullName.ToString() == "Lib2.Ilib2Interface1")
                {

            TODO: cast the object "classobj " to type  Lib2.Ilib2Interface1   
                    interfaceobj = classobj as item ;
                }
            }
            #region old code
        }
    }

Lib2 dll's code is : lib2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lib2
{        
    interface Ilib2Interface1
    {
        void lib2disp1();
    }
}

Lib1 code is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lib1
{        
    interface ISttutil
    {
        void displayutil1();
        void displayutil2();
    }

    interface Isttinterface
    {
        void displayinterface1();
        void displayinterface2();
    }
}

Upvotes: 1

Views: 2768

Answers (1)

user2717675
user2717675

Reputation:

We don't see lib1.Class1 in the example given, but provided it derives from the interface you want to cast it to, something like this should work:

lib1:

using lib2;
using System;

namespace lib1
{
    public class Class1 : IInterface1
    {
        public void MethodOne ( )
          {
            Console.WriteLine ( "MethodOne called!" );
        }
    }
}

lib2:

namespace lib2
{
    public interface IInterface1
    {
        void MethodOne ( );
    }
}

Main:

using lib2;
using System;
using System.IO;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main ( string [ ] args )
          {
              var fileInfo = new FileInfo ( @".\lib1.dll" );
              var assembly = Assembly.LoadFile ( fileInfo.FullName );
            var obj = assembly.CreateInstance ( "lib1.Class1" ) as IInterface1;
            if ( obj != null ) obj.MethodOne ( );
                Console.ReadLine ( );
        }
    }
}

Upvotes: 1

Related Questions