Example #1
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src      = new AttributeSource();
            IFlagsAttribute flagsAtt = src.AddAttribute <IFlagsAttribute>();
            ITypeAttribute  typeAtt  = src.AddAttribute <ITypeAttribute>();

            flagsAtt.Flags = 1234;
            typeAtt.Type   = "TestType";

            AttributeSource    clone = src.CloneAttributes();
            IEnumerator <Type> it    = clone.GetAttributeClassesEnumerator();

            it.MoveNext();
            Assert.AreEqual(typeof(IFlagsAttribute), it.Current, "FlagsAttribute must be the first attribute");
            it.MoveNext();
            Assert.AreEqual(typeof(ITypeAttribute), it.Current, "TypeAttribute must be the second attribute");
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            IFlagsAttribute flagsAtt2 = clone.GetAttribute <IFlagsAttribute>();
            ITypeAttribute  typeAtt2  = clone.GetAttribute <ITypeAttribute>();

            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");

            // test copy back
            flagsAtt2.Flags = 4711;
            typeAtt2.Type   = "OtherType";
            clone.CopyTo(src);
            Assert.AreEqual(4711, flagsAtt.Flags, "FlagsAttribute of original must now contain updated term");
            Assert.AreEqual(typeAtt.Type, "OtherType", "TypeAttribute of original must now contain updated type");
            // verify again:
            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
 private void Copy(AttributeSource target, AttributeSource source)
 {
     if (target != source)
     {
         source.CopyTo(target);
     }
 }