TypeLoadException说'不执行“,但它的实现 [英] TypeLoadException says 'no implementation', but it is implemented

查看:199
本文介绍了TypeLoadException说'不执行“,但它的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我花了几个小时,昨天这个问题的战斗,找不到任何解决方案,在这里或其他地方,所以我加入这个在这里为别人谁拥有这个问题。)

我已经得到了我们的测试机器上的一个非常奇怪的错误。错误是:

I've got a very weird bug on our test machine. The error is:

System.TypeLoadException:方法SetShort'类型'DummyItem从程序集ActiveViewers(......)不具有实施

我只是不明白为什么。 SetShort 是那里的 DummyItem 类,我甚至重新编译版本写入到事件日志刚确保它不是一个部署/版本问题。奇怪的是,调用code甚至不叫 SetShort 方法。

I just can't understand why. SetShort is there in the DummyItem class, and I've even recompiled a version with writes to the event log just to make sure that it's not a deployment/versioning issue. The weird thing is that the calling code doesn't even call the SetShort method.

推荐答案

注意 - 如果这个答案不帮你,请走的时候通过其他的答案,人们纷纷加入到向下滚动因为

NOTE - If this answer doesn't help you, please take the time to scroll down through the other answers that people have added since.

简短的回答

这可以,如果你在一个组件添加一个方法的接口,然后到另一个组件实现类发生,但在重建实现装配而不引用接口组件的新版本。

This can happen if you add a method to an interface in one assembly, and then to an implementing class in another assembly, but you rebuild the implementing assembly without referencing the new version of the interface assembly.

在这种情况下,实现了DummyItem来自另一组件的接口。所述SetShort方法最近被添加到两个接口和DummyItem - 但含有DummyItem组件被重建引用接口组件的previous版本。所以SetShort方法是有效地存在,但没有魔酱它连接到接口的对应方法。

In this case, DummyItem implements an interface from another assembly. The SetShort method was recently added to both the interface and the DummyItem - but the assembly containing DummyItem was rebuilt referencing the previous version of the interface assembly. So the SetShort method is effectively there, but without the magic sauce linking it to the equivalent method in the interface.

龙的答案

如果你想尝试重现此,请尝试以下操作:

If you want to try reproducing this, try the following:

  1. 创建一个类库项目:的InterfaceDef,加上只有一个班,并建立:

  1. Create a class library project: InterfaceDef, add just one class, and build:

public interface IInterface
{
    string GetString(string key);
    //short GetShort(string key);
}

  • 创建第二个类库项目:实施(带独立解决方案),复制InterfaceDef.dll到项目目录,并添加作为文件的引用,只是添加一个类,并建立:

  • Create a second class library project: Implementation (with separate solution), copy InterfaceDef.dll into project directory and add as file reference, add just one class, and build:

    public class ImplementingClass : IInterface
    {
        #region IInterface Members
        public string GetString(string key)
        {
            return "hello world";
        }
    
    
    
    //public short GetShort(string key)
    //{
    //    return 1;
    //}
    #endregion
    

    }

    }

  • 创建第三个,控制台项目:客户端code,两个DLL复制到该项目目录,添加文件引用,并添加以下code到Main方法:

  • Create a third, console project: ClientCode, copy the two dlls into the project directory, add file references, and add the following code into the Main method:

     IInterface test = new ImplementingClass();
     string s = test.GetString("dummykey");
     Console.WriteLine(s);
     Console.ReadKey();
    

  • 运行code一次,控制台说的Hello World

  • Run the code once, the console says "hello world"

    取消对code两个DLL项目和重建 - 复制回两个DLL到客户端code项目,重建并重新运行。试图实例化ImplementingClass时发生TypeLoadException。

    Uncomment the code in the two dll projects and rebuild - copy the two dlls back into the ClientCode project, rebuild and try running again. TypeLoadException occurs when trying to instantiate the ImplementingClass.

    这篇关于TypeLoadException说'不执行“,但它的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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