【问题标题】:Removing a type interface breaks exe's usage of dll (.NET)删除类型接口会破坏 exe 对 dll (.NET) 的使用
【发布时间】:2023-03-03 17:39:01
【问题描述】:

exe 调用 dll,但未明确使用已删除的接口。为什么旧 DLL 编译的 EXE 不能在新 DLL 运行时运行?

编译器必须保留有关类型的额外信息,而不仅仅是使用什么。

EXE

void main()
{
    new Foo().PrintHello();
}

旧 DLL:

public class Foo : IOldInterface
{
   public void PrintHello()
   {
        Console.WriteLine("Hello");
   }
   public int Something { get { return 123; } }
}

public interface IOldInterface
{
   int Something { get; }
}

新建 DLL

public class Foo
{
   public void PrintHello()
   {
        Console.WriteLine("Hello");
   }
}

【问题讨论】:

  • 我无法重现您提供的代码的问题 - 但我不得不更改它以使其编译这一事实表明您可能还没有使用此代码进行测试。请提供一个minimal reproducible example 来实际演示问题。
  • 很抱歉。你说得对。我是从一个更大的代码库中推断出来的,并没有编译它。在有效放弃此问题之前更新了帖子。

标签: .net dll types interface compilation


【解决方案1】:

简而言之,简单地从一个类型中删除一个接口并不会使它与另一个使用它的库不兼容(只要方法原型没有改变,以及这些原因:https://stackoverflow.com/a/806510/578879

我原以为这不是问题,但我自己没有做这个简单的测试就提出了这个问题。我的大型项目中的问题很可能是由于构建问题(不知何故有一个旧版本的 exe,它实际上直接引用了 IOldInterface - 因此 IOldInterface 未找到异常)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多