Ejemplo n.º 1
0
        private void AddProxyInfo(ClassNotation @class, ProxyGeneratorContext context, ITypeSymbolInfo type)
        {
            @class.Members.Add(new FieldNotation()
            {
                Accessibility = AccessibilityInfo.Public, Type = type.FullName, Name = context.GetProxyFieldName()
            });
            var setProxyNode = new MethodNotation()
            {
                Accessibility = AccessibilityInfo.Public, ReturnType = "void", Name = "SetProxy"
            };

            setProxyNode.Parameters.Add(new ParameterNotation()
            {
                Type = "object", Name = "instance"
            });
            setProxyNode.Parameters.Add(new ParameterNotation()
            {
                Type = "System.IServiceProvider", Name = "serviceProvider"
            });
            setProxyNode.Body.AddRange(Notation.Create(context.GetProxyFieldName(), " = instance as ", type.FullName, ";"));
            @class.Members.Add(setProxyNode);
            @class.Inherits.Add("Norns.Destiny.AOP.IInterceptProxy".ToNotation());
            foreach (var f in @class.Members.Select(i => i as FieldNotation).Where(i => i != null && i.IsFromDI))
            {
                setProxyNode.Body.AddRange(Notation.Create(f.Name, " = serviceProvider.GetService(typeof(", f.Type, ")) as ", f.Type, ";"));
            }
        }
Ejemplo n.º 2
0
        private INotation CreateProxyMethod(IMethodSymbolInfo method, ProxyGeneratorContext typeContext)
        {
            var context = new ProxyGeneratorContext()
            {
                Parent = typeContext,
                Symbol = method
            };

            var notation = method.ToNotationDefinition();

            context.SetCurrentMethodNotation(notation);
            var isInterface = method.ContainingType.IsInterface;

            notation.IsOverride = !isInterface && method.CanOverride();
            var returnValueParameterName = context.GetReturnValueParameterName();

            if (method.HasReturnValue)
            {
                notation.Body.AddRange(Notation.Create("var ", returnValueParameterName, " = default(", method.IsAsync ? method.ReturnType.TypeArguments.First().FullName : method.ReturnType.FullName, ");"));
            }
            notation.Body.Add(method.Parameters.Where(i => i.RefKind == RefKindInfo.Out).Select(i => $"{i.Name} = default;".ToNotation()).Combine());
            notation.Body.AddRange(interceptors.SelectMany(i => i.BeforeMethod(context)));
            if (isInterface || (!method.IsAbstract && (method.Accessibility == AccessibilityInfo.Internal || method.Accessibility == AccessibilityInfo.Public)))
            {
                if (method.HasReturnValue)
                {
                    notation.Body.AddRange(Notation.Create(returnValueParameterName, " = "));
                }
                notation.Body.AddRange(Notation.Create(method.IsAsync ? "await " : string.Empty, method.ContainingType.IsInterface && !method.IsAbstract ? $"({context.GetProxyFieldName()} as {method.ContainingType.FullName})"  : context.GetProxyFieldName(), ".", method.Name));
                notation.Body.Add(ConstNotations.OpenParen);
                notation.Body.Add(notation.Parameters.ToCallParameters());
                notation.Body.Add(ConstNotations.CloseParen);
                notation.Body.Add(ConstNotations.Semicolon);
            }
            notation.Body.AddRange(interceptors.SelectMany(i => i.AfterMethod(context)));

            if (method.HasReturnValue)
            {
                notation.Body.AddRange(Notation.Create("return ", returnValueParameterName, ";"));
            }
            return(notation);
        }
Ejemplo n.º 3
0
        private INotation CreateProxyProperty(IPropertySymbolInfo property, ProxyGeneratorContext typeContext)
        {
            var context = new ProxyGeneratorContext()
            {
                Parent = typeContext,
                Symbol = property
            };
            PropertyNotation notation;
            List <INotation> callName = new List <INotation>();

            if (property.IsIndexer)
            {
                var indexer = new IndexerPropertyNotation();
                indexer.Parameters.AddRange(property.Parameters.Select(i => new ParameterNotation()
                {
                    Type = i.Type.FullName,
                    Name = i.Name
                }));
                notation = indexer;
                callName.Add(ConstNotations.OpenBracket);
                callName.Add(indexer.Parameters.ToCallParameters());
                callName.Add(ConstNotations.CloseBracket);
            }
            else
            {
                callName.Add(ConstNotations.Dot);
                callName.Add(property.Name.ToNotation());
                notation = new PropertyNotation();
            }
            var isInterface = property.ContainingType.IsInterface;

            notation.IsOverride    = !isInterface && property.CanOverride();
            notation.Accessibility = property.Accessibility;
            notation.Name          = property.Name;
            notation.Type          = property.Type.FullName;
            if (property.CanRead)
            {
                context.SetCurrentPropertyMethod(property.GetMethod);
                var getter = PropertyMethodNotation.Create(true);
                getter.Accessibility = property.GetMethod.Accessibility;
                var returnValueParameterName = context.GetReturnValueParameterName();
                getter.Body.AddRange(Notation.Create("var ", returnValueParameterName, " = default(", property.Type.FullName, ");"));
                getter.Body.AddRange(interceptors.SelectMany(i => i.BeforeMethod(context)));
                if (isInterface || (getter.Accessibility == AccessibilityInfo.Public || getter.Accessibility == AccessibilityInfo.Internal))
                {
                    getter.Body.AddRange(Notation.Create(returnValueParameterName, " = ", context.GetProxyFieldName()));
                    getter.Body.AddRange(callName);
                    getter.Body.Add(ConstNotations.Semicolon);
                }
                getter.Body.AddRange(interceptors.SelectMany(i => i.AfterMethod(context)));
                getter.Body.AddRange(Notation.Create("return ", returnValueParameterName, ";"));
                notation.Accessers.Add(getter);
            }
            if (property.CanWrite)
            {
                context.SetCurrentPropertyMethod(property.SetMethod);
                var setter = PropertyMethodNotation.Create(false);
                setter.Accessibility = property.SetMethod.Accessibility;
                var returnValueParameterName = context.GetReturnValueParameterName();
                setter.Body.AddRange(Notation.Create("var ", returnValueParameterName, " = value;"));
                setter.Body.AddRange(interceptors.SelectMany(i => i.BeforeMethod(context)));
                if (isInterface || (setter.Accessibility == AccessibilityInfo.Public || setter.Accessibility == AccessibilityInfo.Internal))
                {
                    setter.Body.Add(context.GetProxyFieldName().ToNotation());
                    setter.Body.AddRange(callName);
                    setter.Body.AddRange(Notation.Create(" = ", returnValueParameterName, ";"));
                }
                setter.Body.AddRange(interceptors.SelectMany(i => i.AfterMethod(context)));
                notation.Accessers.Add(setter);
            }
            return(notation);
        }