/* Inherit your application services from this class. */ public abstract class BookStoreAppService : ApplicationService { protected BookStoreAppService() { LocalizationResource = typeof(BookStoreResource); } }
public class Test { [JsonProperty("HelloWorld")] public string WorldHello { get; set; } }
public class MyAppService : BookStoreAppService { [HttpPost("HelloWorld")] public string HelloWorld(Test test) { return test.WorldHello; } }
using System; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.CSharp.Syntax; using System; using System.IO; using System.Linq; using System.Collections.Generic; using Microsoft.CodeAnalysis;
namespace ConsoleApp7 { class Program { static void Main(string[] args) { var texts = File.ReadAllText( "Test.cs" ); var tree = CSharpSyntaxTree.ParseText( texts ); var root = tree.GetRoot( ); var lines = File.ReadAllLines( "Test.cs" ); var members = root.DescendantNodes( ).OfType<MemberDeclarationSyntax>( ); foreach (var member in members) { var p = member as PropertyDeclarationSyntax;
if (p != null) { FileLinePositionSpan span = p.SyntaxTree.GetLineSpan( p.Span ); int lineNumber = span.StartLinePosition.Line;
//撈屬性名稱 var name = p.Identifier.Text; //https://stackoverflow.com/questions/35927427/how-to-create-an-attributesyntax-with-a-parameter var attrName = SyntaxFactory.ParseName( "JsonPropertyName" ); var arguments = SyntaxFactory.ParseAttributeArgumentList( $"(\"{p.Identifier.Text}\")" ); var attribute = SyntaxFactory.Attribute( attrName, arguments ); //MyAttribute("some_param")
var attributeList = new SeparatedSyntaxList<AttributeSyntax>( ); attributeList = attributeList.Add( attribute ); //[MyAttribute("some_param")] var list = SyntaxFactory.AttributeList( attributeList ); var newP = p.AddAttributeLists( list );