mirror of
https://github.com/AlexMacocian/WpfExtended.git
synced 2026-07-21 01:19:29 +00:00
7d3c2fbb9b
Testing project for source generator
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace System.Extensions.Templates
|
|
{
|
|
internal sealed class MethodTemplate : AbstractTemplate
|
|
{
|
|
public List<Modifier> Modifiers { get; } = new List<Modifier>();
|
|
public List<ArgumentTemplate> Arguments { get; } = new List<ArgumentTemplate>();
|
|
public string Name { get; set; }
|
|
public string ReturnType { get; set; }
|
|
public string Body { get; set; }
|
|
|
|
public MethodTemplate WithModifiers(params Modifier[] modifiers)
|
|
{
|
|
this.Modifiers.Clear();
|
|
this.Modifiers.AddRange(modifiers);
|
|
return this;
|
|
}
|
|
public MethodTemplate WithArguments(params ArgumentTemplate[] argumentTemplates)
|
|
{
|
|
this.Arguments.Clear();
|
|
this.Arguments.AddRange(argumentTemplates);
|
|
return this;
|
|
}
|
|
public MethodTemplate WithName(string name)
|
|
{
|
|
this.Name = name;
|
|
return this;
|
|
}
|
|
public MethodTemplate WithReturnType(string returnType)
|
|
{
|
|
this.ReturnType = returnType;
|
|
return this;
|
|
}
|
|
public MethodTemplate WithBody(string body)
|
|
{
|
|
this.Body = body;
|
|
return this;
|
|
}
|
|
|
|
public override void Generate(CodeWriter codeWriter)
|
|
{
|
|
foreach(var modifier in this.Modifiers)
|
|
{
|
|
codeWriter
|
|
.Append(modifier)
|
|
.Append(' ');
|
|
}
|
|
|
|
codeWriter
|
|
.Append(this.ReturnType)
|
|
.Append(' ')
|
|
.Append(this.Name)
|
|
.Append('(');
|
|
for(int i = 0; i < this.Arguments.Count; i++)
|
|
{
|
|
var argument = this.Arguments[i];
|
|
codeWriter.Append(argument);
|
|
if (i < this.Arguments.Count - 1)
|
|
{
|
|
codeWriter.Append(", ");
|
|
}
|
|
}
|
|
|
|
codeWriter
|
|
.Append(')')
|
|
.BeginCodeBlock()
|
|
.Append(this.Body)
|
|
.EndCodeBlock();
|
|
}
|
|
}
|
|
} |