public static GetActorRefClassName ( |
||
type | ||
Результат | string |
private void GenerateRefCode( Type type, CodeWriter.CodeWriter w, Type[] baseTypes, Tuple <Type, List <Tuple <MethodInfo, Tuple <string, string> > > >[] typeInfos) { // NoReply Interface var baseNoReplys = baseTypes.Select(t => Utility.GetNoReplyInterfaceName(t)); var baseNoReplysInherit = baseNoReplys.Any() ? " : " + string.Join(", ", baseNoReplys) : ""; using (w.B($"public interface {Utility.GetNoReplyInterfaceName(type)}{type.GetGenericParameters()}{baseNoReplysInherit}{type.GetGenericConstraintClause()}")) { foreach (var m in typeInfos.First().Item2) { var method = m.Item1; var parameters = method.GetParameters(); var paramStr = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(true))); w._($"void {method.Name}{method.GetGenericParameters()}({paramStr}){method.GetGenericConstraintClause()};"); } } // ActorRef var refClassName = Utility.GetActorRefClassName(type); var refClassGenericName = refClassName + type.GetGenericParameters(); var noReplyInterfaceName = Utility.GetNoReplyInterfaceName(type); var noReplyInterfaceGenericName = noReplyInterfaceName + type.GetGenericParameters(); using (w.B($"public class {refClassName}{type.GetGenericParameters()} : InterfacedActorRef, {type.GetSymbolDisplay()}, {noReplyInterfaceName}{type.GetGenericParameters()}{type.GetGenericConstraintClause()}")) { // InterfaceType property w._($"public override Type InterfaceType => typeof({type.GetSymbolDisplay()});"); w._(); // Constructors using (w.B($"public {refClassName}() : base(null)")) { } using (w.B($"public {refClassName}(IRequestTarget target) : base(target)")) { } using (w.B($"public {refClassName}(IRequestTarget target, IRequestWaiter requestWaiter, TimeSpan? timeout = null) : base(target, requestWaiter, timeout)")) { } // With Helpers using (w.B($"public {noReplyInterfaceGenericName} WithNoReply()")) { w._("return this;"); } using (w.B($"public {refClassGenericName} WithRequestWaiter(IRequestWaiter requestWaiter)")) { w._($"return new {refClassGenericName}(Target, requestWaiter, Timeout);"); } using (w.B($"public {refClassGenericName} WithTimeout(TimeSpan? timeout)")) { w._($"return new {refClassGenericName}(Target, RequestWaiter, timeout);"); } // IInterface message methods foreach (var t in typeInfos) { var payloadTableClassName = Utility.GetPayloadTableClassName(t.Item1) + type.GetGenericParameters(); foreach (var m in t.Item2) { var method = m.Item1; var payloadTypes = m.Item2; var parameters = method.GetParameters(); var parameterTypeNames = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(true))); var parameterInits = string.Join(", ", parameters.Select(Utility.GetParameterAssignment)); var returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault(); // Request Methods var returnTaskType = (returnType != null) ? $"Task<{returnType.GetSymbolDisplay(true)}>" : "Task"; var prototype = $"public {returnTaskType} {method.Name}{method.GetGenericParameters()}({parameterTypeNames}){method.GetGenericConstraintClause()}"; using (w.B(prototype)) { using (w.i("var requestMessage = new RequestMessage {", "};")) { w._($"InvokePayload = new {payloadTableClassName}.{payloadTypes.Item1}{method.GetGenericParameters()} {{ {parameterInits} }}"); } if (returnType != null) { w._($"return SendRequestAndReceive<{returnType.GetSymbolDisplay(true)}>(requestMessage);"); } else { w._($"return SendRequestAndWait(requestMessage);"); } } } } // IInterface_NoReply message methods foreach (var t in typeInfos) { var interfaceName = Utility.GetNoReplyInterfaceName(t.Item1); var interfaceGenericName = interfaceName + t.Item1.GetGenericParameters(); var payloadTableClassName = Utility.GetPayloadTableClassName(t.Item1) + type.GetGenericParameters(); foreach (var m in t.Item2) { var method = m.Item1; var payloadTypes = m.Item2; var parameters = method.GetParameters(); var parameterTypeNames = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(false))); var parameterInits = string.Join(", ", parameters.Select(Utility.GetParameterAssignment)); // Request Methods using (w.B($"void {interfaceGenericName}.{method.Name}{method.GetGenericParameters()}({parameterTypeNames})")) { using (w.i("var requestMessage = new RequestMessage {", "};")) { w._($"InvokePayload = new {payloadTableClassName}.{payloadTypes.Item1}{method.GetGenericParameters()} {{ {parameterInits} }}"); } w._("SendRequest(requestMessage);"); } } } } // Protobuf-net specialized if (Options.UseProtobuf) { var surrogateClassName = Utility.GetSurrogateClassName(type); w._("[ProtoContract]"); using (w.B($"public class {surrogateClassName}")) { w._($"[ProtoMember(1)] public IRequestTarget Target;"); w._(); w._("[ProtoConverter]"); using (w.B($"public static {surrogateClassName} Convert({type.Name} value)")) { w._($"if (value == null) return null;"); w._($"return new {surrogateClassName} {{ Target = (({refClassName})value).Target }};"); } w._("[ProtoConverter]"); using (w.B($"public static {type.Name} Convert({surrogateClassName} value)")) { w._($"if (value == null) return null;"); w._($"return new {refClassName}(value.Target);"); } } } }