c# - 部分方法 C# 3.0 的问题

标签 c# .net c#-3.0

你知道下面问题的答案吗?

Let us say, it MyMethod() is declared as partial method in MyPartialClass in MyPartialClass.cs. I have also provided body of MyMethod() in MyPartialClass in MyPartialClass2.cs. I use a problem without answer“Magic” code generator which has actually generated MyPartialClass.cs, let us say based on some DB schema. Some innocent guy changes schema for good and then runs “Magic”. MyPartialClass.cs is re-generated but with MyMethod2() instead of MyMethod() declaration. Think of me. I am thinking that I have implemented MyMethod() which is used by “Magic” but in reality, “Magic” is using MyMethod2(). C# compiler does not tell me that I have partial method implemented without any declaration and my implementation is not used at all!

Any solution?

我认为这是一个没有答案的问题。

编辑 我得到了答案:-)。我的代码中有一个拼写错误,这就是编译器没有标记错误的原因。乔恩已经指出了这一点。

最佳答案

您应该收到错误 CS0759。测试用例:

partial class MyClass
{
    partial void MyMethod()
    {
        Console.WriteLine("Ow");
    }
}

partial class MyClass
{
    partial void MyMethod2();
}

编译结果:

Test.cs(6,18): error CS0759: No defining declaration found for implementing 
declaration of partial method 'MyClass.MyMethod()'

这不是您想要的吗?

关于c# - 部分方法 C# 3.0 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738580/

相关文章:

c# - 在 C# 中将图像插入到 RTF 文档中

c# - 你如何获得链表中数字的索引?

c# - IEnumerable<T> 到 List<T>

c# - 关于 Web Api 的 Signalr Hub 约定

c# - 将 Azure AD B2C 登录用户链接到自定义数据库表用户

c# - 具有两种可能类型的通用属性

c# - 对象作为字典中的键

c# - 学习 .NET 3.5 的资源

c# - 为什么 main 不等到异步方法完成?

c# - Entity Framework 中的 Linq 查询优化