【问题标题】:Cast a class object to an interface object using reflection C#使用反射 C# 将类对象转换为接口对象
【发布时间】:2013-08-26 09:45:13
【问题描述】:

有两个dll,即 一)lib1 b) 库2 这两个库是使用反射加载的(相对于在 Visual Studio 中添加直接引用)。我正在创建一个类的对象,然后想将该对象类型转换为接口的类型(接口在主程序中加载的 dll 中)。我收到一条错误消息,说类型不匹配。此问题的任何可能解决方案。

这是我的代码块:

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 的代码是: 库2

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

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

Lib1 代码是:

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();
    }
}

【问题讨论】:

  • 您使用哪种方法加载程序集?我记得其中一个有问题。
  • Lib1.Class1 是否实现了Lib2.Ilib2Interface1
  • @WinSharp93 我正在使用 Assembly.LoadFrom() 方法来加载程序集。
  • Ummm...尝试投射它有什么意义?转换几乎 需要 具有对类型的编译时引用。尝试转换为运行时类型几乎没有(任何?)应用程序。也许你应该在这里解释你的最终目标是什么。编辑:更不用说Class1,来自你的代码没有实现接口,所以任何演员都会失败。我不确定你想在这里实现什么。

标签: c# reflection


【解决方案1】:

我们在给出的示例中没有看到 lib1.Class1,但如果它派生自您想要将其强制转换为的接口,那么这样的事情应该可以工作:

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 ( );
    }
}

主要:

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 ( );
        }
    }
}

【讨论】:

  • @Michael 感谢您的解决方案,但我无法直接添加对 dll 的引用(使用 lib2),因为 dll 位于其他目录中,我的主要目标是仅使用反思..有可能这样做吗..
  • @user2176493 我误解了。我没有意识到您可以添加对这两个程序集的引用。
  • 你打算在演员表之后对这个对象做什么?
  • @lasse 在转换对象后我将订阅接口中定义的事件。这将在随后完成..
  • @user2176493: A) 如果没有在编译时引用的程序集,就不能转换它。 B)无论如何,铸造实际上不会对对象做任何事情。 C)您仍然可以通过反射连接您的事件处理程序而无需强制转换。
猜你喜欢
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多