从动态创建的对象加载程序集,并投它的接口(.NET 2.0) [英] Create object from dynamically load assembly and cast it to interface (.NET 2.0)

查看:97
本文介绍了从动态创建的对象加载程序集,并投它的接口(.NET 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态加载大会的问题,铸造它的接口。哪里是我的错?

I have a problem with dynamically loaded assemblys and casting it to interface. Where is my mistake?

主要的应用程序(加载插件):

Main app (load plugins):

namespace Console_IFce_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to find IPlugin library...");
            Console.ReadKey();            

            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll");
            Console.WriteLine("Loading assembly: {0}", Path.GetFileName(files[0]));

            Assembly asm = Assembly.LoadFrom(files[0]);
            //Trying this, but still have problems
            //Assembly asm = Assembly.Load(File.ReadAllBytes(files[0])); 

            foreach (Type t in asm.GetTypes())
            {
                Console.WriteLine("Searching in type {0}... ", t.FullName);

                foreach (Type iface in t.GetInterfaces())
                {
                    Console.WriteLine("Interface found: {0}", iface.FullName);
                }

                if (t is IPlugin)
                {
                    Console.WriteLine("1 - IPlugin found!");
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
                    return;
                }

                if (typeof(IPlugin).IsAssignableFrom(t))
                {
                    Console.WriteLine("2 - IPlugin found!");
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
                    return;
                }
            }

            Console.WriteLine("All operations done! Press any key to exit...");
            Console.ReadKey();
        }
    }
}

接口:

namespace Console_IFce_Test
{
    interface IPlugin
    {
        int GetZero();
    }
}

和插件:

namespace Library
{
    public class Plugin : Console_IFce_Test.IPlugin
    {
        public int GetZero()
        {
            return 0;
        }
    }
}

在以.exe目录 - 只有1 .dll文件(插件)。因此,它的输出:

In directory with .exe - only 1 .dll (plugin). So, it's output:

Press any key to find IPlugin library... 
Loading assembly: Library.dll 
Searching in type Console_IFce_Test.IPlugin... 
Searching in type Library.Plugin... 
Interface found: Console_IFce_Test.IPlugin 
All operations done! Press any key to exit...

您看,该程序发现IPlugin界面组件,但是当我试图把它与接口相比较(二条件语句) - 他们返回false。如果我试图手动施放它 - 它返回异常不能投

You see, that program found IPlugin interface in assembly, but when i trying to compare it with interface (two conditional statements) - they return false. And if i trying to manually cast it - it returns exception "Can't cast".

我发现了类似的问题:<一href="http://stackoverflow.com/questions/3623358/two-types-not-equal-that-should-be/3624044#3624044">Two类型不等于应该和答案写作者:

I found similar problem: Two Types not equal that should be , and author of answer write:

在同一类/由不同的应用程序域[.NET]或类加载类型   装载机【JAVA]不会比较平等的,不分配给/从   彼此直接

The same class / type loaded by different app domains [.NET] or class loaders [Java] will not compare equal and are not assignable to/from each other directly.

但我不明白我应该怎么办?而如何?

But i can't understand what should i do? And how?

推荐答案

您需要以某种方式使.NET加载组件,以相同的情况下才能够做到的类型之间的倒在这些程序集。

You need to somehow make .NET to load the assemblies to the same context to be able to do a cast between the types in those assemblies.

1-把组件在应用路径或GAC使得负载(不是LoadFrom)函数会发现它

1- Put the assembly in the application path or GAC so that the Load (not the LoadFrom) function will find it.

2 - 创建一个应用程序域和所做的工作都在那里。你可以完全控制那些正在寻找一个应用程序域的组件。

2- Create an app domain and do all of the work there. You can fully control where the assemblies are being searched for an app domain.

3使用外接的模式。

阅读这篇文章以了解更多有关你的选择:最佳实践集加载

Read this article to learn more about your options: Best Practices for Assembly Loading

这篇关于从动态创建的对象加载程序集,并投它的接口(.NET 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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