public object Provide(object context) { object o = GetCodeObject(); Type t = o.GetType(); MethodInfo mi = t.GetMethod(CodeMethodName); NameValueContext varContext = new NameValueContext(); foreach (VariableDescriptor varDescr in Variables) { object varValue = varDescr.VarProvider.Provide(context); if (varValue != null && !varDescr.VarType.IsInstanceOfType(varValue)) { ITypeConverter cnv = ConvertManager.FindConverter(varValue.GetType(), varDescr.VarType); if (cnv != null) { varValue = cnv.Convert(varValue, varDescr.VarType); } else { throw new InvalidCastException( String.Format("Context variable {0} cannot be cast to expected type {1}", varDescr.Name, varDescr.VarType)); } } varContext[varDescr.Name] = varValue; } object s = mi.Invoke(o, new object[] { varContext }); return(s); }
protected override object ConvertTo(object o, Type toType) { // optimization: do not use type conversion mechanizm for conversions between primitive types //if (o!=null && o.GetType().IsPrimitive && toType.IsPrimitive) { if (Converter != null && o != null && Converter.CanConvert(o.GetType(), toType)) { return(Converter.Convert(o, toType)); } if (o != null) { ITypeConverter cnv = ConvertManager.FindConverter(o.GetType(), toType); if (cnv != null) { return(cnv.Convert(o, toType)); } } else { if (!toType.IsValueType) { return(null); } } //} return(base.ConvertTo(o, toType)); }
public static bool AreEqual(object o1, object o2) { if (o1 == null && o2 == null) { return(true); } if (o1 != null) { var o1EqRes = o1.Equals(o2); // if Equals returns false try to convert o2 to o1 type if (!o1EqRes && o2 != null) { var o2Conv = ConvertManager.FindConverter(o2.GetType(), o1.GetType()); if (o2Conv != null) { return(o1.Equals(o2Conv.Convert(o2, o1.GetType()))); } } return(o1EqRes); } if (o2 != null) { return(o2.Equals(o1)); } return(false); }
public R Provide(C context) { object prvContext = context; // lets try to convert context to IDictionary b/c this is preferred context in NIC.NET if (prvContext != null) { var cnv = ConvertManager.FindConverter(prvContext.GetType(), typeof(IDictionary)); if (cnv != null) { prvContext = cnv.Convert(prvContext, typeof(IDictionary)); } } object res = UnderlyingProvider.GetObject(prvContext); if (!(res is R) && res != null) { return(ConvertManager.ChangeType <R>(res)); } else { return((R)((object)res)); } }
protected bool TryExecuteWithoutContext(object service, ActionContext context) { var opConv = ConvertManager.FindConverter(service.GetType(), typeof(Action)); if (opConv != null) { var opInstance = (Action)opConv.Convert(service, typeof(Action)); log.Write(LogEvent.Info, "Executing action (thread={0}, service={1})", Context.ThreadIndex, ServiceName); opInstance(); return(true); } else { return(false); } }
/// <summary> /// Composes URL from named route using given object's properties as context. /// </summary> public static string GetRouteUrl(string routeName, object context) { IDictionary <string, object> routeContext = null; if (context != null) { var conv = ConvertManager.FindConverter(context.GetType(), typeof(IDictionary <string, object>)); if (conv != null) { routeContext = conv.Convert(context, typeof(IDictionary <string, object>)) as IDictionary <string, object>; } else { routeContext = new ObjectDictionaryWrapper(context); } } return(GetRouteUrl(routeName, routeContext)); }
public void Execute(C context) { IDictionary contextDict = context as IDictionary; if (contextDict == null && context != null) { ITypeConverter conv = ConvertManager.FindConverter(context.GetType(), typeof(IDictionary)); if (conv != null) { contextDict = conv.Convert(context, typeof(IDictionary)) as IDictionary; } } if (contextDict == null) { contextDict = new Hashtable(); contextDict[DefaultContextKey] = context; } UnderlyingOperation.Execute(contextDict); }
protected void ExecuteService() { log.Write(LogEvent.Info, "Started (thread={0}, service={1}, iteration={2}/{3})", Context.ThreadIndex, ServiceName, Iteration + 1, IterationsCount); var service = ServicePrv.GetObject(ServiceName); if (service == null) { log.Write(LogEvent.Error, "Service does not exist (thread={0}, service={1})", Context.ThreadIndex, ServiceName, ServiceName); } else { // try get IOperation instance var opConv = ConvertManager.FindConverter(service.GetType(), typeof(IOperation <RunnerContext>)); if (opConv != null) { var opInstance = (IOperation <RunnerContext>)opConv.Convert(service, typeof(IOperation <RunnerContext>)); log.Write(LogEvent.Info, "Executing operation (thread={0}, service={1})", Context.ThreadIndex, ServiceName); opInstance.Execute(Context); } else { // try get IProvider instance var prvConv = ConvertManager.FindConverter(service.GetType(), typeof(IProvider <RunnerContext, object>)); if (prvConv != null) { var prvInstance = (IProvider <RunnerContext, object>)prvConv.Convert(service, typeof(IProvider <RunnerContext, object>)); log.Write(LogEvent.Info, "Executing provider (thread={0}, service={1})", Context.ThreadIndex, ServiceName); var res = prvInstance.Provide(Context); log.Write(LogEvent.Info, "Provider result: {2} (thread={0}, service={1})", Context.ThreadIndex, ServiceName, res ?? "NULL"); } else { // don't know how to execute log.Write(LogEvent.Warn, "Unkown service type: nothing to call (thread={0}, service={1})", Context.ThreadIndex, ServiceName); } } } log.Write(LogEvent.Info, "Finished (thread={0}, service={1}, iteration={2}/{3}, duration={4})", Context.ThreadIndex, ServiceName, Iteration + 1, IterationsCount, DateTime.Now.Subtract(StartTime.Value)); }
internal static bool AreEqual(object o1, object o2) { if ((o1 == null && o2 == null) || (DBNull.Value.Equals(o1) && DBNull.Value.Equals(o2))) { return(true); } if (o1 != null) { var o1EqRes = o1.Equals(o2); if (!o1EqRes && o2 != null) { var o2Conv = ConvertManager.FindConverter(o2.GetType(), o1.GetType()); if (o2Conv != null) { return(o1.Equals(o2Conv.Convert(o2, o1.GetType()))); } } return(o1EqRes); } return(o2 != null?o2.Equals(o1) : false); }
protected object[] PrepareActualValues(ParameterInfo[] paramsInfo, object[] values) { object[] res = new object[paramsInfo.Length]; for (int i = 0; i < paramsInfo.Length; i++) { if (values[i] == null || paramsInfo[i].ParameterType.IsInstanceOfType(values[i])) { res[i] = values[i]; continue; } var conv = ConvertManager.FindConverter(values[i].GetType(), paramsInfo[i].ParameterType); if (conv != null) { res[i] = conv.Convert(values[i], paramsInfo[i].ParameterType); continue; } throw new InvalidCastException( String.Format("Invoke method '{0}': cannot convert argument #{1} from {2} to {3}", MethodName, i, values[i].GetType(), paramsInfo[i].ParameterType)); } return(res); }
protected object[] PrepareActualValues(ParameterInfo[] paramsInfo, object[] values) { object[] res = new object[paramsInfo.Length]; for (int i = 0; i < paramsInfo.Length; i++) { if (values[i] == null || paramsInfo[i].ParameterType.IsInstanceOfType(values[i])) { res[i] = values[i]; continue; } var conv = ConvertManager.FindConverter(values[i].GetType(), paramsInfo[i].ParameterType); if (conv != null) { res[i] = conv.Convert(values[i], paramsInfo[i].ParameterType); continue; } // cannot cast? log.Write(LogEvent.Error, new { Action = "Converting args", Msg = "cannot cast", FromType = values[i].GetType(), ToType = paramsInfo[i].ParameterType }); throw new InvalidCastException(); } return(res); }
protected override object ConvertTo(object o, Type toType) { if (toType == null || toType == typeof(object)) { return(o); } if (o != null) { ITypeConverter cnv = ConvertManager.FindConverter(o.GetType(), toType); if (cnv != null) { return(cnv.Convert(o, toType)); } } else { if (!toType.IsValueType) { return(null); } } return(base.ConvertTo(o, toType)); }
public static decimal Sum(object o) { if (o is IEnumerable) { } else { o = new object[] { o }; } decimal sum = 0; foreach (var elem in (IEnumerable)o) { if (elem != null) { var conv = ConvertManager.FindConverter(elem.GetType(), typeof(decimal)); if (conv != null) { sum += (decimal)conv.Convert(elem, typeof(decimal)); } } } return(sum); }
public void TypeConverterStaticMethodsTest() { ITypeConverter cnv = ConvertManager.FindConverter(typeof(Hashtable), typeof(IDictionary <string, object>)); Assert.AreEqual(true, cnv != null); }
public int Compare(object a, object b) { if (a == null && b == null) { return(0); } if (a == null && b != null) { return(-1); } if (a != null && b == null) { return(1); } if ((a is IList) && (b is IList)) { IList aList = (IList)a; IList bList = (IList)b; if (aList.Count < bList.Count) { return(-1); } if (aList.Count > bList.Count) { return(+1); } for (int i = 0; i < aList.Count; i++) { int r = Compare(aList[i], bList[i]); if (r != 0) { return(r); } } // lists are equal return(0); } // test for quick compare if a type is assignable from b if (a is IComparable) { var aComp = (IComparable)a; // quick compare if types are fully compatible if (a.GetType().IsAssignableFrom(b.GetType())) { return(aComp.CompareTo(b)); } } if (b is IComparable) { var bComp = (IComparable)b; // quick compare if types are fully compatible if (b.GetType().IsAssignableFrom(a.GetType())) { return(-bComp.CompareTo(a)); } } // try to convert b to a and then compare if (a is IComparable) { var aComp = (IComparable)a; var conv = ConvertManager.FindConverter(b.GetType(), a.GetType()); if (conv != null) { var bConverted = conv.Convert(b, a.GetType()); return(aComp.CompareTo(bConverted)); } } // try to convert a to b and then compare if (b is IComparable) { var bComp = (IComparable)b; var conv = ConvertManager.FindConverter(a.GetType(), b.GetType()); // try to compare without any conversions if (conv != null) { var aConverted = conv.Convert(a, b.GetType()); return(-bComp.CompareTo(aConverted)); } } throw new InvalidCastException(String.Format("Cannot compare {0} and {1}", a.GetType(), b.GetType())); }