using Microsoft.VisualStudio.TestTools.UnitTesting; using System; namespace Falcon.SugarApi.Test { [TestClass] public class IGetDataTimeNowTest { [TestMethod] public void nowTest() { var year = DateTime.Now.Year; Console.WriteLine($"base year {year}"); var ic1 = new c1(); Console.WriteLine($"c1 year {ic1.now.Year}"); var ic2 = new c2(); Console.WriteLine($"c2 year {ic2.now.Year}"); var ic3 = new c3(); Console.WriteLine($"c3 year {ic3.now.Year}"); Assert.IsTrue(ic1.now.Year == year); Assert.IsTrue(ic3.now.Year == year); Assert.IsTrue(ic2.now.Year == year + 1); } } public class c1 { public c1() { this.now = this is IGetDataTimeNow gdn ? gdn.GetDataTimeNow() : DateTime.Now; } public DateTime now { get; set; } } public class c2:c1, IGetDataTimeNow { public DateTime GetDataTimeNow() { return DateTime.Now.AddYears(1); } } public class c3:c1 { } }