Exemple #1
0
        public static PropertyInfo GetPropertyAccess(this LambdaExpression propertyAccessExpression)
        {
            Debug.Assert(propertyAccessExpression.Parameters.Count == 1);

            var parameterExpression = propertyAccessExpression.Parameters.Single();
            var propertyInfo        = parameterExpression.MatchSimplePropertyAccess(propertyAccessExpression.Body);

            if (propertyInfo == null)
            {
                throw new ArgumentException(ResX.InvalidPropertyExpression(propertyAccessExpression), nameof(propertyAccessExpression));
            }

            var declaringType = propertyInfo.DeclaringType;
            var parameterType = parameterExpression.Type;

            if (declaringType != null &&
                declaringType != parameterType &&
                declaringType.GetTypeInfo().IsInterface &&
                declaringType.GetTypeInfo().IsAssignableFrom(parameterType.GetTypeInfo()))
            {
                var propertyGetter   = propertyInfo.GetMethod;
                var interfaceMapping = parameterType.GetTypeInfo().GetRuntimeInterfaceMap(declaringType);
                var index            = Array.FindIndex(interfaceMapping.InterfaceMethods, p => p == propertyGetter);
                var targetMethod     = interfaceMapping.TargetMethods[index];
                foreach (var runtimeProperty in parameterType.GetRuntimeProperties())
                {
                    if (targetMethod == runtimeProperty.GetMethod)
                    {
                        return(runtimeProperty);
                    }
                }
            }

            return(propertyInfo);
        }
Exemple #2
0
        public static IReadOnlyList <PropertyInfo> GetPropertyAccessList(this LambdaExpression propertyAccessExpression)
        {
            Debug.Assert(propertyAccessExpression.Parameters.Count == 1);

            var propertyPaths = MatchPropertyAccessList(propertyAccessExpression, (p, e) => e.MatchSimplePropertyAccess(p));

            if (propertyPaths == null)
            {
                throw new ArgumentException(ResX.InvalidPropertiesExpression(propertyAccessExpression), nameof(propertyAccessExpression));
            }

            return(propertyPaths);
        }
Exemple #3
0
        public static IReadOnlyList <PropertyInfo> GetComplexPropertyAccess(this LambdaExpression propertyAccessExpression)
        {
            Debug.Assert(propertyAccessExpression.Parameters.Count == 1);

            var propertyPath
                = propertyAccessExpression
                  .Parameters
                  .Single()
                  .MatchPropertyAccess(propertyAccessExpression.Body);

            if (propertyPath == null)
            {
                throw new ArgumentException(ResX.InvalidComplexPropertyExpression(propertyAccessExpression));
            }

            return(propertyPath);
        }