public void TestIsNotEvent() { dynamic tDynamic = new ImpromptuDictionary(); tDynamic.Event = null; var tResult = Impromptu.InvokeIsEvent(tDynamic, "Event"); Assert.AreEqual(false, tResult); bool tTest = false; bool tTest2 = false; tDynamic.Event += new EventHandler <EventArgs>((@object, args) => { tTest = true; }); tDynamic.Event += new EventHandler <EventArgs>((@object, args) => { tTest2 = true; }); Assert.AreEqual(false, tTest); Assert.AreEqual(false, tTest2); tDynamic.Event(null, null); Assert.AreEqual(true, tTest); Assert.AreEqual(true, tTest2); }
public void DictionaryNullMethodsTest() { dynamic tNew = new ImpromptuDictionary(); ISimpleStringMethod tActsLike = Impromptu.ActLike <ISimpleStringMethod>(tNew); Assert.AreEqual(false, tActsLike.StartsWith("Te")); }
public void DictionaryNullPropertyTest() { dynamic tNew = new ImpromptuDictionary(); ISimpeleClassProps tActsLike = Impromptu.ActLike <ISimpeleClassProps>(tNew); Assert.AreEqual(default(string), tActsLike.Prop1); Assert.AreEqual(default(long), tActsLike.Prop2); Assert.AreEqual(default(Guid), tActsLike.Prop3); }
public void DictionaryNullPropertyTest() { dynamic tNew = new ImpromptuDictionary(); ISimpeleClassProps tActsLike = Impromptu.ActLike<ISimpeleClassProps>(tNew); Assert.AreEqual(default(string), tActsLike.Prop1); Assert.AreEqual(default(long), tActsLike.Prop2); Assert.AreEqual(default(Guid), tActsLike.Prop3); }
public void DictionaryPropertyTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Prop1 = "Test"; tNew.Prop2 = 42L; tNew.Prop3 = Guid.NewGuid(); var tActsLike = Impromptu.ActLike<ISimpeleClassProps>(tNew); Assert.AreEqual(tNew.Prop1, tActsLike.Prop1); Assert.AreEqual(tNew.Prop2, tActsLike.Prop2); Assert.AreEqual(tNew.Prop3, tActsLike.Prop3); }
public void DictIndexTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Prop1 = "Test"; tNew.Prop2 = "42"; tNew.Prop3 = Guid.NewGuid(); IObjectStringIndexer tActsLike = Impromptu.ActLike <IObjectStringIndexer>(tNew); Assert.AreEqual(tNew["Prop1"], tActsLike["Prop1"]); }
public void DictionaryPropertyTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Prop1 = "Test"; tNew.Prop2 = 42L; tNew.Prop3 = Guid.NewGuid(); var tActsLike = Impromptu.ActLike <ISimpeleClassProps>(tNew); Assert.AreEqual(tNew.Prop1, tActsLike.Prop1); Assert.AreEqual(tNew.Prop2, tActsLike.Prop2); Assert.AreEqual(tNew.Prop3, tActsLike.Prop3); }
public void DictionaryMethodsOutTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Func = new DynamicTryString(TestOut); String tOut; Assert.AreEqual(true, tNew.Func(null, "Test", out tOut)); Assert.AreEqual("Test", tOut); String tOut2; Assert.AreEqual(false, tNew.Func(null, 1, out tOut2)); Assert.AreEqual(null, tOut2); }
public void DictionaryMethodsTestWithPropertyAccess() { dynamic tNew = new ImpromptuDictionary(); tNew.PropCat = "Cat-"; tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action <bool>(Assert.IsFalse); tNew.Action3 = new ThisFunc <string>(@this => @this.PropCat + "test"); ISimpeleClassMeth tActsLike = Impromptu.ActLike <ISimpeleClassMeth>(tNew); AssertException <AssertionException>(tActsLike.Action1); AssertException <AssertionException>(() => tActsLike.Action2(true)); Assert.AreEqual("Cat-test", tActsLike.Action3()); }
public void TestCacheableIsEventAndIsNotEvent() { object tPoco = new PocoEvent(); var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); var tResult = tCachedInvoke.Invoke(tPoco); Assert.AreEqual(true, tResult); dynamic tDynamic = new ImpromptuDictionary(); tDynamic.Event = null; var tResult2 = tCachedInvoke.Invoke((object)tDynamic); Assert.AreEqual(false, tResult2); }
public void DynamicDictionaryWrappedTest() { var tDictionary = new Dictionary <string, object> { { "Test1", 1 }, { "Test2", 2 }, { "TestD", new Dictionary <string, object>() { { "TestA", "A" }, { "TestB", "B" } } } }; dynamic tNew = new ImpromptuDictionary(tDictionary); Assert.AreEqual(1, tNew.Test1); Assert.AreEqual(2, tNew.Test2); Assert.AreEqual("A", tNew.TestD.TestA); Assert.AreEqual("B", tNew.TestD.TestB); }
internal static bool MassageResultBasedOnInterface(this ImpromptuObject target, string binderName, bool resultFound, ref object result) { if (result is ImpromptuForwarderAddRemove) //Don't massage AddRemove Proxies { return(true); } Type tType; var tTryType = target.TryTypeForName(binderName, out tType); if (tTryType && tType == typeof(void)) { return(true); } if (resultFound) { if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase) && (!tTryType || tType == typeof(object))) { result = new ImpromptuDictionary((IDictionary <string, object>)result); } else if (tTryType) { result = Impromptu.CoerceConvert(result, tType); } } else { result = null; if (!tTryType) { return(false); } if (tType.GetTypeInfo().IsValueType) { result = Impromptu.InvokeConstructor(tType); } } return(true); }
public void DictionaryMethodsTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action <bool>(Assert.IsFalse); tNew.Action3 = new Func <string>(() => "test"); tNew.Action4 = new Func <int, string>(arg => "test" + arg); ISimpeleClassMeth2 tActsLike = Impromptu.ActLike <ISimpeleClassMeth2>(tNew); AssertException <AssertionException>(tActsLike.Action1); AssertException <AssertionException>(() => tActsLike.Action2(true)); Assert.AreEqual("test", tActsLike.Action3()); Assert.AreEqual("test4", tActsLike.Action4(4)); }
public void ForwardMethodsTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action <bool>(Assert.IsFalse); tNew.Action3 = new Func <string>(() => "test"); tNew.Action4 = new Func <int, string>(arg => "test" + arg); dynamic tFwd = new TestForwarder(tNew); AssertException <AssertionException>(() => tFwd.Action1()); AssertException <AssertionException>(() => tFwd.Action2(true)); Assert.AreEqual("test", tFwd.Action3()); Assert.AreEqual("test4", tFwd.Action4(4)); }
public void DynamicObjectEqualsTest() { var tDictionary = new Dictionary <string, object> { { "Test1", 1 }, { "Test2", 2 }, { "TestD", new Dictionary <string, object>() { { "TestA", "A" }, { "TestB", "B" } } } }; dynamic tDynamic = ImpromptuDictionary.Create <IDynamicDict>(tDictionary); dynamic tNotDynamic = ImpromptuDictionary.Create <INonDynamicDict>(tDictionary); Assert.AreEqual(tDynamic, tNotDynamic); Assert.AreEqual(tDynamic, tDictionary); Assert.AreEqual(tNotDynamic, tDictionary); }
public void InterfaceDictionaryWrappedTest() { var tDictionary = new Dictionary <string, object> { { "Test1", 1 }, { "Test2", 2L }, { "Test3", 1 }, { "Test4", "Two" }, { "TestD", new Dictionary <string, object>() { { "TestA", "A" }, { "TestB", "B" } } } }; dynamic tDynamic = ImpromptuDictionary.Create <IDynamicDict>(tDictionary); dynamic tNotDynamic = ImpromptuDictionary.Create <INonDynamicDict>(tDictionary); Assert.AreEqual(tDynamic, tNotDynamic); Assert.AreEqual(1, tDynamic.Test1); Assert.AreEqual(2L, tDynamic.Test2); Assert.AreEqual(TestEnum.One, tDynamic.Test3); Assert.AreEqual(TestEnum.Two, tDynamic.Test4); Assert.AreEqual("A", tDynamic.TestD.TestA); Assert.AreEqual("B", tDynamic.TestD.TestB); Assert.AreEqual(1, tNotDynamic.Test1); Assert.AreEqual(2L, tNotDynamic.Test2); Assert.AreEqual(TestEnum.One, tNotDynamic.Test3); Assert.AreEqual(TestEnum.Two, tNotDynamic.Test4); Assert.AreEqual(typeof(Dictionary <string, object>), tNotDynamic.TestD.GetType()); Assert.AreEqual(typeof(ImpromptuDictionary), tDynamic.TestD.GetType()); }
public void TestIsNotEvent() { dynamic tDynamic = new ImpromptuDictionary(); tDynamic.Event = null; var tResult = Impromptu.InvokeIsEvent(tDynamic, "Event"); Assert.AreEqual(false, tResult); bool tTest = false; bool tTest2 = false; tDynamic.Event += new EventHandler<EventArgs>((@object, args) => { tTest = true; }); tDynamic.Event += new EventHandler<EventArgs>((@object, args) => { tTest2 = true; }); Assert.AreEqual(false, tTest); Assert.AreEqual(false, tTest2); tDynamic.Event(null, null); Assert.AreEqual(true, tTest); Assert.AreEqual(true, tTest2); }
public void TestCacheableIsEventAndIsNotEvent() { object tPoco = new PocoEvent(); var tCachedInvoke = new CacheableInvocation(InvocationKind.IsEvent, "Event"); var tResult = tCachedInvoke.Invoke(tPoco); Assert.AreEqual(true, tResult); dynamic tDynamic = new ImpromptuDictionary(); tDynamic.Event = null; var tResult2 = tCachedInvoke.Invoke((object) tDynamic); Assert.AreEqual(false, tResult2); }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other"> The other. </param> /// <returns> </returns> public bool Equals(ImpromptuDictionary other) { if (ReferenceEquals(null, other)) { return false; } if (ReferenceEquals(this, other)) { return true; } return Equals(other._dictionary, _dictionary); }
public void TestDynamicMemberNamesImpromput() { ImpromptuDictionary tDict = Build.NewObject(Two: 2); Assert.AreEqual("Two", Impromptu.GetMemberNames(tDict, dynamicOnly: true).Single()); }
public void DictIndexTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Prop1 = "Test"; tNew.Prop2 = "42"; tNew.Prop3 = Guid.NewGuid(); IObjectStringIndexer tActsLike = Impromptu.ActLike<IObjectStringIndexer>(tNew); Assert.AreEqual(tNew["Prop1"], tActsLike["Prop1"]); }
public void DictionaryMethodsTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action<bool>(Assert.IsFalse); tNew.Action3 = new Func<string>(() => "test"); tNew.Action4 = new Func<int, string>(arg => "test" + arg); ISimpeleClassMeth2 tActsLike = Impromptu.ActLike<ISimpeleClassMeth2>(tNew); AssertException<AssertionException>(tActsLike.Action1); AssertException<AssertionException>(() => tActsLike.Action2(true)); Assert.AreEqual("test", tActsLike.Action3()); Assert.AreEqual("test4", tActsLike.Action4(4)); }
public void ForwardMethodsTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action<bool>(Assert.IsFalse); tNew.Action3 = new Func<string>(() => "test"); tNew.Action4 = new Func<int, string>(arg => "test" + arg); dynamic tFwd = new TestForwarder(tNew); AssertException<AssertionException>(()=> tFwd.Action1()); AssertException<AssertionException>(() => tFwd.Action2(true)); Assert.AreEqual("test", tFwd.Action3()); Assert.AreEqual("test4", tFwd.Action4(4)); }
public void DictionaryMethodsOutTest() { dynamic tNew = new ImpromptuDictionary(); tNew.Func = new DynamicTryString(TestOut); String tOut; Assert.AreEqual(true, tNew.Func(null, "Test", out tOut)); Assert.AreEqual("Test", tOut); String tOut2; Assert.AreEqual(false, tNew.Func(null, 1, out tOut2)); Assert.AreEqual(null, tOut2); }
public void DictionaryMethodsTestWithPropertyAccess() { dynamic tNew = new ImpromptuDictionary(); tNew.PropCat = "Cat-"; tNew.Action1 = new Action(Assert.Fail); tNew.Action2 = new Action<bool>(Assert.IsFalse); tNew.Action3 = new ThisFunc<string>(@this => @this.PropCat + "test"); ISimpeleClassMeth tActsLike = Impromptu.ActLike<ISimpeleClassMeth>(tNew); AssertException<AssertionException>(tActsLike.Action1); AssertException<AssertionException>(() => tActsLike.Action2(true)); Assert.AreEqual("Cat-test", tActsLike.Action3()); }
public void DictionaryNullMethodsTest() { dynamic tNew = new ImpromptuDictionary(); ISimpleStringMethod tActsLike = Impromptu.ActLike<ISimpleStringMethod>(tNew); Assert.AreEqual(false, tActsLike.StartsWith("Te")); }
public void DynamicDictionaryWrappedTest() { var tDictionary = new Dictionary<string, object> { {"Test1", 1}, {"Test2", 2}, {"TestD", new Dictionary<string,object>() { {"TestA","A"}, {"TestB","B"} } } }; dynamic tNew = new ImpromptuDictionary(tDictionary); Assert.AreEqual(1, tNew.Test1); Assert.AreEqual(2, tNew.Test2); Assert.AreEqual("A", tNew.TestD.TestA); Assert.AreEqual("B", tNew.TestD.TestB); }
public static dynamic CoerceConvert(object target, Type type) { if (target != null && !type.IsInstanceOfType(target) && DBNull.Value != target) { var delegateConversion = CoerceToDelegate(target, type); if (delegateConversion != null) { return(delegateConversion); } if (type.IsInterface) { if (target is IDictionary <string, object> && !(target is ImpromptuDictionaryBase)) { target = new ImpromptuDictionary((IDictionary <string, object>)target); } else { target = new ImpromptuGet(target); } target = Impromptu.DynamicActLike(target, type); } else { try { object tResult; tResult = Impromptu.InvokeConvert(target, type, @explicit: true); target = tResult; } catch (RuntimeBinderException) { Type tReducedType = type; if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { tReducedType = type.GetGenericArguments().First(); } if (target is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType) && !typeof(Enum).IsAssignableFrom(tReducedType)) { target = Convert.ChangeType(target, tReducedType, Thread.CurrentThread.CurrentCulture); } else { //finally check type converter since it's the slowest. #if !SILVERLIGHT var tConverter = TypeDescriptor.GetConverter(tReducedType); #else TypeConverter tConverter = null; var tAttributes = tReducedType.GetCustomAttributes(typeof(TypeConverterAttribute), false); var tAttribute = tAttributes.OfType <TypeConverterAttribute>().FirstOrDefault(); if (tAttribute != null) { tConverter = Impromptu.InvokeConstructor(Type.GetType(tAttribute.ConverterTypeName)); } #endif if (tConverter != null && tConverter.CanConvertFrom(target.GetType())) { target = tConverter.ConvertFrom(target); } #if SILVERLIGHT else if (target is string) { var tDC = new SilverConvertertDC(target as String); var tFE = new SilverConverterFE { DataContext = tDC }; var tProp = SilverConverterFE.GetProperty(tReducedType); tFE.SetBinding(tProp, new System.Windows.Data.Binding("StringValue")); var tResult = tFE.GetValue(tProp); if (tResult != null) { target = tResult; } } #endif } } } } else if (((target == null) || target == DBNull.Value) && type.IsValueType) { target = Impromptu.InvokeConstructor(type); } else if (!type.IsInstanceOfType(target) && DBNull.Value == target) { return(null); } return(target); }
internal static bool MassageResultBasedOnInterface(this ImpromptuObject target, string binderName, bool resultFound, ref object result) { if (result is ImpromptuForwarderAddRemove) //Don't massage AddRemove Proxies { return(true); } Type tType; var tTryType = target.TryTypeForName(binderName, out tType); if (tTryType && tType == typeof(void)) { return(true); } if (resultFound) { if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase) && (!tTryType || tType == typeof(object))) { result = new ImpromptuDictionary((IDictionary <string, object>)result); } else if (tTryType) { if (result != null && !tType.IsAssignableFrom(result.GetType())) { if (tType.IsInterface) { if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase)) { result = new ImpromptuDictionary((IDictionary <string, object>)result); } else { result = new ImpromptuGet(result); } result = Impromptu.DynamicActLike(result, tType); } else { try { object tResult; tResult = Impromptu.InvokeConvert(target, tType, explict: true); result = tResult; } catch (RuntimeBinderException) { Type tReducedType = tType; if (tType.IsGenericType && tType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { tReducedType = tType.GetGenericArguments().First(); } if (result is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType)) { result = Convert.ChangeType(result, tReducedType, Thread.CurrentThread.CurrentCulture); } else { //finally check type converter since it's the slowest. #if !SILVERLIGHT var tConverter = TypeDescriptor.GetConverter(tType); #else TypeConverter tConverter = null; var tAttributes = tType.GetCustomAttributes(typeof(TypeConverterAttribute), false); var tAttribute = tAttributes.OfType <TypeConverterAttribute>().FirstOrDefault(); if (tAttribute != null) { tConverter = Impromptu.InvokeConstructor(Type.GetType(tAttribute.ConverterTypeName)); } #endif if (tConverter != null && tConverter.CanConvertFrom(result.GetType())) { result = tConverter.ConvertFrom(result); } #if SILVERLIGHT else if (result is string) { var tDC = new SilverConvertertDC(result as String); var tFE = new SilverConverterFE { DataContext = tDC }; var tProp = SilverConverterFE.GetProperty(tType); tFE.SetBinding(tProp, new System.Windows.Data.Binding("StringValue")); var tResult = tFE.GetValue(tProp); if (tResult != null) { result = tResult; } } #endif } } } } else if (result == null && tType.IsValueType) { result = Impromptu.InvokeConstructor(tType); } } } else { result = null; if (!tTryType) { return(false); } if (tType.IsValueType) { result = Impromptu.InvokeConstructor(tType); } } return(true); }