генерация кода практически завершена
This commit is contained in:
@@ -104,11 +104,72 @@ namespace {Namespace}
|
||||
// Go through all class members with a particular type (property) to generate method lines.
|
||||
var methodBody = classSymbol.GetMembers()
|
||||
.OfType<IMethodSymbol>();
|
||||
methods.AddRange(methodBody.Select(i => (namespaceName + "." + className, i)));
|
||||
|
||||
var originalName = className;
|
||||
// Build up the source code
|
||||
className = className.TrimStart('I') + "RemoteEndpoint";
|
||||
|
||||
|
||||
var methodsText = new List<string>();
|
||||
|
||||
foreach (var method in methodBody)
|
||||
{
|
||||
var index = methods.Count;
|
||||
methods.Add((namespaceName + "." + className, method));
|
||||
var sb = new StringBuilder();
|
||||
|
||||
//Creating signature
|
||||
// if (method.Parameters.Length == 0)
|
||||
// sb.AppendLine($"public {method.ReturnType.ToDisplayString()} {method.Name}(){{");
|
||||
// else
|
||||
|
||||
bool isAsync = method.ReturnType.ToDisplayString() == "Task";
|
||||
sb.AppendLine("public" + (isAsync
|
||||
? " async "
|
||||
: " ") +
|
||||
$"{method.ReturnType.ToDisplayString()} {method.Name}({string.Join(", ", method.Parameters.Select(p => p.ToDisplayString()))}){{");
|
||||
|
||||
//Creating request
|
||||
if (method.Parameters.Length == 0)
|
||||
sb.AppendLine(
|
||||
$"\t\tvar request = new DefaultCallRequest {{ CommandId = {index}, ObjectId = id }};");
|
||||
else if (method.Parameters.Length == 1 && method.ReturnType.Name != "Task" ||
|
||||
method.Parameters.Length == 2 && method.ReturnType.Name == "Task")
|
||||
{
|
||||
sb.AppendLine(
|
||||
$"\t\tvar request = new DefaultCallRequest {{ CommandId = {index}, ObjectId = id, Parameter = {method.Parameters.First().Name} }};");
|
||||
}
|
||||
|
||||
//Post created request
|
||||
sb.AppendLine("\t\tserialisationModule.PostCallRequest(request);");
|
||||
|
||||
|
||||
if (method.ReturnType.ToString() == "System.Threading.Tasks.Task")
|
||||
{
|
||||
sb.AppendLine(
|
||||
"\t\tawait serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId);");
|
||||
}
|
||||
else if (method.ReturnType.OriginalDefinition.ToString() == "System.Threading.Tasks.Task<TResult>")
|
||||
{
|
||||
var type = method.ReturnType.ToString().Split(['<', '>'], StringSplitOptions.RemoveEmptyEntries)
|
||||
.Last();
|
||||
sb.AppendLine(
|
||||
$"\t\tvar response = await serialisationModule.GetFinalCommandExecution<{type}>(request.CallRequestId);");
|
||||
sb.AppendLine($"\t\treturn ({type})response.Result;");
|
||||
}
|
||||
else if (!isAsync && method.ReturnType.ToDisplayString() != "void")
|
||||
{
|
||||
var type = method.ReturnType.ToDisplayString();
|
||||
sb.AppendLine(
|
||||
$"\t\tvar response = serialisationModule.GetFinalCommandExecution<{type}>(request.CallRequestId).GetAwaiter().GetResult();");
|
||||
sb.AppendLine($"\t\treturn ({type})response.Result;");
|
||||
}
|
||||
|
||||
sb.AppendLine("\t}");
|
||||
|
||||
methodsText.Add(sb.ToString());
|
||||
}
|
||||
|
||||
var code = $@"// <auto-generated/>
|
||||
|
||||
using System;
|
||||
@@ -120,12 +181,14 @@ namespace {namespaceName};
|
||||
partial class {className} (int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule) : {originalName}, IRemoteObject
|
||||
{{
|
||||
public int Id => id;
|
||||
|
||||
{string.Join("\r\n\t", methodsText)}
|
||||
}}
|
||||
";
|
||||
|
||||
|
||||
// Add the source code to the compilation.
|
||||
// context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
|
||||
context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user