public void BasicWithIAPSType()
        {
            ImplementsIAPS a = new ImplementsIAPS();

            object value;

            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value), "We expect the store doesn't have propertyOne for 'a' yet");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyTwo, out value), "We expect the store doesn't have propertyTwo for 'a' yet");

            IAttachedPropertyStore aAsIAPS = a as IAttachedPropertyStore;

            Assert.IsNotNull(aAsIAPS);
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyOne, out value), "We expect the type does not have propertyOne yet");
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyTwo, out value), "We expect the type does not have propertyTwo yet");

            AttachablePropertyServices.SetProperty(a, propertyOne, "Foo Bar");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(a) == 1);
            var props = new KeyValuePair <AttachableMemberIdentifier, object> [1];

            AttachablePropertyServices.CopyPropertiesTo(a, props, 0);
            Assert.IsTrue((string)props[0].Value == "Foo Bar");
            Assert.IsTrue(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value) && (string)value == "Foo Bar", "We should now find the property");
            Assert.IsTrue(aAsIAPS.TryGetProperty(propertyOne, out value) && (string)value == "Foo Bar", "And it's storage should be local on the instance");

            string valueAsString;

            Assert.IsTrue(AttachablePropertyServices.TryGetProperty(a, propertyOne, out valueAsString) && valueAsString == "Foo Bar");

            Assert.IsTrue(AttachablePropertyServices.RemoveProperty(a, propertyOne), "We should be able to remove the property");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value), "We should not have the property anymore");
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyOne, out value), "Even the local copy should not have the property anymore");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(a) == 0);
        }
Exemplo n.º 2
0
        public static bool TryGetProperty <T>(object instance, AttachableMemberIdentifier name, out T value)
        {
            if (instance == null)
            {
                value = default(T);
                return(false);
            }

            IAttachedPropertyStore ap = instance as IAttachedPropertyStore;

            if (ap != null)
            {
                object obj;
                bool   result = ap.TryGetProperty(name, out obj);
                if (result)
                {
                    if (obj is T)
                    {
                        value = (T)obj;
                        return(true);
                    }
                }
                value = default(T);
                return(false);
            }

            return(attachedProperties.TryGetProperty(instance, name, out value));
        }
Exemplo n.º 3
0
            public static int GetSound(IAttachedPropertyStore element)
            {
                object val;

                if (element.TryGetProperty(SoundProperty, out val))
                {
                    return((int)val);
                }
                return(0);
            }
Exemplo n.º 4
0
        public static decimal GetVersion(IAttachedPropertyStore element)
        {
            object val;

            if (element.TryGetProperty(VersionProperty, out val))
            {
                return((decimal)val);
            }
            return(0);
        }
Exemplo n.º 5
0
        public static bool TryGetProperty <T>(object instance, AttachableMemberIdentifier name, out T value)
        {
            object obj2;

            if (instance == null)
            {
                value = default(T);
                return(false);
            }
            IAttachedPropertyStore store = instance as IAttachedPropertyStore;

            if (store == null)
            {
                return(attachedProperties.TryGetProperty <T>(instance, name, out value));
            }
            if (store.TryGetProperty(name, out obj2) && (obj2 is T))
            {
                value = (T)obj2;
                return(true);
            }
            value = default(T);
            return(false);
        }