Object Builder:创建简单对象

今天第一次用ObjectBuilder来创建简单对象,发现方法确实很简单。

[TestMethod]
        public void TestMethod1()
        {
            Builder builder = new Builder();
            //Locator locator = new Locator();
            //LifetimeContainer lContainer = new LifetimeContainer();
            //locator.Add(typeof(LifetimeContainer), lContainer);

            ConstructorPolicy conPolicy = new ConstructorPolicy();
            //conPolicy.AddParameter(new ValueParameter<int>(3));
            builder.Policies.Set<ICreationPolicy>(conPolicy, typeof(SimpleObject), null);
            var o = builder.BuildUp<SimpleObject>(null, null, null);

            Assert.AreEqual(-1, o.IniValue);

        }

        class SimpleObject
        {
            public readonly int IniValue = -2;
            public SimpleObject(int value)
            {
                IniValue = value;
            }
            public SimpleObject()
            {
                IniValue = -1;
            }
        }
 
结果是-1
奇怪的是,如果我不设置ICreationPolicy,结果居然是0。还希望哪位高手指教一下。

你可能感兴趣的:(builder)