Browse files

Better coercian.

  • Loading branch information...
1 parent 1efdced commit 48aba940268ace850a091ab60684c5c97de08a7b jayt committed Jul 24, 2012
View
4 ImpromptuInterface/Properties/AssemblyInfo.cs
@@ -59,10 +59,10 @@
#if SILVERLIGHT
-[assembly: AssemblyVersion("5.8.0.0")]
+[assembly: AssemblyVersion("5.9.0.0")]
#else
-[assembly: AssemblyVersion("5.8.0.*")]
+[assembly: AssemblyVersion("5.9.0.*")]
#endif
View
106 ImpromptuInterface/src/Impromptu.cs
@@ -19,10 +19,13 @@
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
+using System.Threading;
using ImpromptuInterface.Build;
+using ImpromptuInterface.Dynamic;
using ImpromptuInterface.Internal;
using ImpromptuInterface.InvokeExt;
using ImpromptuInterface.Optimization;
+using Microsoft.CSharp.RuntimeBinder;
namespace ImpromptuInterface
{
@@ -609,6 +612,109 @@ public static dynamic InvokeConvert(object target, Type type, bool @explicit =fa
}
+ public static dynamic CoerceConvert(object target, Type type)
+ {
+ if (target != null && !type.IsInstanceOfType(target))
+ {
+
+ 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 && type.IsValueType)
+ {
+ target = Impromptu.InvokeConstructor(type);
+ }
+ return target;
+ }
+
/// <summary>
/// (Obsolete)Invokes the constructor. misspelling
/// </summary>
View
103 ImpromptuInterface/src/Optimization/Util.cs
@@ -120,6 +120,7 @@ public static Type FixContext(this Type context)
return context;
}
+
internal static bool MassageResultBasedOnInterface(this ImpromptuObject target, string binderName, bool resultFound, ref object result)
{
if (result is ImpromptuForwarderAddRemove) //Don't massage AddRemove Proxies
@@ -138,104 +139,10 @@ internal static bool MassageResultBasedOnInterface(this ImpromptuObject target,
{
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, @explicit: 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 if (tTryType)
+ {
+ result = Impromptu.CoerceConvert(result, tType);
+ }
}
else
{
View
2 NuGetBuild/Core/ImpromptuInterface.MetaData.xml
@@ -1,6 +1,6 @@
<metadata>
<id>ImpromptuInterface</id>
- <version>5.8.0</version>
+ <version>5.9.0</version>
<authors>Jay Tuley</authors>
<owners>Jay Tuley</owners>
<description>A Lightweight Duck Casting Framework for dynamic C# 4.0 (.net4 &amp; silverlight4). Includes helper methods for dynamic invocation via method and property names known only at runtime (runs faster than reflection).</description>
View
8 Tests/UnitTestImpromptuInterface/ImpromptuDynamic.cs
@@ -311,6 +311,8 @@ public void InterfaceDictionaryWrappedTest()
{
{"Test1", 1},
{"Test2", 2L},
+ {"Test3",1},
+ {"Test4","Two"},
{"TestD", new Dictionary<string,object>()
{
{"TestA","A"},
@@ -326,12 +328,16 @@ public void InterfaceDictionaryWrappedTest()
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());
View
15 Tests/UnitTestImpromptuInterface/Support/SupportDefinitions.cs
@@ -156,12 +156,23 @@ public PocoOptConstructor(string one="-1", string two="-2", string three="-3")
}
}
+ public enum TestEnum
+ {
+ None,
+ One,
+ Two
+ }
+
public interface IDynamicDict
{
int Test1 { get; }
long Test2 { get; }
+ TestEnum Test3 { get; }
+
+ TestEnum Test4 { get; }
+
dynamic TestD { get; }
}
@@ -171,6 +182,10 @@ public interface INonDynamicDict
long Test2 { get; }
+ TestEnum Test3 { get; }
+
+ TestEnum Test4 { get; }
+
IDictionary<string,object> TestD { get; }
}

0 comments on commit 48aba94

Please sign in to comment.