Ejemplo n.º 1
0
        /// <summary>
        /// Locate properties and fields that marked with attribute <see cref="AppendViewModelAttribute"/>
        /// </summary>
        /// <param name="obj">Object to scan</param>
        /// <returns>Returns the list of located objects.</returns>
        static public List <Tuple <Type, object> > LocateAppendedViewModels(object obj)
        {
            List <Tuple <Type, object> > res = new List <Tuple <Type, object> >();
            var lookUpObject = obj;
            var lookUpType   = obj.GetType();

#if WINDOWS_UWP
            IEnumerable <FieldInfo> info;
#else
            FieldInfo[] info;
#endif
#if WINDOWS_UWP
            info = lookUpType.GetTypeInfo().DeclaredFields;
#else
            info = lookUpType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
#endif
            foreach (var item in info)
            {
#if !WINDOWS_UWP
                var attribR = Attribute.GetCustomAttributes(item, typeof(AppendViewModelAttribute));
                var fType   = item.FieldType;
#else
                var attribR = item.GetCustomAttributes(typeof(AppendViewModelAttribute));
                var fType   = item.FieldType.GetTypeInfo();
#endif

                if (!fType.IsValueType && !BindHelper.CheckKnownTypePrefix(fType.FullName))
                {
                    foreach (Attribute att in attribR)
                    {
                        object value = item.GetValue(lookUpObject);
                        if (value != null)
                        {
                            res.Add(new Tuple <Type, object>(item.FieldType, value));
                        }
                    }
                }
            }

#if WINDOWS_UWP
            IEnumerable <PropertyInfo> propertyInfos;
            propertyInfos = lookUpType.GetTypeInfo().DeclaredProperties;
#else
            PropertyInfo[] propertyInfos;
            propertyInfos = lookUpType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
#endif
            foreach (var item in propertyInfos)
            {
#if !WINDOWS_UWP
                var attribR = Attribute.GetCustomAttributes(item, typeof(AppendViewModelAttribute));
                var fType   = item.PropertyType;
#else
                var attribR = item.GetCustomAttributes(typeof(AppendViewModelAttribute));
                var fType   = item.PropertyType.GetTypeInfo();
#endif

                if (!fType.IsValueType && !BindHelper.CheckKnownTypePrefix(fType.FullName))
                {
                    foreach (Attribute att in attribR)
                    {
                        object value = item.GetValue(lookUpObject, null);
                        if (value != null)
                        {
                            res.Add(new Tuple <Type, object>(item.PropertyType, value));
                        }
                    }
                }
            }

            return(res);
        }