public void GetDelegate_ValueType_DefaultCtor_Boxing() { var info = new ConstructorLookupInfo(typeof(int)); var actual = (Func <object>)info.GetDelegate(typeof(Func <object>)); var instance = actual(); Assert.That(instance, Is.EqualTo(new int())); }
public static T Create <T> (ParamList ctorArgs) { var info = new ConstructorLookupInfo(typeof(T)); var funcDelegate = info.GetDelegate(ctorArgs.FuncType); return((T)ctorArgs.InvokeFunc(funcDelegate)); }
public void DynamicInvoke_ValueType_NonDefaultCtor() { var info = new ConstructorLookupInfo(typeof(DateTime)); var instance = info.DynamicInvoke(new[] { typeof(int), typeof(int), typeof(int) }, new object[] { 2012, 01, 02 }); Assert.That(instance, Is.EqualTo(new DateTime(2012, 01, 02))); }
public void DynamicInvoke_ValueType_DefaultCtor() { var info = new ConstructorLookupInfo(typeof(int)); var instance = info.DynamicInvoke(new Type[0], new object[0]); Assert.That(instance, Is.EqualTo(new int())); }
public void GetDelegate_ValueType_NonDefaultCtor_BoxingInterface() { var info = new ConstructorLookupInfo(typeof(DateTime)); var actual = (Func <int, int, int, IComparable>)info.GetDelegate(typeof(Func <int, int, int, IComparable>)); var instance = actual(2012, 01, 02); Assert.That(instance, Is.EqualTo(new DateTime(2012, 01, 02))); }
public void GetDelegate_ImplicitConversion() { ConstructorLookupInfo lookupInfo = new ConstructorLookupInfo(typeof(TestClass)); var actual = (Func <Base, object>)lookupInfo.GetDelegate(typeof(Func <Base, object>)); var instance = actual(null); Assert.That(instance, Is.TypeOf <TestClass>()); }
public void GetDelegate_WithAbstractType_Throws() { var info = new ConstructorLookupInfo(typeof(AbstractClass), BindingFlags.NonPublic | BindingFlags.Instance); Assert.That( () => info.GetDelegate(typeof(Func <AbstractClass>)), Throws.InvalidOperationException.With.Message.EqualTo( "Cannot create an instance of 'Remotion.Extensions.UnitTests.Reflection.TestDomain.AbstractClass' because it is an abstract type.")); }
public void GetDelegate_WithExactMatchFromDerivedDerived() { ConstructorLookupInfo lookupInfo = new ConstructorLookupInfo(typeof(TestClass)); var actual = (Func <DerivedDerived, TestClass>)lookupInfo.GetDelegate(typeof(Func <DerivedDerived, TestClass>)); TestClass instance = actual(null); Assert.That(instance.InvocationType, Is.SameAs(typeof(Derived))); }
public void DynamicInvoke() { var info = new ConstructorLookupInfo(typeof(AbstractClass), BindingFlags.NonPublic | BindingFlags.Instance); Assert.That( () => info.DynamicInvoke(new Type[0], new object[0]), Throws.InvalidOperationException.With.Message.EqualTo( "Cannot create an instance of 'Remotion.Extensions.UnitTests.Reflection.TestDomain.AbstractClass' because it is an abstract type.")); }