Пример #1
0
        public static void ValidateBeforeAttachedPropertyConversion(this PropertyDefinition property, TypeDefinition type)
        {
            CustomAttribute attribute    = property.GetAttachedPropertyAttribute();
            FieldDefinition backingField = type.GetBackingFieldForProperty(property);

            if (attribute != null && backingField == null)
            {
                throw new WeavingException("Cannot convert to attached property because the property does not have a backing field.");
            }

            if (property.GetMethod == null || property.SetMethod == null)
            {
                throw new WeavingException("Cannot convert to attached property because the property is not an auto property.");
            }

            if (!property.GetMethod.IsStatic)
            {
                throw new WeavingException("Cannot convert to attached property because the property is not static.");
            }
        }
Пример #2
0
        protected override bool ShouldConvert(TypeDefinition type, PropertyDefinition property)
        {
            CustomAttribute typeAttribute     = type.GetAttachedPropertyAttribute();
            CustomAttribute propertyAttribute = property.GetAttachedPropertyAttribute();

            FieldDefinition backingField = type.GetBackingFieldForProperty(property);

            if (typeAttribute == null && propertyAttribute == null)
            {
                return(false);
            }

            if (typeAttribute != null)
            {
                if (backingField == null)
                {
                    return(false);
                }

                if (property.SetMethod == null)
                {
                    return(false);
                }

                if (!property.GetMethod.IsStatic)
                {
                    return(false);
                }

                if (property.CustomAttributes.Any(attribute => attribute.AttributeType.Name == Consts.ExcludeAttachedPropertyAttribute))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        private void AddInitializationToStaticConstructor(TypeDefinition type, PropertyDefinition property, FieldDefinition field, Range instructionRange, int startIndex)
        {
            MethodDefinition staticConstructor = type.GetStaticConstructor();

            List <Instruction> instructions = CreateInstructionsUpToRegistration(type, property, instructionRange, property.GetAttachedPropertyAttribute());

            instructions.Add(Instruction.Create(OpCodes.Call, _registerAttachedProperty));
            instructions.Add(Instruction.Create(OpCodes.Stsfld, field));

            instructions.Reverse();

            foreach (Instruction instruction in instructions)
            {
                staticConstructor.Body.Instructions.Insert(startIndex, instruction);
            }
        }