예제 #1
0
        public void AbstractFamilyTreeWithTypes()
        {
            ConcreteOf2Abstracts ca  = ConcreteOf2Abstracts.Create(1, 2, 3, 4, 5, 6);
            Abstract1            a1a = ca;
            Abstract2            a2a = ca;

            ConcreteOf2Abstracts cb = ca.With(abstract1Field1: 7, abstract2Field1: 8, concreteField1: 9);

            Assert.Equal(7, cb.Abstract1Field1);
            Assert.Equal(2, cb.Abstract1Field2);
            Assert.Equal(8, cb.Abstract2Field1);
            Assert.Equal(4, cb.Abstract2Field2);
            Assert.Equal(9, cb.ConcreteField1);
            Assert.Equal(6, cb.ConcreteField2);

            Abstract1 a1b = a1a.With(abstract1Field1: 7);

            Assert.Equal(7, a1b.Abstract1Field1);
            Assert.Equal(2, a1b.Abstract1Field2);
            Assert.Equal(3, ((ConcreteOf2Abstracts)a1b).Abstract2Field1);
            Assert.Equal(4, ((ConcreteOf2Abstracts)a1b).Abstract2Field2);
            Assert.Equal(5, ((ConcreteOf2Abstracts)a1b).ConcreteField1);
            Assert.Equal(6, ((ConcreteOf2Abstracts)a1b).ConcreteField2);

            Abstract2 a2b = a2a.With(abstract1Field1: 7, abstract2Field1: 8);

            Assert.Equal(7, a2b.Abstract1Field1);
            Assert.Equal(2, a2b.Abstract1Field2);
            Assert.Equal(8, a2b.Abstract2Field1);
            Assert.Equal(4, a2b.Abstract2Field2);
            Assert.Equal(5, ((ConcreteOf2Abstracts)a2b).ConcreteField1);
            Assert.Equal(6, ((ConcreteOf2Abstracts)a2b).ConcreteField2);
        }
        public virtual ConcreteOf2Abstracts ToConcreteOf2Abstracts(
            ImmutableObjectGraph.Optional <System.Int32> abstract2Field1 = default(ImmutableObjectGraph.Optional <System.Int32>),
            ImmutableObjectGraph.Optional <System.Int32> abstract2Field2 = default(ImmutableObjectGraph.Optional <System.Int32>),
            ImmutableObjectGraph.Optional <System.Int32> concreteField1  = default(ImmutableObjectGraph.Optional <System.Int32>),
            ImmutableObjectGraph.Optional <System.Int32> concreteField2  = default(ImmutableObjectGraph.Optional <System.Int32>))
        {
            ConcreteOf2Abstracts that = this as ConcreteOf2Abstracts;

            if (that != null && this.GetType().IsEquivalentTo(typeof(ConcreteOf2Abstracts)))
            {
                if ((!abstract2Field1.IsDefined || abstract2Field1.Value == that.Abstract2Field1) &&
                    (!abstract2Field2.IsDefined || abstract2Field2.Value == that.Abstract2Field2) &&
                    (!concreteField1.IsDefined || concreteField1.Value == that.ConcreteField1) &&
                    (!concreteField2.IsDefined || concreteField2.Value == that.ConcreteField2))
                {
                    return(that);
                }
            }

            return(ConcreteOf2Abstracts.CreateWithIdentity(
                       abstract1Field1: Optional.For(this.Abstract1Field1),
                       abstract1Field2: Optional.For(this.Abstract1Field2),
                       identity: this.Identity,
                       abstract2Field1: abstract2Field1,
                       abstract2Field2: abstract2Field2,
                       concreteField1: concreteField1,
                       concreteField2: concreteField2));
        }