public object Map(object source, Type mapTo) { if (source == null) { return(null); } var dest = GetCreator(mapTo)(); var sourceProps = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var destProps = dest.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var sourceProp in sourceProps) { var value = sourceProp.GetValue(source); var destProp = destProps.FirstOrDefault(p => p.Name == sourceProp.Name); if (destProp != null) { var sourceType = sourceProp.PropertyType; var destType = destProp.PropertyType; if (destType.IsAssignableFrom(sourceType)) { if (sourceType.IsArray) { value = ArrayCopy(value); } destProp.SetValue(dest, value); } } } return(dest); }
public static object Build(Type type, Action <object> map) { var entity = GetCreator(type)(); map?.Invoke(entity); return(entity); }
public static TModel Build <TModel>(Action <TModel> map) { var entity = (TModel)GetCreator(typeof(TModel))(); map?.Invoke(entity); return(entity); }
public MetaTriggerData(string name, List <MetaActionData> allowedActions, System.Type triggerType, GetCreator creator, AddAction addAction, GetActions getActions) { this.name = name; this.triggerType = triggerType; this.allowedActions = allowedActions; this.creator = creator; this.addAction = addAction; this.getActions = getActions; }
public IAutoConfig CreateAutoConfig(IEnumerable <GenericConfig> genericConfigs) { if (genericConfigs == null) { throw new ArgumentNullException(nameof(genericConfigs)); } IAutoConfig result = GetCreator()(); foreach (var item in Properties) { var findGenericConfig = genericConfigs.FirstOrDefault(e => e.AppName == item.AppName && e.Key == item.PropertyAttribute.Key); if (findGenericConfig != null) { item.SetValueFromGenericConfig(result, findGenericConfig); } } return(result); }
public static object Build(Type type) { return(GetCreator(type)()); }
public static object CreateInstance(Type type, Type[] parameterTypes, params object[] args) { return(GetCreator(type, parameterTypes)(args)); }
public object ConvertToObject(JsValue jsValue, Type targetType) { if (jsValue == _getEngine().Global) { return(_global); } if (jsValue.IsUndefined()) { return(null); } if (jsValue.IsObject()) { var obj = jsValue.AsObject(); if (targetType == typeof(string)) { return(obj.ToString()); } switch (obj) { case ClrObject clr: return(clr.Target); case ObjectInstance objInst: return(GetCreator(targetType)(objInst)); } return(jsValue); } if (jsValue.IsBoolean()) { var boolVal = jsValue.AsBoolean(); if (targetType == typeof(string)) { return(boolVal ? "true" : "false"); } return(boolVal); } if (jsValue.IsString()) { return(jsValue.AsString()); } if (jsValue.IsNumber()) { var dbl = jsValue.AsNumber(); if (targetType == typeof(sbyte)) { return((sbyte)dbl); } if (targetType == typeof(byte)) { return((byte)dbl); } if (targetType == typeof(int)) { return((int)dbl); } if (targetType == typeof(uint)) { return((uint)dbl); } if (targetType == typeof(short)) { return((short)dbl); } if (targetType == typeof(ushort)) { return((ushort)dbl); } if (targetType == typeof(long)) { return((long)dbl); } if (targetType == typeof(ulong)) { return((ulong)dbl); } if (targetType == typeof(float)) { return((float)dbl); } if (targetType == typeof(string)) { return(dbl.ToString(CultureInfo.InvariantCulture)); } return(dbl); } return(null); }
public MetaActionData(string name, System.Type actionType, GetCreator creator) { this.name = name; this.actionType = actionType; this.creator = creator; }
/// <summary> Used to convert method's arguments </summary> public object ConvertToClr(object jsObject, Type targetType, object jsThis, bool expandArrayArgs) { if (jsObject == Engine.Global) { return(_globalFn()); } if (targetType == typeof(bool)) { return(ConvertBoolToClr(jsObject)); } if (jsObject == null) { return(targetType.IsPrimitive ? Activator.CreateInstance(targetType) : null); } if (targetType == typeof(double)) { return(Convert.ToDouble(jsObject)); } if (targetType == typeof(double?)) { return((double?)Convert.ToDouble(jsObject)); } if (targetType == typeof(float)) { return(Convert.ToSingle(jsObject)); } if (targetType == typeof(byte)) { return(Convert.ToByte(jsObject)); } if (targetType == typeof(sbyte)) { return(Convert.ToSByte(jsObject)); } if (targetType == typeof(int) || targetType == typeof(int?)) { return(GetIntConverter(0).Invoke(jsObject)); } if (targetType == typeof(uint)) { return(Convert.ToUInt32(jsObject)); } if (targetType == typeof(short)) { return(GetShortConverter(0).Invoke(jsObject)); } if (targetType == typeof(ushort)) { return(Convert.ToUInt16(jsObject)); } if (targetType == typeof(long)) { return(Convert.ToInt64(jsObject)); } if (targetType == typeof(ulong)) { return(Convert.ToUInt64(jsObject)); } if (targetType == typeof(string)) { return(jsObject.ToString()); } if (targetType == typeof(object) && jsObject is int || jsObject is string || jsObject is double) { return(jsObject); } if (_jsToClrMap.TryGetValue(jsObject, out var exist)) { return(exist); } if (jsObject is FunctionInstance func && typeof(Delegate).IsAssignableFrom(targetType)) { var jsThisInst = (ObjectInstance)(jsThis is global::Jurassic.Undefined ? Engine.Global : jsThis); //we have to pin 'this' to the handler. if (_jsToClrDelegatesMap.TryGetValue(new Tuple <ObjectInstance, ObjectInstance>(func, jsThisInst), out var del)) { return(del); } if (targetType == typeof(System.Action)) { var expressionBody = Expression.Call( Expression.Constant(func, typeof(FunctionInstance)), "Call", new Type[0], Expression.Constant(jsThis, typeof(object)), Expression.Constant(new object[0], typeof(object[]))); var lambda = Expression.Lambda <Action>(expressionBody); var compiled = lambda.Compile(); RegisterMap(compiled, func, jsThisInst); return(compiled); } else { var generic = targetType.IsGenericTypeDefinition ? targetType : targetType.IsGenericType ? targetType.GetGenericTypeDefinition() : null; if (generic != null) { if (targetType == typeof(Action <object[]>) && expandArrayArgs) { //special case Action <object[]> handler = args => { var jsArgs = args.Select(ConvertToJs).ToArray(); func.Call(jsThis, jsArgs); }; RegisterMap(handler, func, jsThisInst); return(handler); } else if (generic == typeof(System.Action <>) || generic == typeof(Func <,>)) { var genArgs = targetType.GetGenericArguments(); var parameterExpression = Expression.Parameter(genArgs[0], "arg1"); var argumentsArrayInit = Expression.NewArrayInit(typeof(object), Expression.Call( Expression.Constant(this), "ConvertToJs", new Type[0], Expression.Convert(parameterExpression, typeof(object)) ) ); var expressionBody = (Expression)Expression.Call( Expression.Constant(func, func.GetType()), "Call", new Type[0], Expression.Constant(jsThis, typeof(object)), argumentsArrayInit); if (generic == typeof(Func <,>)) { expressionBody = Expression.Convert(expressionBody, targetType.GetGenericArguments().Last()); } var lambda = Expression.Lambda(targetType, expressionBody, parameterExpression); var compiled = lambda.Compile(); RegisterMap(compiled, func, jsThisInst); return(compiled); } else if (generic == typeof(System.Action <,>)) //todo: two and more arguments callback { throw new NotImplementedException("Support of callback with multiple arguments not implemented"); } } } throw new NotImplementedException(); } if (jsObject is ArrayInstance jsArray && targetType == typeof(object[])) { return(jsArray.ElementValues.Select(ConvertToClr).ToArray()); } if (jsObject is ObjectInstance objectInstance && !(jsObject is ClrObjectInstance)) { return(GetCreator(targetType)(objectInstance)); } return(ConvertToClr(jsObject)); }
private static IConfigStore Create(Type type, IGeneratedStore parent) => GetCreator(type)(parent);
public static Cell GetCellFromChar(char c) { return(GetCreator(c)()); }
public static TModel Build <TModel>() { return((TModel)GetCreator(typeof(TModel))()); }