public static IEnumerable <Project> GetProjects(string solutionFile, string configurationName, string platformName) { var projects = new List <Project>(); var solutionParser = ((object)Impromptu.InvokeConstructor(SolutionParserType)).ActLike <ISolutionParser>(); using (var streamReader = new StreamReader(solutionFile)) { solutionParser.SolutionReader = streamReader; solutionParser.ParseSolution(); var solutionDirectory = Path.GetDirectoryName(solutionFile); var projectsInSolution = solutionParser.Projects.AllActLike <IProjectInSolution>(); foreach (var projectInSolution in projectsInSolution) { var isKnownToBeMsBuildFormat = ObjectHelper.AreEqual(projectInSolution.ProjectType, KnownToBeMsBuildFormat); var isSelectedForBuild = ProjectIsSelectedForBuild(projectInSolution, configurationName, platformName); if (!isKnownToBeMsBuildFormat || !isSelectedForBuild) { continue; } var relativePath = projectInSolution.RelativePath; var projectFile = Path.Combine(solutionDirectory, relativePath); var project = LoadProject(projectFile, configurationName, platformName, solutionDirectory); if (project != null) { projects.Add(project); } } } return(projects); }
public void TestConstructOptional() { PocoOptConstructor tCast = Impromptu.InvokeConstructor(typeof(PocoOptConstructor), "3".WithArgumentName("three")); Assert.AreEqual("-1", tCast.One); Assert.AreEqual("-2", tCast.Two); Assert.AreEqual("3", tCast.Three); }
public void TestConstruct() { var tCast = Impromptu.InvokeConstructor(typeof(List <object>), new object[] { new string[] { "one", "two", "three" } }); Assert.AreEqual("two", tCast[1]); }
public void TestConstructorTimed() { var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeConstructor(typeof(Tuple <string>), "Test"); }); var tWatch2 = TimeIt.Go(() => { var tOut = Activator.CreateInstance(typeof(Tuple <string>), "Test"); }); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + tWatch2.Elapsed); TestContext.WriteLine("Impromptu VS Reflection: {0:0.0} x faster", (double)tWatch2.Elapsed.Ticks / tWatch.Elapsed.Ticks); Assert.Less(tWatch.Elapsed, tWatch2.Elapsed); }
public void TestConstructorValueTypeTimed() { var tIter = 1000000; var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeConstructor(typeof(DateTime), 2010, 1, 20); }, tIter); var tWatch2 = TimeIt.Go(() => { var tOut = Activator.CreateInstance(typeof(DateTime), 2010, 1, 20); }, tIter); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + tWatch2.Elapsed); TestContext.WriteLine("Impromptu VS Reflection: {0:0.0} x faster", (double)tWatch2.Elapsed.Ticks / tWatch.Elapsed.Ticks); Assert.Less(tWatch.Elapsed, tWatch2.Elapsed); }
public void TestOptionalArgumentActivationNoneAndCacheable() { AssertException <MissingMethodException>(() => Activator.CreateInstance <ImpromptuList>()); var tList = Impromptu.InvokeConstructor(typeof(ImpromptuList)); Assert.AreEqual(typeof(ImpromptuList), tList.GetType()); var tCachedInvoke = new CacheableInvocation(InvocationKind.Constructor); var tList1 = tCachedInvoke.Invoke(typeof(ImpromptuList)); Assert.AreEqual(typeof(ImpromptuList), tList1.GetType()); }
private static object InvokeHelper(CallInfo callinfo, IList <object> args, Activate buildType = null) { bool tSetWithName = true; object tArg = null; if (callinfo.ArgumentNames.Count == 0 && callinfo.ArgumentCount == 1) { tArg = args[0]; if (Util.IsAnonymousType(tArg) || tArg is IEnumerable <KeyValuePair <string, object> > ) { tSetWithName = false; } } if (tSetWithName && callinfo.ArgumentNames.Count != callinfo.ArgumentCount) { throw new ArgumentException("Requires argument names for every argument"); } object result; if (buildType != null) { result = buildType.Create(); } else { try { result = Activator.CreateInstance <TObjectProtoType>();//Try first because faster but doens't work with optional parameters } catch (MissingMethodException) { result = Impromptu.InvokeConstructor(typeof(TObjectProtoType)); } } if (tSetWithName) { tArg = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair <string, object>(n, a)); } return(Impromptu.InvokeSetAll(result, tArg)); }
public void TestConstructorNoARgTimed() { var tWatch = TimeIt.Go(() => { var tOut = Impromptu.InvokeConstructor(typeof(List <string>)); }); var tWatch2 = TimeIt.Go(() => { var tOut = Activator.CreateInstance(typeof(List <string>)); }); var tWatch3 = TimeIt.Go(() => { var tOut = Activator.CreateInstance <List <string> >(); }); TestContext.WriteLine("Impromptu: " + tWatch.Elapsed); TestContext.WriteLine("Refelection: " + tWatch2.Elapsed); TestContext.WriteLine("Refelection Generic: " + tWatch3.Elapsed); TestContext.WriteLine("Impromptu VS Reflection: {0:0.0} x faster", (double)tWatch2.Elapsed.Ticks / tWatch.Elapsed.Ticks); Assert.Ignore("I don't think this is beatable at the moment"); Assert.Less(tWatch.Elapsed, tWatch2.Elapsed); }
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 override bool TryGetMember(GetMemberBinder binder, out object result) { var tGroup = _match.Groups[binder.Name]; Type outType; if (!TryTypeForName(binder.Name, out outType)) { outType = typeof(string); } if (!tGroup.Success) { result = null; if (outType.GetTypeInfo().IsValueType) { result = Impromptu.InvokeConstructor(outType); } return(true); } result = Impromptu.CoerceConvert(tGroup.Value, outType); return(true); }
public void TestConstructNullableprimativetype() { var tCast = Impromptu.InvokeConstructor(typeof(Nullable <Int32>)); Assert.AreEqual(null, tCast); }
/// <summary> /// Invokes the invocation on specified target with specific args. /// </summary> /// <param name="target">The target.</param> /// <param name="args">The args.</param> /// <returns></returns> public virtual object Invoke(object target, params object[] args) { switch (Kind) { case InvocationKind.Constructor: return(Impromptu.InvokeConstructor((Type)target, args)); case InvocationKind.Convert: bool tExplict = false; if (Args.Length == 2) { tExplict = (bool)args[1]; } return(Impromptu.InvokeConvert(target, (Type)args[0], tExplict)); case InvocationKind.Get: return(Impromptu.InvokeGet(target, Name.Name)); case InvocationKind.Set: Impromptu.InvokeSet(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.GetIndex: return(Impromptu.InvokeGetIndex(target, args)); case InvocationKind.SetIndex: Impromptu.InvokeSetIndex(target, args); return(null); case InvocationKind.InvokeMember: return(Impromptu.InvokeMember(target, Name, args)); case InvocationKind.InvokeMemberAction: Impromptu.InvokeMemberAction(target, Name, args); return(null); case InvocationKind.InvokeMemberUnknown: { try { return(Impromptu.InvokeMember(target, Name, args)); } catch (RuntimeBinderException) { Impromptu.InvokeMemberAction(target, Name, args); return(null); } } case InvocationKind.Invoke: return(Impromptu.Invoke(target, args)); case InvocationKind.InvokeAction: Impromptu.InvokeAction(target, args); return(null); case InvocationKind.InvokeUnknown: { try { return(Impromptu.Invoke(target, args)); } catch (RuntimeBinderException) { Impromptu.InvokeAction(target, args); return(null); } } case InvocationKind.AddAssign: Impromptu.InvokeAddAssignMember(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.SubtractAssign: Impromptu.InvokeSubtractAssignMember(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.IsEvent: return(Impromptu.InvokeIsEvent(target, Name.Name)); default: throw new InvalidOperationException("Unknown Invocation Kind: " + Kind); } }
/// <summary> /// Override on DynamicObject /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryConvert(ConvertBinder binder, out object result) { result = Impromptu.InvokeConstructor(binder.ReturnType); return(true); }
/// <summary> /// Tries to invoke. /// </summary> /// <param name="binder"></param> /// <param name="args"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) { result = Impromptu.InvokeConstructor(_type, Util.NameArgsIfNecessary(binder.CallInfo, args)); return(true); }
/// <summary> /// Creates this instance. /// </summary> /// <returns></returns> public virtual dynamic Create() { object[] tArgs = Arguments(); return(Impromptu.InvokeConstructor(Type, tArgs)); }
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); }
public void TestConstructOBjectNoParams() { var tCast = Impromptu.InvokeConstructor(typeof(object)); Assert.AreEqual(typeof(object), tCast.GetType()); }
public void TestConstructDateTimeNoParams() { var tCast = Impromptu.InvokeConstructor(typeof(DateTime)); Assert.AreEqual(default(DateTime), tCast); }
public void TestConstructprimativetype() { var tCast = Impromptu.InvokeConstructor(typeof(Int32)); Assert.AreEqual(default(Int32), tCast); }
public void TestConstructValueType() { var tCast = Impromptu.InvokeConstructor(typeof(DateTime), 2009, 1, 20); Assert.AreEqual(20, tCast.Day); }
public void TestConstructGuid() { var tCast = Impromptu.InvokeConstructor(typeof(Guid)); Assert.AreEqual(default(Guid), tCast); }
/// <summary> /// Constructs the type. Override for changing type intialization property changes. /// </summary> /// <param name="type">The type.</param> /// <param name="args">The args.</param> /// <returns></returns> protected virtual object CreateType(Type type, params object[] args) { return(Impromptu.InvokeConstructor(type, args)); }