Esempio n. 1
0
        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;
                }
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        /// <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);
        }