229 lines
11 KiB
C#
229 lines
11 KiB
C#
using System;
|
|
using System.CodeDom;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
namespace CodeGenerator {
|
|
// ReSharper disable always BitwiseOperatorOnEnumWithoutFlags
|
|
public class AttributeBuilder {
|
|
|
|
private CodeTypeDeclaration _attrDecl;
|
|
|
|
private List<(Type, string)> _fields = new();
|
|
|
|
/// <summary>
|
|
/// List of fields registered for the attribute currently being built.
|
|
/// </summary>
|
|
/// <remarks>Each tuple contains the type of the field and the name of the field</remarks>
|
|
public List<(Type, string)> Fields => _fields;
|
|
|
|
private List<(Type, string, string)> _properties = new();
|
|
|
|
/// <summary>
|
|
/// List of properties registered for the attribute currently being built.
|
|
/// </summary>
|
|
/// <remarks>Each tuple contains the type of the property, the name of the property and the name of the backing field</remarks>
|
|
public List<(Type, string, string)> Properties => _properties;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="AttributeBuilder"/> class.
|
|
/// </summary>
|
|
public AttributeBuilder() {
|
|
_attrDecl = new() {
|
|
IsClass = true,
|
|
Attributes = MemberAttributes.Public,
|
|
BaseTypes = { typeof(Attribute) }
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="AttributeBuilder"/> class with the specified name for the Attribute.
|
|
/// </summary>
|
|
/// <param name="name">Name the built attribute will have.</param>
|
|
/// <remarks>The set name can be always overridden by using <see cref="SetName"/> method.</remarks>
|
|
public AttributeBuilder(string name) : this() {
|
|
_attrDecl.Name = name;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the current state of the builder and prepares it for a new attribute declaration.
|
|
/// </summary>
|
|
/// <remarks>clears the internal metadata for fields and properties, also reinstantiates the <see cref="CodeTypeDeclaration"/> internal instance</remarks>
|
|
public AttributeBuilder New() {
|
|
_attrDecl = new() {
|
|
IsClass = true,
|
|
Attributes = MemberAttributes.Public,
|
|
BaseTypes = { typeof(Attribute) }
|
|
};
|
|
_fields.Clear();
|
|
_properties.Clear();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the base type of the attribute.
|
|
/// </summary>
|
|
/// <param name="baseType">Fully qualified name from which this is going to be derived</param>
|
|
public AttributeBuilder DeriveFromClass(string baseType = "System.Attribute") {
|
|
_attrDecl.BaseTypes[0] = new(baseType);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds another base type to the <see cref="CodeTypeDeclaration"/>. To be used only with interfaces.
|
|
/// </summary>
|
|
/// <param name="interfaceName">Fully qualfied name of the type to add to the BaseTypes list.</param>
|
|
public AttributeBuilder Implements(string interfaceName) {
|
|
_attrDecl.BaseTypes.Add(interfaceName);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the name of the attribute that will be generated.
|
|
/// </summary>
|
|
/// <param name="name">Name the attribute will have.</param>
|
|
public AttributeBuilder SetName(string name) {
|
|
_attrDecl.Name = name;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the <see cref="AttributeUsageAttribute"/> for the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="usageArgs">List of arguments for <see cref="AttributeUsageAttribute"/></param>
|
|
public AttributeBuilder SetAttributeUsage(CodeAttributeArgument usageArgs) {
|
|
var attrUsage = new CodeAttributeDeclaration() {
|
|
Name = nameof(AttributeUsageAttribute),
|
|
Arguments = { usageArgs }
|
|
};
|
|
_attrDecl.CustomAttributes.Add(attrUsage);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a field to the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="name">Name of the field to be added.</param>
|
|
/// <param name="attributes">Visibility parameters, OR'd sequence of <see cref="MemberAttributes"/></param>
|
|
/// <typeparam name="T">type of the attribute</typeparam>
|
|
public AttributeBuilder AddField<T>(string name,
|
|
MemberAttributes attributes = MemberAttributes.Private | MemberAttributes.Final) =>
|
|
AddField(typeof(T), name, attributes);
|
|
|
|
/// <summary>
|
|
/// Adds a field to the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="type">Type of the attribute. Must be a base type or a fully qualified name</param>
|
|
/// <param name="name">Name of the field to be added.</param>
|
|
/// <param name="attributes">Visibility parameters, OR'd sequence of <see cref="MemberAttributes"/></param>
|
|
public AttributeBuilder AddField(string type, string name,
|
|
MemberAttributes attributes = MemberAttributes.Private | MemberAttributes.Final) =>
|
|
AddField(Type.GetType(type), name, attributes);
|
|
|
|
/// <summary>
|
|
/// Adds a field to the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="type">Type of the attribute. Must be a base type or a fully qualified name</param>
|
|
/// <param name="name">Name of the field to be added.</param>
|
|
/// <param name="attributes">Visibility parameters, OR'd sequence of <see cref="MemberAttributes"/></param>
|
|
public AttributeBuilder AddField(Type type, string name,
|
|
MemberAttributes attributes = MemberAttributes.Private | MemberAttributes.Final) {
|
|
var field = new CodeMemberField(type, name);
|
|
field.Attributes = attributes;
|
|
_attrDecl.Members.Add(field);
|
|
_fields.Add((type, name));
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an auto-property to the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="type">Type of the attribute. Must be a base type or a fully qualified name</param>
|
|
/// <param name="name">Name of the property to be added.</param>
|
|
/// <param name="attributes">Visibility parameters, OR'd sequence of <see cref="MemberAttributes"/></param>
|
|
/// <param name="get">Whether the property should have a getter</param>
|
|
/// <param name="set">Whether the property should have a setter</param>
|
|
/// <remarks>This method is a composite call to <see cref="AddField{T}"/> and <see cref="AddProperty"/></remarks>
|
|
public AttributeBuilder AddAutoProperty(Type type, string name, MemberAttributes attributes = MemberAttributes.Public, bool get = true, bool set = true) {
|
|
AddField(type, $"_{name}", MemberAttributes.Private);
|
|
return AddProperty(type, name, $"_{name}", attributes, get, set);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a property to the attribute being generated.
|
|
/// </summary>
|
|
/// <param name="type">Type of the attribute. Must be a base type or a fully qualified name</param>
|
|
/// <param name="name">Name of the property to be added.</param>
|
|
/// <param name="backingPropertyName">Name of the backing field for the property</param>
|
|
/// <param name="attributes">Visibility parameters, OR'd sequence of <see cref="MemberAttributes"/></param>
|
|
/// <param name="get">Whether the property should have a getter</param>
|
|
/// <param name="set">Whether the property should have a setter</param>
|
|
public AttributeBuilder AddProperty(Type type, string name, string backingPropertyName,
|
|
MemberAttributes attributes = MemberAttributes.Public, bool get = true, bool set = true) {
|
|
var prop = new CodeMemberProperty {
|
|
Name = name,
|
|
Type = new (type),
|
|
Attributes = attributes,
|
|
HasGet = get,
|
|
HasSet = set
|
|
};
|
|
|
|
if (get) {
|
|
prop.GetStatements.Add(
|
|
new CodeMethodReturnStatement(
|
|
new CodeFieldReferenceExpression(
|
|
new CodeThisReferenceExpression(), backingPropertyName)));
|
|
}
|
|
|
|
if (set) {
|
|
prop.SetStatements.Add(
|
|
new CodeAssignStatement(
|
|
new CodeFieldReferenceExpression(
|
|
new CodeThisReferenceExpression(), backingPropertyName),
|
|
new CodePropertySetValueReferenceExpression()));
|
|
}
|
|
|
|
_properties.Add((type, name, backingPropertyName));
|
|
_attrDecl.Members.Add(prop);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a constructor to all the properties inside the attribute being generated.
|
|
/// </summary>
|
|
public AttributeBuilder AddPropertiesConstructor() {
|
|
var ctor = new CodeConstructor { Attributes = MemberAttributes.Public };
|
|
_attrDecl.Members.Add(ctor);
|
|
_properties.ForEach(property => {
|
|
ctor.Parameters.Add(new (property.Item1, property.Item2));
|
|
ctor.Statements.Add(
|
|
new CodeAssignStatement(
|
|
new CodeFieldReferenceExpression(
|
|
new CodeThisReferenceExpression(), property.Item3), new CodeVariableReferenceExpression(property.Item2)));
|
|
});
|
|
return this;
|
|
}
|
|
|
|
//TODO: tyhis should use something more robust and less error prone than just strings
|
|
/// <summary>
|
|
/// Adds parameters to be passed to the base class
|
|
/// </summary>
|
|
/// <param name="parameters">Array of strings which reference the params</param>
|
|
/// <exception cref="InvalidOperationException"> thrown when no constructor is defined yet.</exception>
|
|
public AttributeBuilder AddBaseConstructorParams(string[] parameters) {
|
|
var ctor = _attrDecl.Members.OfType<CodeConstructor>().FirstOrDefault();
|
|
if (ctor == null) throw new InvalidOperationException("There is no constructor defined yet.");
|
|
foreach (var param in parameters) {
|
|
ctor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(param));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the <see cref="CodeTypeDeclaration"/> instance.
|
|
/// </summary>
|
|
/// <returns>Instance of the <see cref="CodeTypeDeclaration"/> built by the builder.</returns>
|
|
public CodeTypeDeclaration Build() => _attrDecl;
|
|
}
|
|
} |