Beispiel #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, ";"));
            }
        }
Beispiel #2
0
        public override INotation CreateImplement(ITypeSymbolInfo type)
        {
            var @namespace = new NamespaceNotation()
            {
                Name = type.Namespace
            };
            var @class = new ClassNotation()
            {
                Accessibility = type.Accessibility,
                Name          = $"Proxy{type.Name}{RandomUtils.NewName()}"
            };

            @class.CustomAttributes.Add($"[Norns.Destiny.Attributes.Proxy(typeof({CreateImplementKey(type)}))]".ToNotation());
            if (type.IsGenericType)
            {
                @class.TypeParameters.AddRange(type.TypeParameters.Select(i => i.ToNotation()));
            }
            @namespace.Members.Add(@class);
            @class.Inherits.Add(type.FullName.ToNotation());
            var context = new ProxyGeneratorContext()
            {
                Symbol = type
            };

            context.SetCurrentNamespaceNotation(@namespace);
            context.SetCurrentClassNotation(@class);
            foreach (var member in type.Members.Union(type.Interfaces.SelectMany(i => i.Members)).Distinct())
            {
                switch (member)
                {
                case IMethodSymbolInfo method when !method.IsStatic &&
                    method.MethodKind != MethodKindInfo.PropertyGet &&
                    method.MethodKind != MethodKindInfo.PropertySet &&
                    (method.CanOverride() || (method.ContainingType.IsInterface && !method.IsAbstract)) &&
                    method.Name != "Finalize":
                    @class.Members.Add(CreateProxyMethod(method, context));
                    break;

                case IMethodSymbolInfo method when !method.IsStatic && method.MethodKind == MethodKindInfo.Constructor:
                    @class.Members.Add(GenerateImplementConstructor(method, @class.Name));
                    break;

                case IPropertySymbolInfo property when property.CanOverride():
                    @class.Members.Add(CreateProxyProperty(property, context));

                    break;

                default:
                    break;
                }
            }
            AddProxyInfo(@class, context, type);
            return(@namespace);
        }
Beispiel #3
0
        public override INotation CreateImplement(ITypeSymbolInfo type)
        {
            var context = new ProxyGeneratorContext()
            {
                Symbol = type
            };
            var @namespace = new NamespaceNotation()
            {
                Name = type.Namespace
            };
            var @class = new ClassNotation()
            {
                Accessibility = type.Accessibility,
                Name          = $"DefaultImplement{type.Name}{RandomUtils.NewName()}"
            };

            @class.CustomAttributes.Add($"[Norns.Destiny.Attributes.DefaultImplement(typeof({CreateImplementKey(type)}))]".ToNotation());
            if (type.IsGenericType)
            {
                @class.TypeParameters.AddRange(type.TypeParameters.Select(i => i.ToNotation()));
            }
            @namespace.Members.Add(@class);
            @class.Inherits.Add(type.FullName.ToNotation());
            foreach (var member in type.Members.Union(type.Interfaces.SelectMany(i => i.Members)).Distinct(new FullNameSymbolInfoEqualityComparer()))
            {
                switch (member)
                {
                case IMethodSymbolInfo method when method.Accessibility != AccessibilityInfo.Private && !method.IsStatic && method.IsAbstract && method.MethodKind == MethodKindInfo.Method:
                    @class.Members.Add(GenerateImplementMethod(method, context));
                    break;

                case IMethodSymbolInfo method when method.Accessibility != AccessibilityInfo.Private && !method.IsStatic && method.MethodKind == MethodKindInfo.Constructor:
                    @class.Members.Add(GenerateImplementConstructor(method, @class.Name));
                    break;

                case IPropertySymbolInfo property when property.Accessibility != AccessibilityInfo.Private && !property.IsStatic:
                    @class.Members.Add(GenerateImplementProperty(property, context));
                    break;

                default:
                    break;
                }
            }
            return(@namespace);
        }
Beispiel #4
0
 public static void SetCurrentClassNotation(this ProxyGeneratorContext context, ClassNotation @class)
 {
     context[CurrentClassNotation] = @class;
 }