将程序集与部分类链接

发布于 2024-12-20 07:31:43 字数 1136 浏览 4 评论 0原文

我有一个名为 common 的公共项目。里面有一个名为 ExtensionMethods 的类,由遗留应用程序使用。我想将类分成多个文件,但保持名称相同。因此,我没有使用 public static class ExtensionMethods {},而是使用了许多 public staticpartial class ExtensionMethods {}

当我删除从分部类创建的新 dll 时,使用该 dll 的旧应用程序之一收到 Method not found: 异常。是什么原因造成的?文件名信息是否以某种方式嵌入到 dll 中?部分类是否会使新旧 dll 不兼容?这是怎么回事?该 dll 没有版本控制,并且我们没有使用 gac。它只是一个简单的dll,与exe 放在同一目录中。

更新: 感谢大家迄今为止提出的建议。这是我到目前为止检查过的内容
相同的命名空间:检查。
相同的签名:检查。
仍然公开:检查。

我可以构建旧版应用程序,并且构建时不会出现错误。

是的,异常告诉我它是什么方法。未找到方法:(

'System.Collections.Generic.IEnumerable\`1<!!0> CompanyName.Common.ExtentionMethods.Distinct(System.Collections.Generic.IEnumerable\`1<!!0>, System.Func\`3<!!0,!!0,Boolean>)'.

遗憾的是,代码中的“Extension”拼写错误。)

我将开始使用大家建议的一些工具来进一步查找。

旧dll new dll

答案: 用户错误。我认为我复制的 dll 并不是我认为我正在构建的新 dll。我不知道这是需要重建还是源代码控制问题或者只是我的错误。无论如何,下面的两个答案都是正确的,部分类不是问题,并且两个答案都有帮助,但最终,我认为是反射器解决方案引导我找到了问题。谢谢大家。

I have a common project called common. Inside is a class called ExtensionMethods which is used by legacy applications. I want to break apart the class into multiple files, but keep the name the same. So instead of public static class ExtensionMethods {} I have many public static partial class ExtensionMethods {}.

When I drop the new dll created from the partial classes, I am getting a Method not found: exception by one of the legacy applications using the dll. What is causing this? Is the filename infomation embedded in the dll somehow? Do partial classes make the old and new dll incompatible? What's the deal? The dll is not versioned, and we are not using the gac. It's just a simple dll placed in the same directory as the exe.

Update:
Thank you everyone for the suggestions so far. Here's what I've checked so far
same namespace: check.
same signatures: check.
still public: check.

I can build the legacy app and it builds without error.

Yes, the exception tells me what method it is. Method not found:

'System.Collections.Generic.IEnumerable\`1<!!0> CompanyName.Common.ExtentionMethods.Distinct(System.Collections.Generic.IEnumerable\`1<!!0>, System.Func\`3<!!0,!!0,Boolean>)'.

(Sadly, the misspelling of 'Extension' is the way it is in the code.)

I will begin using some tools suggested by you all to look further.

old dll
new dll

Answer: User error. The dll I thought I copied wasn't the new dll I thought I was building. I can't figure if it was a rebuild that was needed or a source control problem or just my mistake. Anyways, both answers below were correct, partial classes wasn't the problem, and both answers were helpful, but in the end, I think it was the reflector solution that lead me to finding the problem. Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肤浅与狂妄 2024-12-27 07:31:43

分部类只是编译器的一个技巧,仅此而已——最终的汇编中没有任何有意义的区别。

所以,我认为你的实际问题出在其他地方。

检查您收到的异常的详细信息并查看该特定方法的来源。

另外,使用检查工具,例如 ReflectordotPeek 来搜索程序集本身中的类/方法,您应该很快就能找到问题。

Partial classes are a compiler trick, nothing more - there's no meaningful difference in the final assembly.

So, I think that your actual problem lies elsewhere.

Check the details of the exception you're getting and review the source for that particular method.

Also, grab an inspection tool like Reflector or dotPeek to search for the class/method in the assembly itself and you should find the problem fairly quickly.

維他命╮ 2024-12-27 07:31:43

假设所有类都在同一名称空间中声明,那么应该绝对没问题。检查:

  • 这些类都位于同一命名空间中(因此在生成的程序集中只有一个具有该名称的类,而不是不同命名空间中的多个类)
  • 您的方法签名完全为它们是之前的 - 参数类型、方法名称等无法更改
  • 这些方法仍然是公共的

如果您尝试针对新 DLL 构建遗留​​应用程序,会发生什么情况?这应该显示编译时缺少的方法,这应该可以提示您哪里出错了。请注意,“未找到方法”异常应该告诉您无论如何都没有找到哪个方法......

Assuming all the classes are declared in the same namespace, it should be absolutely fine. Check:

  • That the classes are all in the same namespace (so you've only got one class with that name in the resulting assembly, rather than multiple classes in different namespaces)
  • That your method signatures are exactly as they were before - the parameter types, method names etc can't change
  • That the methods are still public

What happens if you try to build the legacy application against your new DLL? That should show the method being missing at compile-time, which should give you a hint about where you're going wrong. Mind you, presumably the "Method not found" exception should tell you which method isn't being found anyway...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文