private void AddGetterMethod(AttachedPropertyField field, bool isReadOnly)
        {
            var method = new MethodDefinition("Get" + field.PropertyName,
                                              MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig,
                                              field.PropertyType);

            method.Parameters.Add(new ParameterDefinition("dependencyObject", ParameterAttributes.None, Definition.ImportType<DependencyObject>()));

            var proc = method.Body.GetILProcessor();
            proc.Emit(OpCodes.Ldarg_0);
            proc.Emit(OpCodes.Ldsfld, field.FieldReference);
            if (isReadOnly)
                proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyPropertyKey).GetMethod("get_DependencyProperty")));

            proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyObject).GetMethod("GetValue")));
            proc.Emit(field.PropertyType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, field.PropertyType);
            proc.Emit(OpCodes.Ret);

            field.DeclaringType.Resolve().Methods.Add(method);
        }
        private void AddSetterMethod(AttachedPropertyField field)
        {
            var method = new MethodDefinition("Set" + field.PropertyName,
                                              MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig,
                                              Definition.ImportType(Type.GetType("System.Void")));

            method.Parameters.Add(new ParameterDefinition("dependencyObject", ParameterAttributes.None, Definition.ImportType<DependencyObject>()));
            method.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, field.PropertyType));

            var proc = method.Body.GetILProcessor();
            proc.Emit(OpCodes.Ldarg_0);
            proc.Emit(OpCodes.Ldsfld, field.FieldReference);
            proc.Emit(OpCodes.Ldarg_1);
            if (field.PropertyType.IsValueType)
            {
                proc.Emit(OpCodes.Box, field.PropertyType);
            }
            proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyObject).GetMethod("SetValue", new[] { typeof(DependencyProperty), typeof(object) })));
            proc.Emit(OpCodes.Ret);

            field.DeclaringType.Resolve().Methods.Add(method);
        }
        private void AddSetterMethod(AttachedPropertyField field)
        {
            var method = new MethodDefinition("Set" + field.PropertyName,
                                              MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig,
                                              Definition.ImportType(Type.GetType("System.Void")));

            method.Parameters.Add(new ParameterDefinition("dependencyObject", ParameterAttributes.None, Definition.ImportType <DependencyObject>()));
            method.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, field.PropertyType));

            var proc = method.Body.GetILProcessor();

            proc.Emit(OpCodes.Ldarg_0);
            proc.Emit(OpCodes.Ldsfld, field.FieldReference);
            proc.Emit(OpCodes.Ldarg_1);
            if (field.PropertyType.IsValueType)
            {
                proc.Emit(OpCodes.Box, field.PropertyType);
            }
            proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyObject).GetMethod("SetValue", new[] { typeof(DependencyProperty), typeof(object) })));
            proc.Emit(OpCodes.Ret);

            field.DeclaringType.Resolve().Methods.Add(method);
        }
        private void AddGetterMethod(AttachedPropertyField field, bool isReadOnly)
        {
            var method = new MethodDefinition("Get" + field.PropertyName,
                                              MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig,
                                              field.PropertyType);

            method.Parameters.Add(new ParameterDefinition("dependencyObject", ParameterAttributes.None, Definition.ImportType <DependencyObject>()));

            var proc = method.Body.GetILProcessor();

            proc.Emit(OpCodes.Ldarg_0);
            proc.Emit(OpCodes.Ldsfld, field.FieldReference);
            if (isReadOnly)
            {
                proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyPropertyKey).GetMethod("get_DependencyProperty")));
            }

            proc.Emit(OpCodes.Callvirt, Definition.ImportMethod(typeof(DependencyObject).GetMethod("GetValue")));
            proc.Emit(field.PropertyType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, field.PropertyType);
            proc.Emit(OpCodes.Ret);

            field.DeclaringType.Resolve().Methods.Add(method);
        }