tags:

views:

658

answers:

3
+5  A: 

You should get error CS0759. Test case:

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

partial class MyClass
{
    partial void MyMethod2();
}

Compilation results:

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

Does that not do what you want it to?

Jon Skeet
Thanks Jon, sorry to put Q too fast. I had a typo in my code and that is why compiler was not flagging error.Anyways, thanks for responding fast. SO rocks!
isntn
No problem. There's no surer way of finding the bug yourself than posting it publicly first ;)
Jon Skeet
+2  A: 

In short, no; that is the point of partial methods - the declaring code doesn't need to know whether an implementation is provided or not.

Of course - you could just not declare the partial method: consume it assuming it exists; if you don't provide it, the compiler will complain of a missing method.

There is a hacky way to check at runtime (with partial methods), which is to have the other half update a ref variable:

partial void Foo(ref chk);

partial void Foo(ref chk) { chk++;}

(and verify it changes) - but in general, partial methods are designed to not know if they are called.

Another approach is a base-class with an abstract method - then it is forced by the compiler to be implemented.

Marc Gravell
Although the declaring code doesn't know whether or not it's actually being implemented, the compiler *is* smart enough to know "that I have partial method implemented without any declaration".
Jon Skeet
Yes, I was focusing more on the other half of the problem.
Marc Gravell
Fortunately I suspect in this kind of case (the name of a partial method changing) you don't get one problem without the other, so it's okay.
Jon Skeet
A: 

This is the whole purpose of partial methods. If the method is not implemented, it is removed without a trace, and without a warning.

One solution to this type of problem would be to use a double derived pattern in your code generation. This is used extensively by DSLTools and is quite powerful.

Write the following code by hand :

public class MyClassBase
{
    public abstract void MyMethod();
    //Put all other methods required by the class here.
}

public partial class MyClass : MyClassBase
{
   //This class is entirely empty! 
}

Generate the following code in magic.

public partial class MyClass
{
    public void MyMethod(){}
}

If someone fails to implememnt MyMythod() in the generated code, you will get a compiler error.

Jason Coyne