public static Type GetMemberType(this object target, string name, Type explicitType, ScriptValues.ValueType valueType) { if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor) { return(null); } object targetObject; bool isEnum; MemberInfo mi = FindMember(target, name, explicitType, out targetObject, out isEnum); ObjectLiteral ojl = targetObject as ObjectLiteral; FunctionLiteral ful = targetObject as FunctionLiteral; IDictionary <string, object> odi = targetObject as IDictionary <string, object>; IBasicKeyValueProvider iba = targetObject as IBasicKeyValueProvider; if (mi == null) { if (ojl != null) { return(ojl[name]?.GetType() ?? typeof(object)); } if (ful != null) { return(ful.GetInitialScopeValue(name)?.GetType() ?? typeof(object)); } if (odi != null && odi.ContainsKey(name)) { return(odi[name]?.GetType() ?? typeof(object)); } else if (odi != null) { return(null); } if (iba != null && iba.ContainsKey(name)) { return(iba[name]?.GetType() ?? typeof(object)); } else if (iba != null) { return(null); } } if (isEnum) { return((Type)targetObject); } if (mi == null) { throw new ScriptException(string.Format("Member {0} is not declared on {1}", name, targetObject)); } if (mi is PropertyInfo pi) { if (pi.CanRead) { return(pi.PropertyType); } return(null); } if (mi is FieldInfo fi) { return(fi.FieldType); } if (mi is EventInfo ev) { return(ev.EventHandlerType); } throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType)); }
public static object GetMemberValue(this object target, string name, Type explicitType, ScriptValues.ValueType valueType) { if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor) { var bv = target; var olt = bv as ObjectLiteral; if (valueType == ScriptValues.ValueType.Constructor && olt != null) { return(olt[name]); } return(bv); } object targetObject; bool isEnum; MemberInfo mi = FindMember(target, name, explicitType, out targetObject, out isEnum); ObjectLiteral ojl = targetObject as ObjectLiteral; FunctionLiteral ful = targetObject as FunctionLiteral; IDictionary <string, object> odi = targetObject as IDictionary <string, object>; IBasicKeyValueProvider iba = targetObject as IBasicKeyValueProvider; if (mi == null) { if (ojl != null) { return(ojl[name]); } if (ful != null) { return(ful.GetInitialScopeValue(name)); } if (odi != null && odi.ContainsKey(name)) { return(odi[name]); } else if (odi != null) { return(null); } if (iba != null && iba.ContainsKey(name)) { return(iba[name]); } else if (iba != null) { return(null); } } if ( isEnum) { return(Enum.Parse((Type)targetObject, name)); } if (mi == null) { throw new ScriptException(string.Format("Member {0} is not declared on {1}", name, targetObject)); } if (mi is PropertyInfo) { PropertyInfo pi = (PropertyInfo)mi; if (pi.CanRead) { return(pi.GetValue(targetObject, null)); } return(null); } if (mi is FieldInfo) { return(((FieldInfo)mi).GetValue(targetObject)); } if (mi is EventInfo) { return(null); } throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType)); }
/// <summary> /// Consumes the provided data and provides a value indicating whether the data could be processed and a callback to create a success-notification /// </summary> /// <param name="data">the data that was extracted from an ImportSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected override bool Consume(IBasicKeyValueProvider data, LogParserEventCallback <IBasicKeyValueProvider> logCallback, out Func <DynamicResult, KeyValueAcceptanceCallbackParameter> getNotification) { bool ok = false; List <string> acceptedKeys = new List <string>(); switch (columnMode) { case ColumnNameMode.Original: { foreach (string name in data.Keys) { SetValueOfColumn(name, data[name]); acceptedKeys.Add(name); } AddCurrentRecord(); ok = true; break; } case ColumnNameMode.FromFirstLine: { if (!CheckFirstLine(data)) { foreach (KeyValuePair <string, string> item in autoMap) { SetValueOfColumn(item.Value, data[item.Key]); acceptedKeys.Add(item.Key); } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); } ok = true; break; } case ColumnNameMode.FromConfig: { foreach (ColumnConfiguration column in config.Columns) { try { SetValueOfColumn(column.TableColumn, data[column.RawDataKey]); acceptedKeys.Add(column.RawDataKey); } catch (Exception ex) { logCallback(data, $@"Unable to process the provided Recordset: {ex.Message}", ParserEventSeverity.Error); } } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); ok = true; break; } } getNotification = d => new KeyValueAcceptanceCallbackParameter(ok, tableName, d, data, acceptedKeys.ToArray()); return(ok); }