Creating an Event - accessors required?

Topics: APIs
Aug 3, 2014 at 10:18 AM
Hi all. I've been trying to insert an event into a class by suing a ICodeRefactoringProvider.

The problem is that is seem to me like there is no way to create an event without specifying its accessors. My code:
newDeclaration = newDeclaration.WithMembers(
                        newDeclaration.Members.Add(
                            SyntaxFactory.EventDeclaration(
                                new SyntaxList<AttributeListSyntax>(),
                                SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)),
                                SyntaxFactory.Token(SyntaxKind.EventKeyword),
                                SyntaxFactory.ParseTypeName("PropertyChangedEventHandler"),
                                null,
                                SyntaxFactory.Identifier("PropertyChanged"),
                                SyntaxFactory.AccessorList(
                                    SyntaxFactory.List(
                                        new[]
                                            {
                                                SyntaxFactory.AccessorDeclaration(
                                                    SyntaxKind.AddAccessorDeclaration,
                                                    SyntaxFactory.Block()),
                                                SyntaxFactory.AccessorDeclaration(
                                                    SyntaxKind.RemoveAccessorDeclaration,
                                                    SyntaxFactory.Block()),
                                            })))));
Result:
public event PropertyChangedEventHandler PropertyChanged
{
            add
            {
            }

            remove
            {
            }
}
This produces an event without a hitch, but both the accessors are empty. That is by design, since I don't want to add any custom logic in there. What I would like to see is a way to create an event like this:
 public event PropertyChangedEventHandler PropertyChanged;
I haven't found a way of doing this with the API. Is this possible? [Using 0.6 version with VS2013]
Aug 3, 2014 at 2:17 PM
It looks like you want EventFieldDeclarationSyntax, not EventDeclarationSyntax:
SyntaxFactory.EventFieldDeclaration(
    SyntaxFactory.VariableDeclaration(
        SyntaxFactory.ParseTypeName("PropertyChangedEventHandler"),
        SyntaxFactory.SeparatedList(new[] { SyntaxFactory.VariableDeclarator("PropertyChanged") })))
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
As usual, generating code with Roslyn is very verbose. I hope somebody will write a library that hides all this behind a nicer interface.
Marked as answer by mar3ek on 8/3/2014 at 8:17 AM
Aug 3, 2014 at 3:17 PM
Oh, I totally missed that, thank you! Regarding the verbosity... yeah, the code tends to be crazy to read, especially when emitting some more advanced syntax. Still, better that than nothing, right?

Again, thanks for the info, marking it as answer.