Exemplo n.º 1
0
        private ConstructorNotation GenerateImplementConstructor(IMethodSymbolInfo method, string className)
        {
            var notation = method.ToConstructorNotation(className);

            notation.Accessibility = AccessibilityInfo.Public;
            notation.HasBase       = true;
            return(notation);
        }
Exemplo n.º 2
0
        public static ConstructorNotation ToConstructorNotation(this IMethodSymbolInfo method, string className)
        {
            var notation = new ConstructorNotation()
            {
                Name          = className,
                Accessibility = method.Accessibility
            };

            notation.BaseParameters.AddRange(method.Parameters.Select(i => new ParameterNotation()
            {
                RefKind = i.RefKind,
                Type    = i.Type.FullName,
                Name    = i.Name
            }));
            return(notation);
        }
Exemplo n.º 3
0
        public static (bool, bool) GetMethodExtensionInfo(this IMethodSymbolInfo method)
        {
            var returnTypeStr = method.ReturnType?.FullName;

            if (returnTypeStr == null)
            {
                return(false, false);
            }
            else
            {
                var isTask         = returnTypeStr.StartsWith(TaskFullName);
                var isValueTask    = returnTypeStr.StartsWith(ValueTaskFullName);
                var isAsync        = isTask || isValueTask;
                var hasReturnValue = isAsync ? returnTypeStr.EndsWith(">") : returnTypeStr != VoidFullName;
                return(isAsync, hasReturnValue);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public static MethodNotation ToNotationDefinition(this IMethodSymbolInfo method)
        {
            var notation = new MethodNotation()
            {
                Accessibility = method.Accessibility,
                ReturnType    = method.ReturnType.FullName,
                Name          = method.Name
            };

            notation.Parameters.AddRange(method.Parameters.Select(i => new ParameterNotation()
            {
                RefKind = i.RefKind,
                Type    = i.Type.FullName,
                Name    = i.Name
            }));
            if (method.IsGenericMethod)
            {
                notation.TypeParameters.AddRange(method.TypeParameters.Select(i => i.ToNotation()));
            }
            notation.IsAsync = method.IsAsync;
            return(notation);
        }
Exemplo n.º 6
0
        private INotation GenerateImplementMethod(IMethodSymbolInfo method, ProxyGeneratorContext typeContext)
        {
            var context = new ProxyGeneratorContext()
            {
                Parent = typeContext,
                Symbol = method
            };
            var notation = method.ToNotationDefinition();

            context.SetCurrentMethodNotation(notation);
            notation.IsOverride = !method.ContainingType.IsInterface && method.CanOverride();
            notation.Body.Add(method.Parameters.Where(i => i.RefKind == RefKindInfo.Out).Select(i => $"{i.Name} = default;".ToNotation()).Combine());
            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, ");"));
            }
            if (method.HasReturnValue)
            {
                notation.Body.AddRange(Notation.Create("return ", returnValueParameterName, ";"));
            }
            return(notation);
        }
Exemplo n.º 7
0
 public override IEnumerable <INotation> AfterMethod(ProxyGeneratorContext context, IMethodSymbolInfo method)
 {
     if (method.HasReturnValue)
     {
         yield return($"System.Console.WriteLine($\"return {{{context.GetReturnValueParameterName()}}} at {{System.DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss.fff\")}}\");".ToNotation());
     }
 }
Exemplo n.º 8
0
        public override IEnumerable <INotation> BeforeMethod(ProxyGeneratorContext context, IMethodSymbolInfo method)
        {
            _ = typeof(System.Console).Name.ToString(); // just make sure load System.Console dll before jit generate code
            if (!method.Parameters.IsEmpty)
            {
                yield return($"System.Console.WriteLine($\"Call Method {method.Name} at {{System.DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss.fff\")}} {method.Parameters.First().Type.FullName} {method.Parameters.First().Name} = {{{method.Parameters.First().Name}}}".ToNotation());

                foreach (var item in method.Parameters.Skip(1))
                {
                    yield return($", {item.FullName} {item.Name} = {{{item.Name}}}".ToNotation());
                }
                yield return("\");".ToNotation());
            }
        }
Exemplo n.º 9
0
        public override IEnumerable <INotation> AfterProperty(ProxyGeneratorContext context, IPropertySymbolInfo property, IMethodSymbolInfo method)
        {
            if (method.HasReturnValue)
            {
                var r     = context.GetReturnValueParameterName();
                var rType = method.ReturnType;
                if (rType.IsType <int>())
                {
                    yield return(r.ToNotation());

                    yield return("-=5;".ToNotation());
                }
            }
        }
Exemplo n.º 10
0
 public static bool CanOverride(this IMethodSymbolInfo method)
 {
     return(!method.IsSealed && !method.IsStatic && (method.IsAbstract || method.IsVirtual || method.IsOverride));
 }
Exemplo n.º 11
0
 public virtual IEnumerable <INotation> AfterProperty(ProxyGeneratorContext context, IPropertySymbolInfo property, IMethodSymbolInfo method) => ConstNotations.Nothings;
Exemplo n.º 12
0
 public virtual IEnumerable <INotation> BeforeMethod(ProxyGeneratorContext context, IMethodSymbolInfo method) => ConstNotations.Nothings;
Exemplo n.º 13
0
 public static void SetCurrentPropertyMethod(this ProxyGeneratorContext context, IMethodSymbolInfo method)
 {
     context[CurrentPropertyMethod] = method;
 }