Save
This commit is contained in:
@@ -8,52 +8,113 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Convention.RScript.Variable
|
||||
{
|
||||
[System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class RScriptMethodAttribute : Attribute
|
||||
namespace Attr
|
||||
{
|
||||
public string Description { get; set; }
|
||||
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
|
||||
public class SerializeAttribute : Attribute
|
||||
{
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class MethodAttribute : SerializeAttribute
|
||||
{
|
||||
}
|
||||
|
||||
[System.AttributeUsage(AttributeTargets.Enum, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class EnumAttribute : SerializeAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class RScriptInjectVariable
|
||||
{
|
||||
public string RScriptString { get; private set; }
|
||||
|
||||
protected abstract string WritePageHead(Type currentType);
|
||||
|
||||
#region Enum
|
||||
|
||||
protected abstract string WriteEnumHead(Type currentEnum);
|
||||
protected abstract string WriteEnumBodyEnter(Type currentType);
|
||||
protected abstract string WriteEnumName(string enumName, Type valueType);
|
||||
protected abstract string WriteEnumBodyExit(Type currentType);
|
||||
protected abstract string WriteEnumTail(Type currentType);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Class
|
||||
|
||||
protected abstract string WriteClassHead(Type currentType);
|
||||
protected abstract string WriteClassBodyEnter(Type currentType);
|
||||
protected abstract string WriteClassMethod(Type returnType, string methodName, string[] parameterNames, Type[] parameterTypes, string description);
|
||||
protected abstract string WriteClassBodyExit(Type currentType);
|
||||
protected abstract string WriteClassTail(Type currentType);
|
||||
|
||||
#endregion
|
||||
|
||||
protected abstract string WritePageEnd(Type currentType);
|
||||
|
||||
/// <summary>
|
||||
/// 生成targetType类型的实体
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public delegate object Generater();
|
||||
|
||||
private Generater MyGenerater;
|
||||
public string name { get; private set; }
|
||||
|
||||
public readonly static Dictionary<string, RScriptInjectVariable> AllRScriptInjectVariables = new();
|
||||
|
||||
public readonly string name;
|
||||
public readonly string script;
|
||||
|
||||
public object Generate()
|
||||
{
|
||||
return MyGenerater();
|
||||
}
|
||||
|
||||
public RScriptInjectVariable([MaybeNull]Generater generater, string name)
|
||||
public RScriptInjectVariable(Type targetType, [MaybeNull]Generater generater, string name)
|
||||
{
|
||||
var currentType = this.GetType();
|
||||
StringBuilder builder = new();
|
||||
builder.AppendLine(this.WriteClassHead(currentType));
|
||||
builder.AppendLine(this.WriteClassBodyEnter(currentType));
|
||||
var scriptMethods = from method in currentType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
|
||||
where method.GetCustomAttribute<RScriptMethodAttribute>() != null
|
||||
select method;
|
||||
foreach (var method in scriptMethods)
|
||||
if (AllRScriptInjectVariables.ContainsKey(name))
|
||||
throw new InvalidOperationException($"{name} is been used");
|
||||
|
||||
{
|
||||
var attr = method.GetCustomAttribute<RScriptMethodAttribute>();
|
||||
var returnType = method.ReturnType;
|
||||
var methodName = method.Name;
|
||||
var parameters = method.GetParameters();
|
||||
var parameterNames = from item in parameters select item.Name;
|
||||
var parameterTypes = from item in parameters select item.ParameterType;
|
||||
builder.AppendLine(this.WriteClassMethod(returnType, methodName, parameterNames.ToArray(), parameterTypes.ToArray(), attr.Description));
|
||||
StringBuilder builder = new();
|
||||
builder.AppendLine(this.WritePageHead(targetType));
|
||||
builder.AppendLine(this.WriteClassHead(targetType));
|
||||
builder.AppendLine(this.WriteClassBodyEnter(targetType));
|
||||
// 绘制枚举
|
||||
{
|
||||
var scriptEnums = from enumItem in targetType.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)
|
||||
where enumItem.GetCustomAttribute<Attr.EnumAttribute>() != null
|
||||
select enumItem;
|
||||
foreach (var enumItem in scriptEnums)
|
||||
{
|
||||
var attr = enumItem.GetCustomAttribute<Attr.EnumAttribute>();
|
||||
|
||||
}
|
||||
}
|
||||
// 绘制方法
|
||||
{
|
||||
var scriptMethods = from method in targetType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
|
||||
where method.GetCustomAttribute<Attr.MethodAttribute>() != null
|
||||
select method;
|
||||
foreach (var method in scriptMethods)
|
||||
{
|
||||
var attr = method.GetCustomAttribute<Attr.MethodAttribute>();
|
||||
var returnType = method.ReturnType;
|
||||
var methodName = method.Name;
|
||||
var parameters = method.GetParameters();
|
||||
var parameterNames = from item in parameters select item.Name;
|
||||
var parameterTypes = from item in parameters select item.ParameterType;
|
||||
builder.AppendLine(this.WriteClassMethod(returnType, methodName, parameterNames.ToArray(), parameterTypes.ToArray(), attr.Description));
|
||||
}
|
||||
}
|
||||
builder.AppendLine(this.WriteClassBodyExit(targetType));
|
||||
builder.AppendLine(this.WriteClassTail(targetType));
|
||||
builder.AppendLine(this.WritePageEnd(targetType));
|
||||
this.script = builder.ToString();
|
||||
}
|
||||
builder.AppendLine(this.WriteClassBodyExit(currentType));
|
||||
builder.AppendLine(this.WriteClassTail(currentType));
|
||||
|
||||
this.MyGenerater = generater;
|
||||
this.name = name;
|
||||
|
||||
Reference in New Issue
Block a user