コード例 #1
0
        private static object ToLw(object modelIn, Type lwType)
        {
            object outputBase = Activator.CreateInstance(lwType);

            outputBase = DataConverter.ConvertObject(modelIn, lwType);

            if (outputBase is LWAttributeSetContainer lwObj)
            {
                IEnumerable <PropertyInfo> attributeSets = modelIn.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(LWAttributeSet)));
                foreach (var attributeProperty in attributeSets)
                {
                    LWAttributeSet attr = attributeProperty.GetCustomAttribute <LWAttributeSet>();

                    var modelObj = attributeProperty.GetValue(modelIn);
                    if (modelObj != null)
                    {
                        if (IsPropertyACollection(attributeProperty))
                        {
                            var modelList = modelObj as IEnumerable;
                            foreach (var val in modelList)
                            {
                                var lwAttrObj = ToLw(val, attr._Type);
                                lwObj.Add(lwAttrObj as LWAttributeSetContainer);
                            }
                        }
                        else
                        {
                            var lwAttrObj = ToLw(modelObj, attr._Type);
                            lwObj.Add(lwAttrObj as LWAttributeSetContainer);
                        }
                    }
                }
            }
            return(outputBase);
        }
コード例 #2
0
        public static T FromLW <T>(object obj)
        {
            T outputBase = (T)Activator.CreateInstance(typeof(T));

            //Get all of our output data types properties which are marked as coming from an LWAttributeSet
            IEnumerable <PropertyInfo> attributeSets = outputBase.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(LWAttributeSet)));

            //Convert the base object over to our output type.
            outputBase = DataConverter.ConvertTo <T>(obj);

            if (obj is LWAttributeSetContainer lwObj)
            {
                //Get attribute sets from the input object and convert them over to our output type.
                foreach (var attributeProperty in attributeSets)
                {
                    LWAttributeSet attr = attributeProperty.GetCustomAttribute <LWAttributeSet>();

                    var lwChildren = lwObj.GetAttributeSets(attr._Type.Name);
                    if (lwChildren?.Count > 0)
                    {
                        if (IsPropertyACollection(attributeProperty))
                        {
                            var listObj = Activator.CreateInstance(attributeProperty.PropertyType);

                            foreach (var lwChild in lwChildren)
                            {
                                var setObj = FromLw(lwChild, attributeProperty.PropertyType.GetGenericArguments()[0]);
                                listObj.GetType().GetMethod("Add").Invoke(listObj, new[] { setObj });
                            }
                            attributeProperty.SetValue(outputBase, listObj);
                        }
                        else
                        {
                            var lwChild = lwChildren.FirstOrDefault();
                            var setObj  = FromLw(lwChild, attributeProperty.PropertyType);
                            attributeProperty.SetValue(outputBase, setObj);
                        }
                    }
                }
            }

            return(outputBase);
        }