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 8430333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:34:36+00:00 2026-06-10T05:34:36+00:00

I want to pass in a strongly types property to a method and use

  • 0

I want to pass in a strongly types property to a method and use this propertyname as a string for mhy collection, so I found some code where I can pass my property strongly typed:

public static void Add<TObject, TProperty>(this NameValueCollection collection, Expression<Func<TObject, TProperty>> expression, string value)
{
    var member = expression.Body as MemberExpression;

    collection.Add(member.Member.Name, value);
}

This works and does what I want, but I was wondering how this exactly works. The part I am interested in is the generic arguments of the method (Add<Tobject, TProperty>) in combination with the Func expression. Can someone explain to me how this works? And why I can call this method like collection.Add((MyObject m) => m.FullName, "Martijn")? Why isn’t it necessarly to use Add<MyObject, ???>(m => m.FullName, "Martijn")?

Update:
I now have my method refactored to this:

public static void Add<TObject>(this NameValueCollection collection, Expression<Func<TObject, string>> expression, string value)
{
    var member = expression.Body as MemberExpression;

    collection.Add(member.Member.Name, value);
}
  • 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-06-10T05:34:37+00:00Added an answer on June 10, 2026 at 5:34 am

    Expression<T> is an expression tree that has the signature of delegate-type T. Expression trees are complex, but basically: instead of being a delegate that is the operation, this is an object-model that describes the operation, and that can be inspected to see how it is composed.

    Thus, an Expression<Func<TObject,TProperty>> is an expression-tree representing something that accepts TObject parameter and returns TProperty result.

    As for why you don’t need to tell it the <MyObject, ???> manually: that is generic type inference, and is normal. Given a generic method, say:

    void Foo<T>(T bar);
    

    You can call that as:

    Foo<string>("abc");
    

    but you can also use:

    Foo("abc");
    

    The compiler will then inspect the parameters to see if it can resolve all of the generic type parameters – in this case, the "abc" is a string and pins T to being a string. If it can resolve all of them, you don’t need to specify them.

    In your example, the TObject is pinned because your lambda explicitly takes a MyObject, via (MyObject) m, and the TProperty is pinned to (presumably) a string, because m.FullName (presumably) returns a string. Since all the generic type parameters have been resolved automatically you do not need to specify the <...> manually.

    Note that generic type inference only applies to generic methods (via the parameters), not to generic types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass an uninitialized object pointer to some method. Within that method
I want pass a class-instace to a method of an other and be sure
I want to pass '#' to the query string like ?page.aspx?someParam=1234#5 .
I want to pass a string that contains few words to jQuery when click
I want to pass some arguments to the my Javascript template in Rails3 application
Is it possible to pass in the strongly typed name of a method as
If i have not strongly typed Partial and i want to pass parameter to
I have created a strongly type view. I want to pass IEnumerable Object or
I want pass string to table view. In 1st, I can pass to table
I have a controller with that I want to pass off some data to

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.