6 comments
Comment actions Permalink

Hello Alex,

I'm not sure if got your question correct, but the 'Invoke' template can be edited in the Templates Explorer. I've attached an image for a reference.

Please let me know if it helps or not.
Thanks.



Attachment(s):
screen415.png
0
Comment actions Permalink

No, this is not exactly...

Now generated invoker:

protected virtual void OnSomeEvent() {
      EventHandler handler = SomeEvent;
      if (handler != null) handler(this, EventArgs.Empty);
}


But I always want to get:

private void OnSomeEvent() {

      if (SomeEvent!= null) {
            SomeEvent(this, EventArgs.Empty);
      }
}


How can I always get this generation?

PS. there is tag "code"?

0
Comment actions Permalink

Alex,

Could you please describe a way you'd like to get (or are getting now) this generation?
One of the options is to create a custom Live Temlate for your needs, if you'd like to use it this way.

Thanks.

0
Comment actions Permalink

Your preferred code has a subtle race condition which could lead to a NullReferenceException. In multi-threaded code, it is possible that the last subscriber to the event could be removed between the "if (SomeEvent != null)" test and the "SomeEvent(this, EventArgs.Empty)" call. When the last subscriber is removed, the event delegate will become null, and the attempt to call it will result in an exception.

Have a look at the answers to this StackOverflow question for more information.

0
Comment actions Permalink

Richard Deeming, yes, I know. The code above is an example.

Alex Berezoutsky, now, I edit it manually. I would like to be able to edit the template generation handlers as in snippets =)
Hmm, how can snippet help me? After all, there will also have to manually edit Event name. And Event Handler name. Or not?

0
Comment actions Permalink

Alex,

You may use your own placeholders in custom Live Templates to do it in more easy way, however I'm not sure if it's possible to automatically add the correct naming as you'd like...

Thanks.

0

Please sign in to leave a comment.