public static string[] TryGetParamsByNameFromSubKey(string inKeyName, IKey inKey, ILogPrinter inLogger, bool inSubKeyMustBe, params int[] inParamCounts) { IKey sub_key = inKey.FindChildByName(inKeyName, StringComparison.InvariantCultureIgnoreCase); if (sub_key == null) { if (inSubKeyMustBe) { inLogger.LogError(string.Format("Can't find subkey by name. Key [{0}] must have subkey {1}!", inKey.GetPath(), inKeyName)); } return(null); } int value_count = sub_key.GetValuesCount(); if (!inParamCounts.ContainsCheck(value_count)) { string counts = inParamCounts.ToString(", "); inLogger.LogError(string.Format("Subkey [{0}] must have [{1}] counts of values, but was found {2}", sub_key.GetPath(), counts, value_count)); return(null); } var a = new string[value_count]; for (int i = 0; i < value_count; i++) { a[i] = sub_key.GetValueAsString(i); } return(a); }
public object ReadKey(IKey key, ILogPrinter inLogger) { int count = key.GetValuesCount(); var arr = new NamedId[count]; for (int i = 0; i < count; i++) { arr[i] = NamedId.GetNamedId(key.GetValueAsString(i)); } return(arr); }
object DeserializeAtomic(object inInstance, IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger) { object instance; IKey key = inKey; if (key.GetValuesCount() == 0) { instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger); } else if (key.GetValuesCount() > 1) { var sb = new StringBuilder(); sb.Append("\""); for (int i = 0; i < key.GetValuesCount(); ++i) { if (i > 0) { sb.Append(","); } sb.Append(key.GetValueAsString(i)); } sb.Append("\""); LogError(inLogger, string.Format("Need one value for type {1}. Key: {0} [{3}]; Values: {2} ", key, type.Name, sb, key.GetPath())); instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger); } else { string key_value = key.GetValueAsString(0); if (!ReflectionHelper.StringToAtomicValue(key_value, type, out instance, _reflectionProvider, inLogger)) { LogError(inLogger, string.Format("Key {0} [{3}] with value {1} can't convert value to type {2}", key, key_value, type.Name, key.GetPath())); instance = ReflectionHelper.GetDefaultValue(type, _reflectionProvider, inLogger); } } return(instance); }
object DeserializeClass(object inInstance, IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger) { IKey type_key = inKey.GetChild("RealObjectType"); if (type_key != null) { string type_name = type_key.GetValueAsString(0); string assembly_name = type_key.GetValueAsString(1); try { Assembly assembly = Assembly.Load(assembly_name); Type obj_type = assembly.GetType(type_name, true); if (obj_type != null) { type = obj_type; } } catch (Exception ex) { LogError(inLogger, string.Format("Cant take type from RealObjectType {0}. Exception: {1}", type_name, ex.Message)); } } object instance = inInstance; if (instance == null) { instance = _reflectionProvider.Instantiate(type, inLogger); } if (instance != null) { //MethodInfo mi = type.GetMethod("DeserializationFromCscd", new Type[] { typeof(CascadeParser.IKey), typeof(CascadeParser.ILogPrinter) }); MethodInfo mi = type.GetMethod("DeserializationFromCscd", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (mi != null) { if (inKey != null && !inKey.IsEmpty) { IKey key = inKey; mi.Invoke(instance, new object[] { key, inLogger }); } } else { MemberInfo[] member_infos = _reflectionProvider.GetSerializableMembers(type); foreach (MemberInfo memberInfo in member_infos) { SCustomMemberParams member_params = GetMemberParams(memberInfo); Type memberType = memberInfo.GetMemberType(); IKey sub_key = inKey.GetChild(member_params.ChangedName); if (sub_key == null) { sub_key = inKey.GetChild(member_params.Name); } if (sub_key != null) { object readValue; if (member_params.Converter != null) { readValue = member_params.Converter.ReadKey(sub_key, inLogger); } else { readValue = DeserializeInternal(null, sub_key, memberType, 0, inStructDeep + 1, inLogger); } // This dirty check is naive and doesn't provide performance benefits //if (memberType.IsClass && readValue != currentValue && (readValue == null || !readValue.Equals(currentValue))) _reflectionProvider.SetValue(memberInfo, instance, readValue, inLogger); } else if (member_params.DefaultValue != null) { _reflectionProvider.SetValue(memberInfo, instance, member_params.DefaultValue, inLogger); } else if (memberType.IsClass || memberType.IsStruct()) { object already_exists_member = _reflectionProvider.GetValue(memberInfo, instance); if (already_exists_member != null) { //for set default values inside this object already_exists_member = DeserializeInternal(already_exists_member, IKeyFactory.CreateKey(string.Empty), memberType, 0, inStructDeep + 1, inLogger); if (already_exists_member != null) { _reflectionProvider.SetValue(memberInfo, instance, already_exists_member, inLogger); } } } } } mi = type.GetMethod("OnDeserializedMethod", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (mi != null) { var context = new StreamingContext(StreamingContextStates.Other); mi.Invoke(instance, new object[] { context }); } } return(instance); }
object DeserializeArray(IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger) { if (inKey.IsEmpty) { return(null); } IKey key = inKey; Array multi_dim_array = null; Type declaredItemType = type.GetElementType(); bool is_atomic_elems = declaredItemType.IsAtomic(); bool is_array_elems = declaredItemType.IsArray; int[] dims; if (is_array_elems) { dims = new int[] { key.GetChildCount() } } ; else { dims = FindArrayDimension(key, is_atomic_elems); } if (dims.Length == 0) { return(null); } object instance = Array.CreateInstance(declaredItemType, dims); multi_dim_array = instance as Array; CMultiArrayIndexer indexer = new CMultiArrayIndexer(multi_dim_array); while (indexer.MoveNext()) { IKey dim_child = GetKeyByArrayIndex(key, indexer.Current); if (dim_child == null) { LogError(inLogger, string.Format("Cant get value for multi array index {0}", indexer)); } else { object obj_value; int last_index = indexer.Current[indexer.Current.Length - 1]; if (is_atomic_elems) { string str_value; if (dim_child.GetValuesCount() == 0) { str_value = string.Empty; } else { str_value = dim_child.GetValueAsString(last_index); } if (!ReflectionHelper.StringToAtomicValue(str_value, declaredItemType, out obj_value, _reflectionProvider, inLogger)) { LogError(inLogger, string.Format("Key {0} [{3}] for collection with element type {1} can't convert value {2}", key, declaredItemType.Name, str_value, key.GetPath())); } } else { IKey child = dim_child.GetChild(last_index); obj_value = DeserializeInternal(null, child, declaredItemType, 0, inStructDeep + 1, inLogger); } multi_dim_array.SetValue(obj_value, indexer.Current); } } return(instance); }