类型XXXXXX中无法转换对象类型IXXXXX(.NET 4.0) [英] Unable to cast object of type XXXXXX to type IXXXXX (.NET 4.0)

查看:266
本文介绍了类型XXXXXX中无法转换对象类型IXXXXX(.NET 4.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释发生了什么给我?我有一个测试项目,测试我的服务的一个虚拟实例。在测试项目中,我只是引用dummyService.exe和System.SystemProcess DLL。

Can someone please explain what is happening to me? I have a test project that tests a dummy instance of my service. In the test project, I simply reference the dummyService.exe and System.SystemProcess dll.

在我dummyService的项目不过,我所引用的类库,它本身使用来自其他componentsn其他DLL以及其他项目在我的解决方案。

In my dummyService project however, I have referenced the class library, which itself uses other dlls from other componentsn as well as other projects in my solution.

现在的问题是,当我跑我的测试,异常抛出(第一次机会例外其中加载和dummyService工作的DLL)中,除了invalidcast异常(以下错误消息)。

The problem is that when I run my test, exceptions get thrown( First Chance exceptions for dlls which are loaded and working in the dummyService), in addition invalidcast exception (error message below).

无法转换型Export.CaseOutputGenerator的对象类型Export.ICaseOutputGenerator。 System.InvalidCastException被抓   消息=无法转换类型的对象Export.CaseOutputProcess.CustomCaseOutputGenerator到类型 Export.CaseOutputProcess.ICaseOutputGenerator。   来源= Export.CaseOutputProcess   堆栈跟踪:        在Export.CaseOutputProcess.CaseOutputGeneratoryFactory.GetCaseOutputGeneratorObject(字符串的AssemblyName,弦乐的className) 在C:\监视\ Export.CaseOutputProcess \ CaseOutputGeneratoryFactory.cs:行56 在Monitor.BOMock.GenerateCaseOutput(字符串OutputFolder,字符串iFile的,SEQNUM的Int32,DataTable中CaseSettings,串SettingFileName) 在C:\监视\ BOMock \ BOMock.cs:行1069 在Monitor.BOMock.Handling()在C:\监视\ BOMock \ BOMock.cs:行492的InnerException:

Unable to cast object of type 'Export.CaseOutputGenerator' to type 'Export.ICaseOutputGenerator'. System.InvalidCastException was caught Message=Unable to cast object of type 'Export.CaseOutputProcess.CustomCaseOutputGenerator' to type 'Export.CaseOutputProcess.ICaseOutputGenerator'. Source=Export.CaseOutputProcess StackTrace: at Export.CaseOutputProcess.CaseOutputGeneratoryFactory.GetCaseOutputGeneratorObject(String assemblyName, String className) in C:\Monitor\Export.CaseOutputProcess\CaseOutputGeneratoryFactory.cs:line 56 at Monitor.BOMock.GenerateCaseOutput(String OutputFolder, String iFile, Int32 seqNum, DataTable CaseSettings, String SettingFileName) in C:\Monitor\BOMock\BOMock.cs:line 1069 at Monitor.BOMock.Handling() in C:\Monitor\BOMock\BOMock.cs:line 492 InnerException:

     public static ICaseOutputGenerator GetCaseOutputGeneratorObject(string assemblyName, string className)
            {
                ICaseOutputGenerator customeOutputGen = null;

                var obj = GetObject(assemblyName, className);
                if (obj != null)
                    caseOutputGen = (ICaseOutputGenerator)obj; // FAILS HERE
                return caseOutputGen;
            }

   private static object GetObject(string fullName, string className)
        {
            try
            {
                Type caseOutputGen = null;
                var localAssembly = Assembly.LoadFrom(fullName);
                foreach (var testType in localAssembly.GetTypes())
                {
                    if (!testType.FullName.EndsWith(className, StringComparison.InvariantCultureIgnoreCase)) continue;
                    caseOutputGen = testType;
                    break;
                }
                if (caseOutputGen == null) return null;

                var obj = Activator.CreateInstance(caseOutputGen);
                return obj;
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception("Failed to load assembly: " + Environment.NewLine + fullName, ex);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to load assembly: " + Environment.NewLine + fullName, ex);
            }
        }

在哪里的AssemblyName是路径的dll文件加载和类名恰好是类来创建一个实例的名称。

Where assemblyName is the Path to the dll file to load and className happens to be the name of the class to create an instance of.

在code,如你所见,使用反射我加载大会提供的(字符串的AssemblyName)的 http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx ,然后使用反射再次,我然后创建包含在已加载组件的类名(字符串的className)的一个实例。 http://msdn.microsoft.com/en-us/library /system.activator.createinstance.aspx

In the code, as you see, using reflection I load the assembly at the assemblyName PATH provided (String assemblyName) http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx , and then using reflection again, I then create an instance of the className (String className ) contained in the loaded assembly. http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

我该如何解决这个问题吗?我不希望有参考我的所有的dll中的测试项目。我该如何解决,或解决这个问题,请?先谢谢了。

How do I remedy this problem please? I don't want to have to reference all my dlls in the test project. How do I get around or solve this problem please?? Thanks in advance.

推荐答案

根据该堆栈跟踪,它看起来像其中居住类不被发现的组件。如果你只需要添加引用到编译EXE,你可能不会得到其他库与它一起。我认为你有两个选择:

Based on that stack trace, it looks like the assembly where the type lives is not being found. If you just add the reference to the compiled exe, you're probably not going to get the other libraries along with it. I think you've got a couple of choices:

  1. 来吧,咬咬牙:在您的测试项目中添加引用到其他库。他们通常不传递的:仅仅因为你的服务知道它们并不一定意味着你的测试的组件知道他们以及
  2. 添加后编译步骤,测试的项目,超过其他组件的副本,以便他们可以通过运行测试应用程序域发现。
  3. 使用依赖注入和控制容器的倒置。有不少在那里,但温莎城堡,StructureMap和团结浮现在脑海中。斯科特Hanselman的得到了他们的大名单在他的博客: http://www.hanselman.com/blog /ListOfNETDependencyInjectionContainersIOC.aspx
  1. Go ahead and bite the bullet: add the references to the other libraries in your test project. They're typically not transitive: just because your service knows about them doesn't necessarily follow that your test's assembly knows about them as well.
  2. Add a post-compilation step to your test's project that copies over the other assemblies so that they can be found by the app domain running your test.
  3. Use dependency injection and an inversion of control container. There are quite a few out there, but Castle Windsor, StructureMap and Unity come to mind. Scott Hanselman's got a great list of them on his blog: http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

这篇关于类型XXXXXX中无法转换对象类型IXXXXX(.NET 4.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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