Esempio n. 1
0
 public PropertyBuilderInfoItem(
     AttributedProperty attribute,
     Property property,
     __IPropertyGetterInterceptor interfaceGetter,
     __IPropertySetterInterceptor interfaceSetter,
     __IPropertyInterceptorInitialize interfaceInitializer)
 {
     this.Attribute                  = attribute;
     this.InterfaceGetter            = interfaceGetter;
     this.InterfaceSetter            = interfaceSetter;
     this.InterfaceInitializer       = interfaceInitializer;
     this.Property                   = property;
     this.HasSyncRootInterface       = attribute.Attribute.Type.Implements(__ISyncRoot.Type.Fullname);
     this.AssignMethodAttributeInfos = AssignMethodAttributeInfo.GetAllAssignMethodAttributedFields(attribute);
     this.InterceptorInfo            = new InterceptorInfo(this.Attribute.Attribute.Type);
 }
        private void InterceptProperties(Builder builder, IEnumerable <BuilderType> attributes)
        {
            if (!attributes.Any())
            {
                return;
            }

            var stopwatch = Stopwatch.StartNew();

            var iPropertyInterceptorInitialize = new __IPropertyInterceptorInitialize(builder);
            var iPropertyGetterInterceptor     = new __IPropertyGetterInterceptor(builder);
            var iPropertySetterInterceptor     = new __IPropertySetterInterceptor(builder);
            var propertyInterceptionInfo       = new __PropertyInterceptionInfo(builder);

            var properties = builder
                             .FindPropertiesByAttributes(attributes)
                             .GroupBy(x => x.Property)
                             .Select(x => new PropertyBuilderInfo(x.Key, x.Select(y => new PropertyBuilderInfoItem(y, y.Property,
                                                                                                                   y.Attribute.Type.Implements(iPropertyGetterInterceptor.Type.Fullname) ? iPropertyGetterInterceptor : null,
                                                                                                                   y.Attribute.Type.Implements(iPropertySetterInterceptor.Type.Fullname) ? iPropertySetterInterceptor : null,
                                                                                                                   y.Attribute.Type.Implements(iPropertyInterceptorInitialize.Type.Fullname) ? iPropertyInterceptorInitialize : null))))
                             .ToArray();

            foreach (var member in properties)
            {
                this.Log($"Implementing interceptors in property {member.Property}");

                if (member.Property.BackingField == null)
                {
                    this.Log(LogTypes.Error, member.Property, "Unable to detect the backing field of property: " + member.Property);
                    continue;
                }

                var propertyField = member.Property.CreateField(propertyInterceptionInfo.Type, $"<{member.Property.Name}>p__propertyInfo");

                var actionObjectCtor = builder.Import(typeof(Action <object>).GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                var propertySetter   = member.Property.DeclaringType.CreateMethod(member.Property.Modifiers.GetPrivate(), $"<{member.Property.Name}>m__setterMethod", builder.GetType(typeof(object)));

                CreatePropertySetterDelegate(builder, member, propertySetter);

                var indexer           = 0;
                var interceptorFields = member.InterceptorInfos.ToDictionary(x => x.Attribute.Identification,
                                                                             x => member.Property.DeclaringType.CreateField(x.Property.Modifiers.GetPrivate(), x.Attribute.Attribute.Type,
                                                                                                                            $"<{x.Property.Name}>_attrib{indexer++}_{x.Attribute.Identification}"));

                if (member.HasInitializer)
                {
                    AddPropertyInitializeInterception(builder, propertyInterceptionInfo, member, propertyField, actionObjectCtor, propertySetter, interceptorFields);
                }

                if (member.HasGetterInterception)
                {
                    AddPropertyGetterInterception(builder, propertyInterceptionInfo, member, propertyField, actionObjectCtor, propertySetter, interceptorFields);
                }

                if (member.HasSetterInterception)
                {
                    AddPropertySetterInterception(builder, propertyInterceptionInfo, member, propertyField, actionObjectCtor, propertySetter, interceptorFields);
                }

                // Do this at the end to ensure that syncroot init is always on the top
                if (member.RequiresSyncRootField)
                {
                    if (member.SyncRoot.IsStatic)
                    {
                        member.Property.DeclaringType.CreateStaticConstructor().NewCode()
                        .Assign(member.SyncRoot).NewObj(builder.GetType(typeof(object)).Import().ParameterlessContructor)
                        .Insert(InsertionPosition.Beginning);
                    }
                    else
                    {
                        foreach (var ctors in member.Property.DeclaringType.GetRelevantConstructors().Where(x => x.Name == ".ctor"))
                        {
                            ctors.NewCode().Assign(member.SyncRoot).NewObj(builder.GetType(typeof(object)).Import().ParameterlessContructor).Insert(InsertionPosition.Beginning);
                        }
                    }
                }

                // Also remove the compilergenerated attribute
                member.Property.Getter?.CustomAttributes.Remove(typeof(CompilerGeneratedAttribute));
                member.Property.Setter?.CustomAttributes.Remove(typeof(CompilerGeneratedAttribute));
            }

            stopwatch.Stop();
            this.Log($"Implementing property interceptors took {stopwatch.Elapsed.TotalMilliseconds}ms");
        }
Esempio n. 3
0
 public PropertyBuilderInfoItem(AttributedProperty attribute, Property property, __IPropertyGetterInterceptor interfaceGetter, __IPropertySetterInterceptor interfaceSetter, __IPropertyInterceptorInitialize interfaceInitializer)
 {
     this.Attribute            = attribute;
     this.InterfaceGetter      = interfaceGetter;
     this.InterfaceSetter      = interfaceSetter;
     this.InterfaceInitializer = interfaceInitializer;
     this.Property             = property;
     this.HasSyncRootInterface = attribute.Attribute.Type.Implements(__ISyncRoot.TypeName);
 }