InvalidCastException,DLL和C#中的反思问题 [英] InvalidCastException, DLLs and reflection trouble in C#

查看:202
本文介绍了InvalidCastException,DLL和C#中的反思问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用预编译的DLL进行反射,以便为DLL中的类实例化一个接口。我试过这本书,但不行。当我尝试执行以下操作时,它会抛出InvalidCastException:

I'm trying to use a precompiled DLL with reflection, to instantiate an interface for my class that is in the DLL. I tried by the book, but it won't work. It throws InvalidCastException when I try to do something like:

ICompute iCompute = (ICompute)Activator.CreateInstance(type);

课程类型是我的类,实现ICompute接口。我被卡住了,不知道该怎么办完整的代码如下:

Where type of course is my class that implements ICompute interface. I'm stuck and don't know what to do. The complete code follows:

这是DLL内容:

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

namespace ConsoleApplication18
{
    public class ClassThatImplementsICompute : ICompute
    {
       public int sumInts(int term1, int term2)
       {
           return term1 + term2;
       }

       public int diffInts(int term1, int term2)
       {
           return term1 - term2;
       }
    }
}

实际程序: p>

The actual program:

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

using System.Reflection;

namespace ConsoleApplication18
{

    public interface ICompute
    {
        int sumInts(int term1, int term2);
        int diffInts(int term1, int term2);
    }



    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Loading dll...");
            Assembly assembly = Assembly.LoadFrom("mylib.dll");

            Console.WriteLine("Getting type...");
            Type type = assembly.GetType("ConsoleApplication18.ClassThatImplementsICompute");
            if (type == null) Console.WriteLine("Could not find class type");

            Console.WriteLine("Instantiating with activator...");
            //my problem!!!
            ICompute iCompute = (ICompute)Activator.CreateInstance(type);

            //code that uses those functions...



        }
    }
}

任何人都可以帮我吗?谢谢!

Can anyone help me? Thanks!

推荐答案

问题在于如何使用 Assembly.LoadFrom()加载程序集,

LoadFrom()将程序集加载到不同的上下文中您想要投放的 ICompute 界面。如果可能,请尝试使用 Assembly.Load()。即将装配体放入垃圾桶/探测路径文件夹,并以完整的名称加载。

LoadFrom() load the assembly into different context compared to context of the ICompute interface you are trying to cast to. Try to use Assembly.Load() instead if possible. i.e. put the assembly into the bin / probing path folder and load by the full strong name.

某些引用:
http://msdn.microsoft.com/en-us/library/dd153782.aspx
< a href =http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx =nofollow> http://blogs.msdn.com/b/suzcook/ archive / 2003/05/29 / 57143.aspx (请参阅LoadFrom的缺点)

Some references: http://msdn.microsoft.com/en-us/library/dd153782.aspx http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx (see the disadvantage bit for LoadFrom)

这篇关于InvalidCastException,DLL和C#中的反思问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆