private ConstructorNotation GenerateImplementConstructor(IMethodSymbolInfo method, string className) { var notation = method.ToConstructorNotation(className); notation.Accessibility = AccessibilityInfo.Public; notation.HasBase = true; return(notation); }
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); }
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); } }
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); }
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); }
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); }
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()); } }
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()); } }
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()); } } }
public static bool CanOverride(this IMethodSymbolInfo method) { return(!method.IsSealed && !method.IsStatic && (method.IsAbstract || method.IsVirtual || method.IsOverride)); }
public virtual IEnumerable <INotation> AfterProperty(ProxyGeneratorContext context, IPropertySymbolInfo property, IMethodSymbolInfo method) => ConstNotations.Nothings;
public virtual IEnumerable <INotation> BeforeMethod(ProxyGeneratorContext context, IMethodSymbolInfo method) => ConstNotations.Nothings;
public static void SetCurrentPropertyMethod(this ProxyGeneratorContext context, IMethodSymbolInfo method) { context[CurrentPropertyMethod] = method; }