Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 211435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:05:43+00:00 2026-05-11T18:05:43+00:00

Update: This does work, I was being stupid :( i have the following extension

  • 0

Update: This does work, I was being stupid 🙁

i have the following extension method

public static string ExtMethod(this object self, object myparameter);

at runtime this is called in any number of ways ways, i think these are all possibilities:

Expression<Func<T, string>> expr = x => x.property.ExtMethod(5);
Expression<Func<T, string>> expr = x => x.property.ExtMethod(new object());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.OtherProperty);

what i need to do is evaluate the “myparameter“, given “expr” and a “T“

because of the two cases where x is used in myparameter, i thought i needed to create a delegate of the form:

Expression<Func<T, object>> expr = x => [myparameter expression here]

i thought this would work:

var extMethodExpr = expr.Body as MethodCallExpression;
var myparameterExpr = extMethodExpr.Arguments[1];

var myparam = Expression.Lambda(myparameterExpr, expr.Parameters).Compile().Invoke(someT)

but for the expressions that do not involve x, i get TargetParameterCountException 🙁

in these cases, if i do:

var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT)

it works fine.

How do I solve this?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:05:43+00:00Added an answer on May 11, 2026 at 6:05 pm

    OK; got to the bottom of it; in the line:

    var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT);
    

    If you weren’t trying to pass in a someT, this would work for those expressions that don’t involve x in the argument; for those that do, you need to tell the lambda to include the parameter (the same one from the original lambda) – simply by:

    var myparam = Expression.Lambda(myparameterExpr,
                 outerLambda.Parameters[0]).Compile().Invoke(someT);
    

    Here’s some working code that evaluates the inner parameter (given an instance of the argument type); note that I use the parameter even if it doesn’t involve an x – otherwise, what would it do with the instance?

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    class Foo {
        public string Bar {get;set;}
        public int someMethod() { return 4; }
        public int OtherProperty { get { return 3; } }
    }
    static class Program
    {
        static int someMethod() { return 3; }
        static void Main()
        {
            Foo foo = new Foo();
            Test<Foo>(x => x.Bar.ExtMethod(5), foo);
            Test<Foo>(x => x.Bar.ExtMethod(new object()), foo);
            Test<Foo>(x => x.Bar.ExtMethod(someMethod()), foo);
            Test<Foo>(x => x.Bar.ExtMethod(x.someMethod()), foo);
            Test<Foo>(x => x.Bar.ExtMethod(x.OtherProperty), foo);
        }
        static void Test<T>(Expression<Func<T, string>> expr, T instance)
        {
            if (expr.Body.NodeType != ExpressionType.Call)
            {
                throw new InvalidOperationException("Call expected");
            }
            var call = ((MethodCallExpression)expr.Body);
            if (call.Method != typeof(Program).GetMethod(
                "ExtMethod", BindingFlags.Static | BindingFlags.NonPublic))
            {
                throw new InvalidOperationException("ExtMethod expected");
            }
            // we know that ExtMethod has 2 args; pick myParameter (the 2nd);
            // then build an expression over arg, re-using our outer param
            var newLambda = Expression.Lambda<Func<T, object>>(
                call.Arguments[1], expr.Parameters[0]);
    
            // evaluate it and show the argument value
            object value = newLambda.Compile()(instance);
            Console.WriteLine(value);
        }
        static string ExtMethod(this object self, object myParameter) {
            return self.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 134k
  • Answers 134k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Like Document Type Declarations? May 12, 2026 at 6:48 am
  • Editorial Team
    Editorial Team added an answer Digs through bookmarks http://blog.andrew.net.au/2009/01/01#usb_power_control Seems like you need to connect… May 12, 2026 at 6:48 am
  • Editorial Team
    Editorial Team added an answer this is my try. var a = 'param1=2&param2=1&param3=5'; var b… May 12, 2026 at 6:48 am

Related Questions

I'm faced with a situation that I think can only be solved by using
RESOLVED After much confusion and frustration, I finally got my hard disk to interrupt.
I don't know how else to say it so I'm just going to explain
I've tentatively written this method: public static Func<T> WeakCacheFor<T>( Func<T> provider ) where T:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.