Skip to content

Commit

Permalink
Added Tests, Tupler, Fixed Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtule committed May 25, 2013
1 parent 3d90bf8 commit a4e628d
Show file tree
Hide file tree
Showing 44 changed files with 4,427 additions and 331 deletions.
Binary file modified .gitignore
Binary file not shown.
7 changes: 7 additions & 0 deletions Dynamitey.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{691EBA79
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{C33F07DB-7ACB-4081-92C2-BB739CB605C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SupportLibrary1", "SupportLibrary1\SupportLibrary1.csproj", "{348152A9-6A9C-4115-B60A-9E08CA72451D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,11 +30,16 @@ Global
{C33F07DB-7ACB-4081-92C2-BB739CB605C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C33F07DB-7ACB-4081-92C2-BB739CB605C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C33F07DB-7ACB-4081-92C2-BB739CB605C0}.Release|Any CPU.Build.0 = Release|Any CPU
{348152A9-6A9C-4115-B60A-9E08CA72451D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{348152A9-6A9C-4115-B60A-9E08CA72451D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{348152A9-6A9C-4115-B60A-9E08CA72451D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{348152A9-6A9C-4115-B60A-9E08CA72451D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C33F07DB-7ACB-4081-92C2-BB739CB605C0} = {691EBA79-CAA4-4670-BC8B-4537F990ADBF}
{348152A9-6A9C-4115-B60A-9E08CA72451D} = {691EBA79-CAA4-4670-BC8B-4537F990ADBF}
EndGlobalSection
EndGlobal
12 changes: 6 additions & 6 deletions Dynamitey/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class Builder
/// <returns></returns>
public static IImpromptuBuilder New()
{
return new BaseBuilder<ChainableDictionary>();
return new Builder<ChainableDictionary>();
}


Expand All @@ -43,7 +43,7 @@ public static IImpromptuBuilder New()
/// <returns></returns>
public static IImpromptuBuilder New<TObjectPrototype>() where TObjectPrototype : new()
{
return new BaseBuilder<TObjectPrototype>();
return new Builder<TObjectPrototype>();
}


Expand All @@ -54,10 +54,10 @@ public static IImpromptuBuilder New()
/// </summary>
public static class Build
{
private static readonly dynamic _objectBuilder = new BaseBuilder<ChainableDictionary>().Object;
private static readonly dynamic _objectBuilder = new Builder<ChainableDictionary>().Object;

private static readonly dynamic _listBuilder =
Dynamic.Curry(new BaseBuilder<ChainableDictionary>().ListSetup<List>()).
Dynamic.Curry(new Builder<ChainableDictionary>().ListSetup<List>()).
List();

/// <summary>
Expand Down Expand Up @@ -92,11 +92,11 @@ public static dynamic NewList
public static class Build<TObjectPrototype> where TObjectPrototype : new()
{
// ReSharper disable StaticFieldInGenericType
private static readonly dynamic _typedBuilder = new BaseBuilder<TObjectPrototype>().Object;
private static readonly dynamic _typedBuilder = new Builder<TObjectPrototype>().Object;
// ReSharper restore StaticFieldInGenericType

// ReSharper disable StaticFieldInGenericType
private static readonly dynamic _typedListBuilder = Dynamic.Curry(new BaseBuilder<TObjectPrototype>().ListSetup<TObjectPrototype>()).List();
private static readonly dynamic _typedListBuilder = Dynamic.Curry(new Builder<TObjectPrototype>().ListSetup<TObjectPrototype>()).List();
// ReSharper restore StaticFieldInGenericType

/// <summary>
Expand Down
211 changes: 105 additions & 106 deletions Dynamitey/Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
// limitations under the License.

using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;

using Dynamitey.Internal;
using Dynamitey.Internal.Optimization;
using Microsoft.CSharp.RuntimeBinder;
Expand Down Expand Up @@ -123,7 +123,7 @@ public static CallSite<T> CreateCallSite<T>(CallSiteBinder binder, String_OR_Inv
}


public static dynamic DynamicLinq(object enumerable)
public static dynamic Linq(object enumerable)
{
if(enumerable.GetType().GetInterfaces().Where(it=>it.IsGenericType)
.All(it => it.GetGenericTypeDefinition() != typeof(IEnumerable<>)))
Expand Down Expand Up @@ -274,8 +274,6 @@ public static dynamic InvokeUnaryOpartor(ExpressionType op, dynamic arg)
default:
throw new ArgumentException("Unsupported Operator", "op");
}

return null;
}

/// <summary>
Expand Down Expand Up @@ -559,14 +557,11 @@ public static dynamic InvokeGet(object target, string name)
CallSite tSite = null;
return InvokeHelper.InvokeGetCallSite(target, name, tContext, tStaticContext, ref tSite);
}
//#if SILVERLIGHT
// private static readonly Regex _chainRegex
// = new Regex(@"((\.?(?<Getter>\w+))|(\[(?<IntIndexer>\d+)\])|(\['(?<StringIndexer>\w+)'\]))");
//#else


private static readonly Regex _chainRegex
= new Regex(@"((\.?(?<Getter>\w+))|(\[(?<IntIndexer>\d+)\])|(\['(?<StringIndexer>\w+)'\]))");
//#endif


/// <summary>
/// Invokes the getter property chain.
/// </summary>
Expand Down Expand Up @@ -616,19 +611,8 @@ public static bool InvokeIsEvent(object target, string name)
CallSite tCallSite = null;
return InvokeHelper.InvokeIsEventCallSite(target, name, tContext, ref tCallSite);
}
/// <summary>
/// Invokes add assign with correct behavior for events.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
[Obsolete("Use InvokeAddAssignMember")]
public static void InvokeAddAssign(object target, string name, object value)
{
InvokeAddAssignMember(target, name, value);
}

/// <summary>
/// <summary>
/// Invokes add assign with correct behavior for events.
/// </summary>
/// <param name="target">The target.</param>
Expand All @@ -651,17 +635,6 @@ public static void InvokeAddAssignMember(object target, string name, object valu
InvokeHelper.InvokeAddAssignCallSite(target, name, args, argNames, context, staticContext, ref callSiteIsEvent, ref callSiteAdd, ref callSiteGet, ref callSiteSet);
}

/// <summary>
/// Invokes subtract assign with correct behavior for events.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
[Obsolete("use InvokeSubtractAssignMember instead")]
public static void InvokeSubtractAssign(object target, string name, object value)
{
InvokeSubtractAssignMember(target,name,value);
}
/// <summary>
/// Invokes subtract assign with correct behavior for events.
/// </summary>
Expand Down Expand Up @@ -709,6 +682,12 @@ public static dynamic InvokeConvert(object target, Type type, bool @explicit =fa

internal static readonly IDictionary<Type, Delegate> CompiledExpressions = new Dictionary<Type, Delegate>();

/// <summary>
/// Coerces any invokable to specificied delegate type.
/// </summary>
/// <param name="invokeableObject">The invokeable object.</param>
/// <param name="delegateType">Type of the delegate.</param>
/// <returns></returns>
public static dynamic CoerceToDelegate(object invokeableObject, Type delegateType)
{
if (!typeof(Delegate).IsAssignableFrom(delegateType.BaseType))
Expand Down Expand Up @@ -752,9 +731,68 @@ public static dynamic CoerceToDelegate(object invokeableObject, Type delegateTyp

}

private static readonly dynamic LateConvert = new DynamicObjects.LateType(typeof(Convert));


/// <summary>
/// Determines whether value is DBNull dynamically (Useful for PCL)
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if [is DBNull]; otherwise, <c>false</c>.
/// </returns>
public static bool IsDBNull(object value)
{

try
{
return LateConvert.IsDBNull(value);
}
catch
{
return false;
}
}

/// <summary>
/// Applies the equivalent type hint to dynamic object
/// </summary>
/// <param name="target">The target.</param>
/// <param name="types">The types.</param>
public static void ApplyEquivalentType(DynamicObjects.IEquivalentType target, params Type[] types)
{
if(types.Length == 1)
target.EquivalentType = types.First();
else
target.EquivalentType = new DynamicObjects.AggreType(types.ConvertAll<DynamicObjects.FauxType>().ToArray());

}

/// <summary>
/// Implicit or Explicit Converts the items of the specified enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <param name="explict">if set to <c>true</c> [explict].</param>
/// <returns></returns>
public static IEnumerable<T> ConvertAll<T>(this System.Collections.IEnumerable enumerable, bool explict =false)
{
return enumerable.Cast<Object>().Select(it => InvokeConvert(it, typeof (T), explict)).Cast<T>();
}

internal static readonly dynamic Impromptu
= new DynamicObjects.LateType("ImpromptuInterface.Impromptu, ImpromptuInterface, PublicKeyToken=0b1781c923b2975b");


/// <summary>
/// Goes the extra mile to convert target to type.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static dynamic CoerceConvert(object target, Type type)
{
if (target != null && !type.IsInstanceOfType(target)/* && DBNull.Value != target*/)
if (target != null && !type.IsInstanceOfType(target) && !IsDBNull(target))
{

var delegateConversion = CoerceToDelegate(target, type);
Expand All @@ -763,22 +801,26 @@ public static dynamic CoerceConvert(object target, Type type)
return delegateConversion;


//if (type.IsInterface)
//{
// if (target is IDictionary<string, object> && !(target is DynamicObjects.DictionaryBase))
// {
// target = new DynamicObjects.Dictionary((IDictionary<string, object>)target);
// }
// else
// {
// target = new ImpromptuGet(target);
// }
if (type.IsInterface && Impromptu.IsAvailable)
{



if (target is IDictionary<string, object> && !(target is DynamicObjects.BaseObject))
{
target = new DynamicObjects.Dictionary((IDictionary<string, object>)target);
}
else if(!(target is DynamicObjects.BaseObject))
{
target = new DynamicObjects.Get(target);
}


// target = Dynamic.DynamicActLike(target, type);
//}
//else
target = Impromptu.DynamicActLike(target, type);
}
else
{


try
{
Expand All @@ -791,76 +833,33 @@ public static dynamic CoerceConvert(object target, Type type)
catch (RuntimeBinderException)
{
Type tReducedType = type;
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
tReducedType = type.GetGenericArguments().First();
}

if (typeof (Enum).IsAssignableFrom(tReducedType) && target is string)
{
target = Enum.Parse(tReducedType, target as String, true);

if (target is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType) && !typeof(Enum).IsAssignableFrom(tReducedType))
}else if (target is IConvertible && typeof(IConvertible).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 = Dynamic.InvokeConstructor(type);
//}else if(!type.IsInstanceOfType(target) && DBNull.Value == target)
//{
// return null;
//}
else if (((target == null) || IsDBNull(target )) && type.IsValueType)
{
target = Dynamic.InvokeConstructor(type);
}
else if (!type.IsInstanceOfType(target) && IsDBNull(target))
{
return null;
}
return target;
}

Expand Down Expand Up @@ -943,8 +942,8 @@ public static IEnumerable<string> GetMemberNames(object target, bool dynamicOnly
tList.AddRange(tTarget.GetMetaObject(Expression.Constant(tTarget)).GetDynamicMemberNames());
}else
{
if(ComObjectType !=null && ComObjectType.IsInstanceOfType(target))

if (ComObjectType != null && ComObjectType.IsInstanceOfType(target) && ComBinder.IsAvailable)
{
tList.AddRange(ComBinder.GetDynamicDataMemberNames(target));
}
Expand Down
Loading

0 comments on commit a4e628d

Please sign in to comment.