Example #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public static MethodInfo ResolveMethodFromLambda(LambdaExpression lambda)
        {
            var originalDeclaration        = ExtractMethodInfoFromLambda(lambda);
            var shouldResolveDeclaringType = TypeTemplate.IsTemplateType(originalDeclaration.DeclaringType);

            if (!originalDeclaration.IsGenericMethod && !shouldResolveDeclaringType)
            {
                return(originalDeclaration);
            }

            var resolvedDeclaration = originalDeclaration;

            if (shouldResolveDeclaringType)
            {
                var resolvedDeclaringType  = TypeTemplate.Resolve(originalDeclaration.DeclaringType);
                var resolvedReturnType     = TypeTemplate.Resolve(originalDeclaration.ReturnType);
                var resolvedParameterTypes = originalDeclaration.GetParameters().Select(p => TypeTemplate.Resolve(p.ParameterType)).ToArray();

                resolvedDeclaration = TypeMemberCache.Of(resolvedDeclaringType)
                                      .Methods.Where(m => m.Name == originalDeclaration.Name)
                                      .OfSignature(resolvedReturnType, resolvedParameterTypes)
                                      .Single();
            }

            if (resolvedDeclaration.IsGenericMethod)
            {
                var resolvedGenericArguments = originalDeclaration.GetGenericArguments().Select(TypeTemplate.Resolve).ToArray();
                resolvedDeclaration = resolvedDeclaration.GetGenericMethodDefinition().MakeGenericMethod(resolvedGenericArguments);
            }

            return(resolvedDeclaration);
        }
Example #2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public static FieldInfo ResolveFieldFromLambda(LambdaExpression lambda)
        {
            var originalFieldInfo = (FieldInfo)((MemberExpression)lambda.Body).Member;

            if (TypeTemplate.IsTemplateType(originalFieldInfo.DeclaringType))
            {
                var resolvedDeclaringType = TypeTemplate.Resolve(originalFieldInfo.DeclaringType);
                return(TypeMemberCache.Of(resolvedDeclaringType).Fields.Single(m => m.Name == originalFieldInfo.Name));
            }
            else
            {
                return(originalFieldInfo);
            }
        }
Example #3
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public static PropertyInfo ResolvePropertyFromLambda(LambdaExpression lambda)
        {
            var propertyInfo = (PropertyInfo)((MemberExpression)lambda.Body).Member;

            if (TypeTemplate.IsTemplateType(propertyInfo.DeclaringType))
            {
                var resolvedDeclaringType  = TypeTemplate.Resolve(propertyInfo.DeclaringType);
                var resolvedPropertyType   = TypeTemplate.Resolve(propertyInfo.PropertyType);
                var resolvedParameterTypes = propertyInfo.GetIndexParameters().Select(p => TypeTemplate.Resolve(p.ParameterType)).ToArray();
                var resolvedPropertyInfo   = TypeMemberCache.Of(resolvedDeclaringType)
                                             .Properties.Where(p => p.Name == propertyInfo.Name && p.DeclaringType == resolvedDeclaringType)
                                             .OfSignature(resolvedPropertyType, resolvedParameterTypes)
                                             .Single();

                return(resolvedPropertyInfo);
            }
            else
            {
                return(propertyInfo);
            }
        }