private object GetDefaultValue(Type type) { object value; if (_defaultValues.TryGetValue(type, out value)) { return(value); } if (!type.IsClass && !type.IsNullable()) { var mi = InfoOf.Method(() => GetDefaultValue <int>()); value = Expression.Lambda <Func <object> >( Expression.Convert( Expression.Call(mi.GetGenericMethodDefinition().MakeGenericMethod(type)), typeof(object))) .Compile()(); } _defaultValues[type] = value; return(value); }
public void ExtractingCtor() { var expected = typeof(User).GetConstructors().First(c => c.GetParameters().Length != 0); var ctor1 = InfoOf.Constructor(() => new User("", "")); var ctor2 = InfoOf <User> .Constructor(() => new User("", "")); Assert.AreEqual(expected, ctor1, "#1"); Assert.AreEqual(expected, ctor2, "#2"); }
public void ExtractingMethod1() { var expected = typeof(User).GetMethod(nameof(User.Debug)); var method1 = InfoOf.Method(() => new User().Debug()); var method2 = InfoOf.Method <User>(u => u.Debug()); var method3 = InfoOf <User> .Method(u => u.Debug()); Assert.AreEqual(expected, method1, "#1"); Assert.AreEqual(expected, method2, "#2"); Assert.AreEqual(expected, method3, "#3"); }
public void ExtractingDefaultStructCtor() { var ctor1 = InfoOf.Constructor <Guid>(); var ctor2 = InfoOf.Constructor(() => new Guid()); var ctor3 = InfoOf <Guid> .Constructor(() => new Guid()); var ctor4 = Expression.New(typeof(Guid)).Constructor; Assert.Null(ctor1, "#1"); Assert.Null(ctor2, "#2"); Assert.Null(ctor3, "#3"); Assert.Null(ctor4, "#4"); }
public void ExtractingPropertyGetMethod() { var expected = typeof(User).GetProperty("Name").GetMethod; var method1 = InfoOf.Method(() => new User().Name); var method2 = InfoOf.Method <User>(u => u.Name); var method3 = InfoOf.Method <User, string>(u => u.Name); var method4 = InfoOf <User> .Method(u => u.Name); Assert.AreEqual(expected, method1, "#1"); Assert.AreEqual(expected, method2, "#2"); Assert.AreEqual(expected, method3, "#3"); Assert.AreEqual(expected, method4, "#4"); }
public void ExtractingMethod2() { var expected = typeof(User).GetMethod(nameof(User.GetAge)); var method1 = InfoOf.Method(() => new User().GetAge()); var method2 = InfoOf.Method <User>(u => u.GetAge()); var method3 = InfoOf.Method <User, int>(u => u.GetAge()); var method4 = InfoOf <User> .Method(u => u.GetAge()); Assert.AreEqual(expected, method1, "#1"); Assert.AreEqual(expected, method2, "#2"); Assert.AreEqual(expected, method3, "#3"); Assert.AreEqual(expected, method4, "#4"); }
public void ExtractingFieldInfo() { var expected = typeof(User).GetField(nameof(User.LastName)); var info1 = InfoOf.Field(() => new User().LastName); var info2 = InfoOf.Field <User>(u => u.LastName); var info3 = InfoOf.Field <User, string>(u => u.LastName); var info4 = InfoOf <User> .Field(u => u.LastName); Assert.AreEqual(expected, info1, "#1"); Assert.AreEqual(expected, info2, "#2"); Assert.AreEqual(expected, info3, "#3"); Assert.AreEqual(expected, info4, "#4"); }
public void ExtractingPropertyInfo2() { var expected = typeof(User).GetProperty(nameof(User.Age)); var info1 = InfoOf.Property(() => new User().Age); var info2 = InfoOf.Property <User>(u => u.Age); var info3 = InfoOf.Property <User, int>(u => u.Age); var info4 = InfoOf <User> .Property(u => u.Age); Assert.AreEqual(expected, info1, "#1"); Assert.AreEqual(expected, info2, "#2"); Assert.AreEqual(expected, info3, "#3"); Assert.AreEqual(expected, info4, "#4"); }
public void ExtractingDefaultClassCtor() { var expected = typeof(User).GetConstructors().First(c => c.GetParameters().Length == 0); var ctor1 = InfoOf.Constructor <User>(); var ctor2 = InfoOf.Constructor(() => new User()); var ctor3 = InfoOf <User> .Constructor(() => new User()); var ctor4 = Expression.New(typeof(User)).Constructor; Assert.NotNull(ctor1, "#1"); Assert.NotNull(ctor2, "#2"); Assert.NotNull(ctor3, "#3"); Assert.NotNull(ctor4, "#4"); Assert.AreEqual(expected, ctor1); Assert.AreEqual(expected, ctor2); Assert.AreEqual(expected, ctor3); Assert.AreEqual(expected, ctor4); }