1

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();
    }
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
user2176493
  • 31
  • 2
  • 7
  • Which method are you using to load the assembly? I remember there is a problem with one of them. – Matthias Aug 26 '13 at 09:46
  • 1
    Does `Lib1.Class1` implement `Lib2.Ilib2Interface1`? – Fredrik Mörk Aug 26 '13 at 09:47
  • @ WinSharp93 I am using Assembly.LoadFrom() method to load the assembly. – user2176493 Aug 26 '13 at 09:50
  • 1
    Ummm... what's the point of trying to cast it? Casting pretty much _requires_ to have compile-time references to the types. Trying to cast to a runtime type has very little (any?) application. Perhaps you should explain what your end-goal is here. EDIT: Not to mention that the `Class1`, from the code you have doesn't implement the interface, so any cast would fail anyway. I'm not sure what you're trying to achieve here. – Chris Sinclair Aug 26 '13 at 09:52
  • possible duplicate of [Difference between LoadFile and LoadFrom with .NET Assemblies?](http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies) – Dennis Aug 26 '13 at 09:52
  • What's the use of casting like this? Since you don't compile against Lib2, you cannot declare variables of type ILib2Interface1 in your program. Neither the type of the actual object nor the reference is affected by casting (try `ReferenceEquals ((IEnumerable) "foo", "foo")`). If you want to check whether Class1 implements ILib2Interface1, use IsAssignableFrom. By the way, it is much better to use Assembly.GetType(string) than to loop over all types. – Anton Tykhyy Aug 26 '13 at 09:53
  • Also, `Type libtype = type2[1];` is a horrible idea because the order of types in a module is not stable. `type.FullName` is already a string, there is no point calling `ToString` on it. – Anton Tykhyy Aug 26 '13 at 10:01
  • @ Anton yes i totally agree with you, thank you for the inputs about the coding standards. – user2176493 Aug 26 '13 at 10:03
  • @ Dennis the link provided by you dint solve the problem .. – user2176493 Aug 26 '13 at 11:13

1 Answers1

1

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 ( );
        }
    }
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
  • @ Michael Thank you for the solution, but i cannot directly add a reference to the dll's (Using lib2) as the dll's are in some other directory, my main goal is to type cast it by just using reflection.. is it possible to do so.. – user2176493 Aug 26 '13 at 10:25
  • @user2176493 I misunderstood. I didn't realize you could add a reference to neither assembly. –  Aug 26 '13 at 10:34
  • 1
    What do you intend to do with the object after the cast? – Lasse V. Karlsen Aug 26 '13 at 10:34
  • @ lasse after casting the object i would be subscribing to the events defined in the interface . which would be done subsequently .. – user2176493 Aug 26 '13 at 10:39
  • 1
    @user2176493: A) You can't cast it without having the assembly referenced at compile-time. B) Casting won't actually do anything to the object anyway. C) You can still wire your event handlers via reflection without casting. – Chris Sinclair Aug 26 '13 at 12:05
  • @ chris ok let me try to call the event handlers via reflection without casting, thanks ... – user2176493 Aug 26 '13 at 12:11