GetSurrogateClassName() public static method

public static GetSurrogateClassName ( Type type ) : string
type System.Type
return string
Esempio n. 1
0
        private void EnsureSurrogateForIRequestTarget(Type callerType, CodeWriter.CodeWriter w)
        {
            if (_surrogateForIActorRefGenerated)
            {
                return;
            }

            var surrogateClassName = Utility.GetSurrogateClassName("IRequestTarget");

            w._($"#region {surrogateClassName}");
            w._();

            var namespaceHandle = (string.IsNullOrEmpty(callerType.Namespace) == false)
                ? w.B($"namespace {callerType.Namespace}")
                : null;

            w._("[ProtoContract]");
            using (w.B($"public class {surrogateClassName}"))
            {
                w._($"[ProtoMember(1)] public int Id;");
                w._($"[ProtoMember(2)] public string Address;");
                w._();

                w._("[ProtoConverter]");
                using (w.B($"public static {surrogateClassName} Convert(IRequestTarget value)"))
                {
                    w._($"if (value == null) return null;");
                    w._($"var target = ((BoundActorTarget)value);");
                    w._($"return new {surrogateClassName} {{ Id = target.Id, Address = target.Address }};");
                }

                w._("[ProtoConverter]");
                using (w.B($"public static IRequestTarget Convert({surrogateClassName} value)"))
                {
                    w._($"if (value == null) return null;");
                    w._($"return new BoundActorTarget(value.Id, value.Address);");
                }
            }

            namespaceHandle?.Dispose();

            w._();
            w._($"#endregion");

            _surrogateForIActorRefGenerated = true;
        }
        private void EnsureSurrogateForINotificationChannel(Type callerType, CodeWriter.CodeWriter w)
        {
            if (_surrogateForINotificationChannelGenerated)
            {
                return;
            }

            var surrogateClassName = Utility.GetSurrogateClassName("INotificationChannel");

            w._($"#region {surrogateClassName}");
            w._();

            var namespaceHandle = (string.IsNullOrEmpty(callerType.Namespace) == false)
                ? w.B($"namespace {callerType.Namespace}")
                : null;

            w._("[ProtoContract]");
            using (w.B($"public class {surrogateClassName}"))
            {
                w._("[ProtoConverter]");
                using (w.B($"public static {surrogateClassName} Convert(INotificationChannel value)"))
                {
                    w._($"if (value == null) return null;");
                    w._($"return new {surrogateClassName}();");
                }

                w._("[ProtoConverter]");
                using (w.B($"public static INotificationChannel Convert({surrogateClassName} value)"))
                {
                    w._($"return null;");
                }
            }

            namespaceHandle?.Dispose();

            w._();
            w._($"#endregion");

            _surrogateForINotificationChannelGenerated = true;
        }
        private void GenerateObserverCode(
            Type type, CodeWriter.CodeWriter w, Type[] baseTypes,
            Tuple <Type, List <Tuple <MethodInfo, string> > >[] typeInfos)
        {
            var className = Utility.GetObserverClassName(type);

            using (w.B($"public class {className}{type.GetGenericParameters()} : InterfacedObserver, {type.GetSymbolDisplay()}{type.GetGenericConstraintClause()}"))
            {
                // Constructor

                using (w.B($"public {className}()",
                           $": base(null, 0)"))
                {
                }

                // Constructor (INotificationChannel)

                using (w.B($"public {className}(INotificationChannel channel, int observerId = 0)",
                           $": base(channel, observerId)"))
                {
                }

                // Observer method messages

                foreach (var t in typeInfos)
                {
                    var payloadTableClassName = Utility.GetPayloadTableClassName(t.Item1) + type.GetGenericParameters();

                    foreach (var m in t.Item2)
                    {
                        var method      = m.Item1;
                        var payloadType = m.Item2;
                        var parameters  = method.GetParameters();

                        var parameterNames     = string.Join(", ", parameters.Select(p => p.Name));
                        var parameterTypeNames = string.Join(", ", parameters.Select(p => (p.GetCustomAttribute <ParamArrayAttribute>() != null ? "params " : "") + p.ParameterType.GetSymbolDisplay(true) + " " + p.Name));
                        var parameterInits     = string.Join(", ", parameters.Select(Utility.GetParameterAssignment));

                        // Request Methods

                        using (w.B($"public void {method.Name}{method.GetGenericParameters()}({parameterTypeNames}){method.GetGenericConstraintClause()}"))
                        {
                            w._($"var payload = new {payloadTableClassName}.{payloadType}{method.GetGenericParameters()} {{ {parameterInits} }};",
                                $"Notify(payload);");
                        }
                    }
                }
            }

            // Protobuf-net specialized

            if (Options.UseProtobuf)
            {
                var surrogateClassName = Utility.GetSurrogateClassName(type);

                w._("[ProtoContract]");
                using (w.B($"public class {surrogateClassName}"))
                {
                    w._("[ProtoMember(1)] public INotificationChannel Channel;");
                    w._("[ProtoMember(2)] public int ObserverId;");
                    w._();

                    w._("[ProtoConverter]");
                    using (w.B($"public static {surrogateClassName} Convert({type.Name} value)"))
                    {
                        w._($"if (value == null) return null;");
                        w._($"var o = ({className})value;");
                        w._($"return new {surrogateClassName} {{ Channel = o.Channel, ObserverId = o.ObserverId }};");
                    }

                    w._("[ProtoConverter]");
                    using (w.B($"public static {type.Name} Convert({surrogateClassName} value)"))
                    {
                        w._($"if (value == null) return null;");
                        w._($"return new {className}(value.Channel, value.ObserverId);");
                    }
                }
            }
        }
Esempio n. 4
0
        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);");
                    }
                }
            }
        }