コード例 #1
0
        public void GetRuntimeProperties()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeProperties(null);
            });

            List <string> properties = new List <string>();

            foreach (TypeInfo type in types)
            {
                if (!type.Namespace.Equals("PropertyDefinitions", StringComparison.Ordinal))
                {
                    continue;
                }

                properties.Clear();
                properties.AddRange((IEnumerable <string>)type.GetDeclaredField("DeclaredPropertyNames").GetValue(null));
                properties.AddRange((IEnumerable <string>)type.GetDeclaredField("InheritedPropertyNames").GetValue(null));

                Assert.All(type.AsType().GetRuntimeProperties(), p => Assert.True(properties.Remove(p.Name)));
                Assert.Empty(properties);
            }
        }
コード例 #2
0
        public static IEnumerable <PropertyInfo> GetRuntimeProperties(this Type type)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeProperties(type));
#else
            return(type.GetProperties());
#endif
        }
コード例 #3
0
        public static PropertyInfo GetAnyProperty(this Type type, string name)
        {
            List <PropertyInfo> list = (from p in RuntimeReflectionExtensions.GetRuntimeProperties(type)
                                        where p.Name == name
                                        select p).ToList();

            if (list.Count > 1)
            {
                throw new AmbiguousMatchException();
            }
            return(list.SingleOrDefault());
        }
コード例 #4
0
ファイル: JsonMapper.cs プロジェクト: dqchess/ColaFrameWork
        public static PropertyInfo[] GetPublicInstanceProperties(Type type)
        {
#if NETFX_CORE
            List <PropertyInfo> list = new List <PropertyInfo>();
            foreach (PropertyInfo propertyInfo in RuntimeReflectionExtensions.GetRuntimeProperties(type))
            {
                if (propertyInfo.GetMethod.IsPublic && !propertyInfo.GetMethod.IsStatic)
                {
                    list.Add(propertyInfo);
                }
            }
            return(list.ToArray());
#else
            return(type.GetProperties());
#endif
        }
コード例 #5
0
        public MvxDBMapping(Type type)
        {
            Type      = type;
            TableName = type.Name;
            var attributes = type.GetCustomAttributes(typeof(MvxDBTableAttribute), true).Cast <MvxDBTableAttribute>().ToList();

            if (attributes.Any())
            {
                TableName = attributes[0].Name;
            }

            var query = RuntimeReflectionExtensions.GetRuntimeProperties(type);

            PropertyInfoKey = query.FirstOrDefault(p => p.GetCustomAttribute <MvxDBKeyAttribute>(true) != null);
            query           = query.Where(p => p.CanRead && p.CanWrite && p.GetCustomAttribute <MvxDBIgnoreAttribute>(true) == null && p.GetCustomAttribute <MvxDBKeyAttribute>(true) == null);
            PropertiesInfos = query.ToList();
        }
コード例 #6
0
 public static IEnumerable <MemberInfo> GetMembersInHierarchy(this Type type)
 {
     do
     {
         foreach (PropertyInfo item in from pi in RuntimeReflectionExtensions.GetRuntimeProperties(type)
                  where !(pi.GetMethod ?? pi.SetMethod).IsStatic
                  select pi)
         {
             yield return(item);
         }
         foreach (FieldInfo item2 in from f in RuntimeReflectionExtensions.GetRuntimeFields(type)
                  where !f.IsStatic
                  select f)
         {
             yield return(item2);
         }
         type = type.BaseType;
     }while (type != null);
 }
コード例 #7
0
        public void GetRuntimeProperties()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeProperties(null);
            });

            List <String> properties = new List <String>();

            foreach (TypeInfo type in types)
            {
                if (!type.Namespace.Equals("PropertyDefinitions", StringComparison.Ordinal))
                {
                    continue;
                }

                properties.Clear();
                properties.AddRange((IEnumerable <String>)type.GetDeclaredField("DeclaredPropertyNames").GetValue(null));
                properties.AddRange((IEnumerable <String>)type.GetDeclaredField("InheritedPropertyNames").GetValue(null));

                foreach (PropertyInfo pi in type.AsType().GetRuntimeProperties())
                {
                    if (properties.Remove(pi.Name))
                    {
                        continue;
                    }

                    Assert.False(true, String.Format("Type: {0}, Property: {1} is not expected", type, pi));
                }

                foreach (String propertyName in properties)
                {
                    Assert.False(true, String.Format("Property: {0} cannot be found in {1}", propertyName, type));
                }
            }
        }
コード例 #8
0
ファイル: CloneObject.cs プロジェクト: Daoting/dt
 static PropertyInfo[] GetProperties(Type type)
 {
     return(Enumerable.ToArray <PropertyInfo>(RuntimeReflectionExtensions.GetRuntimeProperties(type)));
 }
コード例 #9
0
        /// <summary>
        /// Prepares the object as form fields using the provided name.
        /// </summary>
        /// <param name="name">root name for the variable</param>
        /// <param name="value">form field value</param>
        /// <param name="keys">Contains a flattend and form friendly values</param>
        /// <returns>Contains a flattend and form friendly values</returns>
        public static Dictionary <string, object> PrepareFormFieldsFromObject(String name, Object value,
                                                                              Dictionary <String, Object> keys = null)
        {
            keys = keys ?? new Dictionary <string, object>();

            if (value == null)
            {
                return(keys);
            }
            if (value is Stream)
            {
                keys[name] = value;
                return(keys);
            }
            if (value is IList)
            {
                int i          = 0;
                var enumerator = ((IEnumerable)value).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var subValue = enumerator.Current;
                    if (subValue == null)
                    {
                        continue;
                    }
                    var fullSubName = name + '[' + i + ']';
                    PrepareFormFieldsFromObject(fullSubName, subValue, keys);
                    i++;
                }
            }
            else if (value is Enum)
            {
                var isStringEnum = value.GetType().GetTypeInfo().GetCustomAttributes(false).FirstOrDefault(a => a is JsonConverterAttribute) != null;
                if (isStringEnum)
                {
                    value = JsonConvert.SerializeObject(value).Trim('"');
                }
                else
                {
                    value = (int)value;
                }
                keys[name] = value;
            }
            else if (value is IDictionary)
            {
                var obj = (IDictionary)value;
                foreach (var sName in obj.Keys)
                {
                    var    subName     = sName.ToString();
                    var    subValue    = obj[subName];
                    string fullSubName = string.IsNullOrWhiteSpace(name) ? subName : name + '[' + subName + ']';
                    PrepareFormFieldsFromObject(fullSubName, subValue, keys);
                }
            }
            else if (!(value.GetType().Namespace.StartsWith("System")))
            {
                //Custom object Iterate through its properties
                var          enumerator = RuntimeReflectionExtensions.GetRuntimeProperties(value.GetType()).GetEnumerator();
                PropertyInfo pInfo      = null;
                var          t          = new JsonPropertyAttribute().GetType();
                while (enumerator.MoveNext())
                {
                    pInfo = enumerator.Current as PropertyInfo;

                    var    jsonProperty = (JsonPropertyAttribute)pInfo.GetCustomAttributes(t, true).FirstOrDefault();
                    var    subName      = (jsonProperty != null) ? jsonProperty.PropertyName : pInfo.Name;
                    string fullSubName  = string.IsNullOrWhiteSpace(name) ? subName : name + '[' + subName + ']';
                    var    subValue     = pInfo.GetValue(value, null);
                    PrepareFormFieldsFromObject(fullSubName, subValue, keys);
                }
            }
            else
            {
                keys[name] = value;
            }
            return(keys);
        }