public static T JsonTo <T>(this string s, T targetTypeDefault) { var jDic = new JsonDictionaryConverter(s); var com = new CommunicationBase(); foreach (var p in jDic.Dictionary.Keys) { /* * Get Deeper Level From string Result */ var val = jDic.Dictionary[p].StringValue; var newCom = new CommunicationBase() { Name = p, Value = val.ToCurrentType() }; var jDic2 = new JsonDictionaryConverter(jDic.Dictionary[p].StringValue); if (jDic2.Dictionary != null && jDic2.Dictionary.Count > 0) { newCom = val.JsonTo <CommunicationBase>(newCom); } com.Add(newCom); } return(com.To <T>(targetTypeDefault)); }
private static void ConvertListToICommunicationDictionary(object sourceValue, CommunicationBase result) { try { if (sourceValue == null || ((IDictionary)sourceValue).Count == 0) { return; } } catch { return; } var dic = ((IDictionary)sourceValue); foreach (var key in dic.Keys) { var iComBase = new CommunicationBase(); iComBase.Name = "Dic"; var i = (CommunicationBase)dic[key].To <ICommunication>(new CommunicationBase()); if (i.Count > 0) { iComBase.Value = key.ToString() + ":" + i; } else { iComBase.Value = key.ToString() + ":" + dic[key]; } iComBase.Property = null; result.Add(iComBase); } }
public IDictionary BuildDictionaryFromCom(CommunicationBase com, Type keyType, Type valType) { var DicInstance = (IDictionary)typeof(Dictionary <,>) .MakeGenericType(keyType, valType) .GetConstructor(Type.EmptyTypes) .Invoke(null); foreach (var item in com) { var parts = JsonBlockExtractor.FirstRestSplitter( item.Value.ToString(), new string[] { ":" }); var key = CreateInstance(keyType); var val = CreateInstance(valType); key = BuildFromCom(new CommunicationBase() { Value = parts[0] }, key); val = BuildFromCom(new CommunicationBase() { Value = parts[1] }, val); DicInstance.Add(key, val); } return(DicInstance); }
private static void ConvertListToICommunicationList(object sourceValue, CommunicationBase result) { try { if (sourceValue == null || ((IEnumerable)sourceValue).Cast <object>().ToList().Count == 0) { return; } } catch { return; } foreach (var listElement in ((IEnumerable)sourceValue).Cast <object>().ToList()) { var iComBase = (CommunicationBase)listElement.To <ICommunication>(new CommunicationBase()); iComBase.Name = "List"; if (iComBase.Count == 0) { iComBase.Value = listElement; } iComBase.Property = null; result.Add(iComBase); } }
private static void ConvertArrayToICommunicationList(object sourceValue, CommunicationBase result) { foreach (var arrayElement in (object[])sourceValue) { var iComBase = (CommunicationBase)arrayElement.To <ICommunication>(new CommunicationBase()); iComBase.Name = "Array"; if (iComBase.Count == 0) { iComBase.Value = arrayElement; } iComBase.Property = null; result.Add(iComBase); } }
public IList BuildListFromCom(CommunicationBase com, Type gType) { // Profiler.SetPoint(Adaptor.ProfilerKey, "s list " + gType.Name); var listInstance = (IList)typeof(List <>) .MakeGenericType(gType) .GetConstructor(Type.EmptyTypes) .Invoke(null); foreach (var item in com) { object ob = CreateInstance(gType); listInstance.Add(BuildFromCom((CommunicationBase)item, ob)); } // Profiler.SetPoint(Adaptor.ProfilerKey, "e list"); return(listInstance); }
public Out BuildFromCom <Out>(CommunicationBase com) { Out oldFilledProperty; var outType = (typeof(Out)); if (IsExclusiveType(outType)) { oldFilledProperty = (Out)(new Object()); } else { oldFilledProperty = (Out)outType.Assembly.CreateInstance( outType.Assembly.FullName); } return(BuildFromCom(com, oldFilledProperty)); }
private static void AssignToPatternRelatedProperty(CommunicationBase result, PropertyInfo flaggedProperty, object flaggedPropertyValue, CommunicationBase iComBase, AssignValueToAttribute flaggedPropertyCustomAttributes) { if (result.All(x => x.Name != flaggedPropertyCustomAttributes.Pattern)) { iComBase.Name = flaggedPropertyCustomAttributes.Pattern; if (iComBase.Count == 0) { iComBase.Value = flaggedPropertyValue; } iComBase.Property = flaggedProperty; result.Add(iComBase); } else { var firstOrDefault = result.FirstOrDefault(x => x.Name == flaggedPropertyCustomAttributes.Pattern); if (firstOrDefault != null) { firstOrDefault.Value += CommunicationBase.PatternJoint + flaggedPropertyValue; } } }
public Out BuildFromCom <Out>(CommunicationBase com, Out oldFilledProperty, Type targetType = null) { //Profiler.SetPoint(Adaptor.ProfilerKey, "start Build"); if (com == null || (com.Count == 0 && com.Value == null)) { return(oldFilledProperty); } var outType = (oldFilledProperty != null ? oldFilledProperty.GetType() : typeof(Out)); if (oldFilledProperty == null) { oldFilledProperty = (Out)CreateInstance(outType); } if (IsExclusiveType(outType) && com.Count == 0) { return((Out)BuildExclusiveFromCom(com, oldFilledProperty)); // Boxing and Unboxing } //else if (outType.IsGenericType && outType.Name.Contains("List")) //{ // // Under constructions : .class:List<> //} //else if (outType.IsGenericType && outType.Name.Contains("Dictionary")) //{ // // Under constructions : .class:Dictionary<,> //} else if (outType.IsEnum) { return((Out)BuildEnumFromCom((CommunicationBase)com, oldFilledProperty)); } else if (outType.IsClass) { var properties = GetAdaptorProperties(outType, targetType); //Profiler.SetPoint(Adaptor.ProfilerKey, "Get Property"); if (properties.Count > 0) { foreach (var property in properties) { #region Fetch Data To Instance var subIx = com.FirstOrDefault(x => x.Name == property.Name); if (subIx != null) { object ob = CreateInstance(property.PropertyType); if (subIx.Value != null && isExclusive(ob)) { var obj = BuildExclusiveFromCom((CommunicationBase)subIx, ob); property.SetValue(oldFilledProperty, obj, null); } else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("List")) { var gtype = property.PropertyType.GetGenericArguments()[0]; var obj = BuildListFromCom((CommunicationBase)subIx, gtype); property.SetValue(oldFilledProperty, obj, null); } else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("Dictionary")) { var keyType = property.PropertyType.GetGenericArguments()[0]; var valType = property.PropertyType.GetGenericArguments()[1]; var obj = BuildDictionaryFromCom((CommunicationBase)subIx, keyType, valType); property.SetValue(oldFilledProperty, obj, null); } else if (property.PropertyType.IsEnum) { var obj = BuildEnumFromCom((CommunicationBase)subIx, ob); property.SetValue(oldFilledProperty, obj, null); } else if (property.PropertyType.IsClass) // try like Class { var obj = BuildFromCom((CommunicationBase)subIx, ob); property.SetValue(oldFilledProperty, obj, null); } else { // Wrong Type Requested For Build Action } //Profiler.SetPoint(Adaptor.ProfilerKey, "Fetch " + property.Name); } #endregion } } } return(oldFilledProperty); }
public object BuildEnumFromCom <GType>(CommunicationBase com, GType defaultValue) { var gType = (defaultValue != null ? defaultValue.GetType() : typeof(GType)); return(Enum.Parse(gType, com.Value.ToString())); }
public object BuildExclusiveFromCom <OutType>(CommunicationBase com, OutType defaultValue) { if (com.Value == null) { return(null); } var outType = (defaultValue != null ? defaultValue.GetType() : typeof(OutType)); var inType = com.Value.GetType(); if (outType.FullName.Contains("System.String")) { return((OutType)com.Value); } if (inType.FullName.Contains("System.String")) { object ob; Int64 int64; Decimal dsm; DateTime date; Guid guid; if (IsFlag(outType)) { if (com.Value.ToString() == "true" || com.Value.ToString() == "True") { ob = true; } else { ob = false; } } else if (IsInteger(outType)) { if (Int64.TryParse(com.Value.ToString(), out int64)) { ob = int64; } else { int64 = 0; } if (outType == typeof(byte)) { return((byte)int64); } else if (outType == typeof(short)) { return((short)int64); } else if (outType == typeof(int)) { return((int)int64); } else if (outType == typeof(long)) { return((long)int64); } else if (outType == typeof(Int16)) { return((Int16)int64); } else if (outType == typeof(Int32)) { return((Int32)int64); } else { return((Int64)int64); } } else if (IsFloatNumber(outType)) { if (Decimal.TryParse(com.Value.ToString(), out dsm)) { ob = dsm; } else { dsm = 0; } if (outType == typeof(float)) { return((float)dsm); } else if (outType == typeof(double)) { return((double)dsm); } else if (outType == typeof(Decimal)) { return((Decimal)dsm); } else if (outType == typeof(Single)) { return((Single)dsm); } else if (outType == typeof(Double)) { return((Double)dsm); } else { return((decimal)dsm); } } else if (outType == typeof(DateTime)) { if (DateTime.TryParse(com.Value.ToString(), out date)) { ob = date; } else { ob = defaultValue; } } else if (outType == typeof(Guid)) { if (Guid.TryParse(com.Value.ToString(), out guid)) { ob = guid; } else { ob = defaultValue; } } else { ob = defaultValue; } return((OutType)ob); } //if ((IsInteger(outType) && IsInteger(inType)) || (IsFloatNumber(outType) && IsFloatNumber(inType)) || // (IsInteger(outType) && IsFloatNumber(inType)) || (IsFloatNumber(outType) && IsInteger(inType))) return((OutType)com.Value); }
/// <summary> /// populates properties of an object given as parameter /// </summary> /// <param name="ix"></param> /// <param name="respectiveObject"></param> /// <returns></returns> public static object PopulateRespectiveObjectValue(CommunicationBase ix, object respectiveObject) { if (respectiveObject == null) { return(null); } var respectiveObjectType = respectiveObject.GetType(); foreach (var property in GetCommunicationFlaggedProperties(respectiveObjectType, ConversionMode.Build)) { var buildOptions = property.GetCustomAttributes(typeof(BuildValueFromAttribute), true) .Select(x => (BuildValueFromAttribute)x).FirstOrDefault(); if (buildOptions != null && buildOptions.Pattern != property.Name) { if (buildOptions.Properties.Count == 1) { var firstOrDefault = ix.FirstOrDefault(x => x.Name == buildOptions.Pattern); if (firstOrDefault != null) { property.SetValue(respectiveObject, firstOrDefault.Value, null); } } else { var value = ""; foreach (var bProperties in buildOptions.Properties) { var firstOrDefault = ix.FirstOrDefault(x => x.Name == buildOptions.Pattern); if (firstOrDefault != null) { value += CommunicationBase.PatternJoint + firstOrDefault.Value.ToString(); } } if (value != "") { property.SetValue(respectiveObject, value.Substring(CommunicationBase.PatternJoint.Length), null); } } } else { var subIx = ix.FirstOrDefault(x => x.Name == property.Name); if (subIx != null && property.CanWrite) { if (subIx.Value != null && IsOfExclusiveTypes(property.PropertyType)) { try { property.SetValue(respectiveObject, subIx.Value.ToCurrentType(), null); } catch { if (property.PropertyType == typeof(bool)) { property.SetValue(respectiveObject, (subIx.Value.ToString().ToLower() != "true" ? false : true), null); } else if (property.PropertyType == typeof(int)) { property.SetValue(respectiveObject, (subIx.Value.ToString().ToLower() != "" ? subIx.Value.To <int>(0) : 0), null); } else { property.SetValue(respectiveObject, subIx.Value.ToString(), null); } } } else if (subIx.Value != null && property.PropertyType == typeof(System.Object) && !subIx.Value.ToString().StartsWith("{")) { property.SetValue(respectiveObject, subIx.Value.ToCurrentType(), null); } else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("List")) { var gtype = property.PropertyType.GetGenericArguments()[0]; var listInstance = (IList)typeof(List <>) .MakeGenericType(gtype) .GetConstructor(Type.EmptyTypes) .Invoke(null); foreach (var item in (CommunicationBase)subIx) { try { if (gtype.IsEnum) { var it = Enum.Parse(gtype, item.Value.ToString()); listInstance.Add(it); } else if (gtype.IsClass && !gtype.FullName.StartsWith("System")) { var it = item.Value.ToString().JsonTo(gtype.Assembly.CreateInstance(gtype.FullName)); listInstance.Add(it); } else { listInstance.Add(item.Value); } } catch { } } property.SetValue(respectiveObject, listInstance, null); } else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("Dictionary")) { var keyType = property.PropertyType.GetGenericArguments()[0]; var valType = property.PropertyType.GetGenericArguments()[1]; var DicInstance = (IDictionary)typeof(Dictionary <,>) .MakeGenericType(keyType, valType) .GetConstructor(Type.EmptyTypes) .Invoke(null); foreach (var item in (CommunicationBase)subIx) { try { var parts = JsonBlockExtractor.FirstRestSplitter( item.Value.ToString(), new string[] { ":" }); dynamic key = ""; dynamic val = ""; if (keyType.IsEnum) { key = Enum.Parse(keyType, parts[0]); } else if (keyType.IsClass && !keyType.FullName.StartsWith("System")) { key = parts[0].JsonTo( keyType.Assembly.CreateInstance(keyType.FullName)); } else { key = parts[0]; } if (valType.IsEnum) { val = Enum.Parse(valType, parts[1]); } else if (valType.IsClass && !valType.FullName.StartsWith("System")) { val = parts[1].JsonTo( valType.Assembly.CreateInstance(valType.FullName)); } else { val = parts[1]; } DicInstance.Add(key, val); } catch { } } property.SetValue(respectiveObject, DicInstance, null); } else if (property.PropertyType.IsArray) { } else if (property.PropertyType.IsEnum) { if (((CommunicationBase)subIx).PropertyType == CommunicationBase.PropertyTypes.Enum) { try { var val = Enum.Parse(property.PropertyType, ((CommunicationBase)subIx)[0].Value.ToString() .Replace("'", "") .Replace("\"", "")); property.SetValue(respectiveObject, val, null); } catch { var val = Enum.Parse(property.PropertyType, ((CommunicationBase)subIx).Value.ToString() .Replace("'", "") .Replace("\"", "")); property.SetValue(respectiveObject, val, null); } } else { var val = Enum.Parse(property.PropertyType, subIx.Value.ToString() .Replace("'", "") .Replace("\"", "")); property.SetValue(respectiveObject, val, null); } } else { if (((CommunicationBase)subIx).PropertyType == CommunicationBase.PropertyTypes.Class) { property.SetValue( respectiveObject, property.PropertyType.Assembly.CreateInstance(property.PropertyType.FullName) , null); } PopulateRespectiveObjectValue( (CommunicationBase)ix.Where(x => x.Name == property.Name) .FirstOrDefault(), property.GetValue(respectiveObject, null)); } } } } return(respectiveObject); }
private static void ConvertFlaggedPropertyToICommunication(object sourceValue, CommunicationBase result, PropertyInfo flaggedProperty) { var flaggedPropertyValue = flaggedProperty.GetValue(sourceValue, null); var iComBase = (CommunicationBase)flaggedPropertyValue.To <ICommunication>(new CommunicationBase()); var flaggedPropertyCustomAttributes = flaggedProperty.GetCustomAttributes(typeof(AssignValueToAttribute), true).Select(x => (AssignValueToAttribute)x).FirstOrDefault(); if (flaggedPropertyCustomAttributes != null && flaggedPropertyCustomAttributes.Pattern != flaggedProperty.Name) { AssignToPatternRelatedProperty(result, flaggedProperty, flaggedPropertyValue, iComBase, flaggedPropertyCustomAttributes); } else { iComBase.Name = flaggedProperty.Name; if (iComBase.Count == 0) { iComBase.Value = flaggedPropertyValue; } iComBase.Property = flaggedProperty; result.Add(iComBase); } }
/// <summary> /// converts to an impmelemtantion of BaseCommunication /// </summary> public ICommunication CommunicationValue(Type targetType = null) { var sourceValue = NormalizeSource(); // extracts source type Type sourceType = sourceValue.GetType(); var result = new CommunicationBase(); if (IsOfExclusiveTypes(sourceType)) { result.PropertyType = CommunicationBase.PropertyTypes.Object; return(result); } else if (sourceType == typeof(ICommunication) || sourceType == typeof(CommunicationBase)) { result.PropertyType = CommunicationBase.PropertyTypes.Communication; return(result); } if (sourceType.IsEnum) { result.PropertyType = CommunicationBase.PropertyTypes.Enum; return(result); } if (sourceType.IsArray) { result.PropertyType = CommunicationBase.PropertyTypes.Array; ConvertArrayToICommunicationList(sourceValue, result); return(result); } if (sourceType.IsGenericType) { if (sourceType.Name.Contains("List")) { result.PropertyType = CommunicationBase.PropertyTypes.List; ConvertListToICommunicationList(sourceValue, result); return(result); } if (sourceType.Name.Contains("Dictionary")) { result.PropertyType = CommunicationBase.PropertyTypes.Dictionary; ConvertListToICommunicationDictionary(sourceValue, result); return(result); } else if (!sourceType.Name.Contains("AnonymousType")) { result.PropertyType = CommunicationBase.PropertyTypes.Anonymous; return(result); } } foreach (var flaggedProperty in GetCommunicationFlaggedProperties(sourceType, ConversionMode.Assign, targetType)) { if (sourceType.ToString() != "System.RuntimeType") { ConvertFlaggedPropertyToICommunication(sourceValue, result, flaggedProperty); } } result.PropertyType = CommunicationBase.PropertyTypes.Class; return(result); }