예제 #1
0
        public void ThrowsOnCreateExistingProperty()
        {
            var context = new AttachedPropertyContext();
            var name    = typeof(SomeClass).GetProperties().First().Name;

            Assert.Throws <ArgumentException>("name", () => new AttachedProperty <SomeClass, object>(name, context));
        }
예제 #2
0
        public void ThrowsOnSetNullAttachedProperty()
        {
            var context = new AttachedPropertyContext();
            var tested  = new object();

            Assert.Throws <ArgumentNullException>(() => tested.SetAttachedValue(null, "asdf", context));
        }
예제 #3
0
        public void ThrowsOnGetNullAttachedProperty()
        {
            var context = new AttachedPropertyContext();
            var tested  = new object();

            Assert.Throws <ArgumentNullException>(() => tested.GetAttachedValue((AttachedProperty <object, string>)null, context));
        }
예제 #4
0
        public void ThrowsOnSetNullContext()
        {
            var context          = new AttachedPropertyContext();
            var attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context);
            var tested           = new object();

            Assert.Throws <ArgumentNullException>(() => tested.SetAttachedValue(attachedProperty, "asdf", null));
        }
예제 #5
0
        public void ThrowsOnGetNullInstance()
        {
            var    context          = new AttachedPropertyContext();
            var    attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context);
            object tested           = null;

            Assert.Throws <ArgumentNullException>(() => tested.GetAttachedValue(attachedProperty, context));
        }
예제 #6
0
        public void CanCreate()
        {
            var context = new AttachedPropertyContext();
            var tested  = new AttachedProperty <object, int>("SomeName", context);

            Assert.Equal("System.Object.SomeName", tested.FullName);
            Assert.Equal(typeof(object), tested.OwnerType);
            Assert.Equal(typeof(int), tested.PropertyType);
        }
예제 #7
0
        public void ThrowsOnCreateDuplicate()
        {
            var context = new AttachedPropertyContext();

            var first = new AttachedProperty <object, int>("asdf", context);

            Assert.NotNull(first);
            Assert.Throws <AttachedPropertyException>(() => new AttachedProperty <object, int>("asdf", context));
        }
예제 #8
0
        public void CanGetDefaultWithoutSet()
        {
            var context          = new AttachedPropertyContext();
            var attachedProperty = new AttachedProperty <object, string>("CanGetDefaultWithoutSet", context);
            var tested           = new object();

            var value = tested.GetAttachedValue(attachedProperty, context);

            Assert.Equal(default(string), value);
        }
예제 #9
0
        public void CanSetAndGet()
        {
            var context          = new AttachedPropertyContext();
            var attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context);
            var tested           = new object();

            tested.SetAttachedValue(attachedProperty, "asdf", context);
            var value = tested.GetAttachedValue(attachedProperty, context);

            Assert.Equal("asdf", value);
        }
예제 #10
0
        public void CanGetInstancesDefaultValue()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested);
            var instance         = new object();

            instance.SetAttachedValue(attachedProperty, default(int), tested);
            var instances = tested.GetInstances();

            Assert.Equal(0, instances.Count);
        }
예제 #11
0
        public void ThrowsOnGetNotRegistered()
        {
            var context = new AttachedPropertyContext();
            var tested  = new object();

            var attachedProperty = new AttachedProperty <object, int>("asdf", context);

            Assert.Throws <AttachedPropertyException>(() =>
            {
                var _ = tested.GetAttachedValue(attachedProperty);
            });
        }
예제 #12
0
        public void CanSetDefaultValueDoesNotStore()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, string>("CanGetDefaultWithoutSet", tested);
            var instance         = new object();

            instance.SetAttachedValue(attachedProperty, default(string), tested);
            var properties = tested.GetInstanceProperties(instance);

            Assert.Equal(0, properties.Count);
        }
예제 #13
0
        public void CanGetNonObjectGetValueNoSet()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("CanGetNonObjectGetValueNoSet", tested);
            var another          = new AttachedProperty <object, int>("Another", tested);
            var instance         = new object();

            instance.SetAttachedValue(another, 1, tested);
            var value = instance.GetAttachedValue(attachedProperty, tested);

            Assert.Equal(0, value);
        }
예제 #14
0
        public void CanGetProperties()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested);
            var properties       = tested.GetProperties();

            Assert.Equal(1, properties.Count);
            Assert.All(properties, x =>
            {
                Assert.Same(attachedProperty, x);
            });
        }
예제 #15
0
        public void CanGetInstancesNoReference()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested);
            var instance         = new object();

            instance.SetAttachedValue(attachedProperty, 1, tested);
            instance = null;
            GC.Collect(0, GCCollectionMode.Forced, true);
            var instances = tested.GetInstances();

            Assert.Null(instance);
            Assert.Equal(0, instances.Count);
        }
예제 #16
0
        public void CanGetInstances()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested);
            var instance         = new object();

            instance.SetAttachedValue(attachedProperty, 1, tested);
            var instances = tested.GetInstances();

            Assert.Equal(1, instances.Count);
            Assert.All(instances, x =>
            {
                Assert.Same(instance, x);
            });
        }
예제 #17
0
        public void CanGetPropertiesForInstance()
        {
            var tested = new AttachedPropertyContext();

            var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested);
            var instance         = new object();

            instance.SetAttachedValue(attachedProperty, 1, tested);
            var values = tested.GetInstanceProperties(instance);

            Assert.Equal(1, values.Count);
            Assert.All(values, x =>
            {
                Assert.Same(attachedProperty, x.Key);
                Assert.Equal(1, x.Value);
            });
        }
예제 #18
0
        public void ThrowsOnCreateNullName()
        {
            var context = new AttachedPropertyContext();

            Assert.Throws <ArgumentNullException>("name", () => new AttachedProperty <object, object>(null, context));
        }
예제 #19
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="instance"></param>
        /// <param name="attachedProperty"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static TResult GetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, AttachedPropertyContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var value = context.GetInstanceValue(instance, attachedProperty);

            return(value);
        }
예제 #20
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="instance"></param>
        /// <param name="attachedProperty"></param>
        /// <param name="value"></param>
        /// <param name="context"></param>
        public static void SetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, TResult value, AttachedPropertyContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.SetInstanceValue(instance, attachedProperty, value);
        }
예제 #21
0
        /// <summary>
        ///
        /// </summary>
        protected AbstractAttachedProperty(Type ownerType, Type propertyType, string name, AttachedPropertyContext context)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var propertyInfo = ownerType.GetRuntimeProperty(name);

            if (propertyInfo != null)
            {
                throw new ArgumentException(string.Format("Type {0} already has a property named {1}", ownerType.FullName, name), nameof(name));
            }

            OwnerType    = ownerType;
            PropertyType = propertyType;
            Name         = name;

            context.Register(this);
        }