Exemplo n.º 1
0
        public static bool IsMarkedAsReadOnly(this PropertyDefinition property)
        {
            CustomAttribute attribute  = property.GetDependencyPropertyAttribute();
            bool?           isReadonly = attribute?.Properties.FirstOrDefault(p => p.Name == Consts.IsReadOnly).Argument.Value as bool?;

            return(isReadonly == true);
        }
        protected override bool ShouldConvert(TypeDefinition type, PropertyDefinition property)
        {
            CustomAttribute typeAttribute     = type.GetDependencyPropertyAttribute();
            CustomAttribute propertyAttribute = property.GetDependencyPropertyAttribute();

            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.CustomAttributes.Any(attribute => attribute.AttributeType.Name == nameof(ExcludeDependencyPropertyAttribute)))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public static void ValidateBeforeDependencyPropertyConversion(this PropertyDefinition property, TypeDefinition type)
        {
            CustomAttribute attribute    = property.GetDependencyPropertyAttribute();
            FieldDefinition backingField = type.GetBackingFieldForProperty(property);

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

            if (property.GetMethod == null || property.SetMethod == null)
            {
                throw new WeavingException("Cannot convert to dependency property because the property is not an auto property.");
            }
        }
        private void AddInitializationToStaticConstructorReadonly(TypeDefinition type, PropertyDefinition property, FieldDefinition dependencyPropertyKeyField, FieldDefinition field, Range instructionRange)
        {
            MethodDefinition staticConstructor = type.GetStaticConstructor();

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

            instructions.Add(Instruction.Create(OpCodes.Call, _registerDependencyPropertyReadOnly));
            instructions.Add(Instruction.Create(OpCodes.Stsfld, dependencyPropertyKeyField));

            instructions.Add(Instruction.Create(OpCodes.Ldsfld, dependencyPropertyKeyField));
            instructions.Add(Instruction.Create(OpCodes.Callvirt, _getDependencyProperty));
            instructions.Add(Instruction.Create(OpCodes.Stsfld, field));

            instructions.Reverse();

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