コード例 #1
0
        private static IMPropertyInfo GetMPropertyInfo(Type pObjectType, string pProperty, IMPropertyInfo pParent)
        {
            if (pProperty.Contains('.'))
            {
                var properties = pProperty.Split('.');

                if (properties.Length < 2)
                {
                    throw new ArgumentException($"{nameof(pProperty)} not valid: " + pProperty);
                }

                var parentProperty = GetPropertyInfo(pObjectType, properties.First());

                string childProperties = string.Join(".", properties.Skip(1));

                var parent = new MPropertyInfo(parentProperty, pParent);

                var childPropType = parentProperty.PropertyType.GetProperty(properties.Skip(1).First());

                return(GetIMPropertyInfo(parentProperty.PropertyType, childProperties, childPropType.PropertyType, parent));
            }

            PropertyInfo pi = GetPropertyInfo(pObjectType, pProperty);

            if (pi == null)
            {
                throw new InvalidOperationException(pObjectType + " does not have property " + pProperty);
            }

            return(new MPropertyInfo(pi, pParent));
        }
コード例 #2
0
        public static IMPropertyInfo GetIMPropertyInfo(Type pObjectType, string pProperty, Type pPropertyType, IMPropertyInfo pParent = null)
        {
            if (typeof(IDictionary <string, object>).IsAssignableFrom(pObjectType))
            {
                return(new MPropertyExpandoInfo(pProperty, pPropertyType));
            }

            if (pProperty.Contains('.'))
            {
                var properties = pProperty.Split('.');

                if (properties.Length < 2)
                {
                    throw new ArgumentException($"{nameof(pProperty)} not valid: " + pProperty);
                }

                var parentProperty = pObjectType.GetProperty(properties.First());

                string childProperties = string.Join(".", properties.Skip(1));

                var parent = new MPropertyInfo(parentProperty, pParent);

                return(GetIMPropertyInfo(parentProperty.PropertyType, childProperties, null, parent)); //TODO pPropertyType
            }

            var pi = pObjectType.GetProperty(pProperty);

            if (pi == null && pObjectType.IsInterface)
            {
                pi = pObjectType.GetInterfaces().SelectMany(i => i.GetProperties()).SingleOrDefault(p => p.Name == pProperty);
            }

            if (pi == null)
            {
                throw new InvalidOperationException(pObjectType + " does not have property " + pProperty);
            }

            return(new MPropertyInfo(pi, pParent));
        }