Consider the following class in a file "MyClass.cs"
using System;
public class MyClass : Entity<long>
{
public long Id
{
get;
set;
}
[Required]
public string Name
{
get;
set;
}
public string Slug
{
get;
set;
}
public DateTime CreatedOn
{
get;
private set;
}
public DateTime UpdatedOn
{
get;
private set;
}
/* ... */
}
Currently I manually create data contract classes looking as follows:
[DataContract(Namespace = "http://example.com/", Name = "MyClass")]
public sealed class MyClass
{
[DataMember(EmitDefaultValue = false, Name = "Id")]
public long Id
{
get;
set;
}
[DataMember(EmitDefaultValue = false, Name = "Name", IsRequired = true)]
public string Name
{
get;
set;
}
[DataMember(EmitDefaultValue = false, Name = "Slug")]
public string Slug
{
get;
set;
}
[DataMember(EmitDefaultValue = false, Name = "CreatedOn")]
public DateTime CreatedOn
{
get;
set;
}
[DataMember(EmitDefaultValue = false, Name = "UpdatedOn")]
public DateTime UpdatedOn
{
get;
set;
}
}
I'd like to use Roslyn to rewrite "MyClass.cs" so its looks like the class I create by hand. Currently I have the following:
using System;
using System.IO;
using Roslyn.Compilers.CSharp;
internal class Program
{
private static void Main()
{
var reader = new StreamReader(@"....MyClass.cs");
var source = reader.ReadToEnd();
var tree = SyntaxTree.ParseCompilationUnit(source);
var rewriter = new MyRewriter();
var newRoot = rewriter.Visit(tree.Root);
Console.WriteLine(newRoot.Format());
}
}
public class MyRewriter : SyntaxRewriter
{
protected override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
var declaration = (TypeDeclarationSyntax) base.VisitClassDeclaration(node);
return ((ClassDeclarationSyntax) declaration).Update(
declaration.Attributes,
Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword), Syntax.Token(SyntaxKind.SealedKeyword)),
declaration.Keyword,
declaration.Identifier,
declaration.TypeParameterListOpt,
null,
declaration.ConstraintClauses,
declaration.OpenBraceToken,
declaration.Members,
declaration.CloseBraceToken,
declaration.SemicolonTokenOpt);
}
protected override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
var typeSyntax = node.Type;
if (node.Identifier.ValueText == "Id")
{
typeSyntax = Syntax.IdentifierName("string");
}
var newProperty = Syntax.PropertyDeclaration(
modifiers: Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword)),
type: typeSyntax,
identifier: node.Identifier,
accessorList: Syntax.AccessorList(
accessors: Syntax.List(
Syntax.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration,
semicolonTokenOpt: Syntax.Token(SyntaxKind.SemicolonToken)),
Syntax.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration,
semicolonTokenOpt: Syntax.Token(SyntaxKind.SemicolonToken))
)
)
);
return newProperty;
}
}
I have been trying to find a way to add the DataMember and DataContract custom attributes to MyClass but have been unsuccessful. How does one add the custom attributes?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…