Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Partial methods and Action, non implemented body
    primarykey
    data
    text
    <p>I want to call a partial method from outside of the declaring class. This is not allowed as partial methods are implicitly private. You can not set a delegate to point to a partial so I am proposing the following:</p> <pre><code> public partial class MyClass { AnotherClass _anotherClass; public MyClass () { _anotherClass = new AnotherClass(); _anotherClass.Method = new Action(() =&gt; { this.Method(); }); } partial void Method(); //sometimes this method will be implemented //partial void Method() { //do something //} } public class AnotherClass { public Action Method { get; set;} public void SomeOtherMethod(){ this.Method(); } } </code></pre> <p>The classes are tightly coupled, they are in a parent child relationship. I want the parent to have a method that it can override to know about property changes on the child. I could attach event handlers to each of the children however the child already knows about its parent so having the child inform the parent directly seems like the way to go. Except when the parent <strong>does not care</strong> which is why I want to be able to implement a partial if I do care. This question is basically about ease of programming vs performance. I know I could attach event handlers only to the situation where I do care, but with the implementation as above I can generate all the partial methods and only implement the partials if I care.</p> <p>My question relates to the times that the partial method <code>Method()</code> is not implemented. When the Action method is called will the compiler optimise it away as its body is empty? If I get a heap of these Action calls being made could I suffer a performance penalty? Is there a better way to get the same functionality?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload