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

  • Home
  • SEARCH
  • 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 155983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:15:06+00:00 2026-05-11T10:15:06+00:00

I am writing an extension method where I happen to need the field name

  • 0

I am writing an extension method where I happen to need the field name that the extension method is needed within the extension method. I can not figure out how to do this.

My code:


Extension method:

public static Validate Demand<T>(this T parameter) {     string name = ...     var field = GetField(parameter);     return Validation.CreateValidation(parameter, field, name); } 

Use case:

void SomeMethod(T someParameter) {    someParameter.Demand(); } 

I want name to hold someParameter.

  • 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. 2026-05-11T10:15:07+00:00Added an answer on May 11, 2026 at 10:15 am

    You can’t. It would be nice in a few cases, particularly for argument checking, but it’s just not possible. As an example of where it would be handy, I have this method:

    internal static void ThrowIfNull<T>(this T argument, string name)     where T : class {     if (argument == null)     {         throw new ArgumentNullException(name);     } } 

    which I have to call with:

    foo.ThrowIfNull('foo'); 

    It’s better than nothing, but it would be nice not to have to supply the ‘foo’ bit. Unfortunately, there’s just no way of doing it. You could probably write a post-build IL rewriter which determined how the method was called and rebuilt it from that, but that’s rather more work than the benefit, I suspect 🙂

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

Sidebar

Related Questions

I am writing a dirty little extension method for HtmlHelper so that I can
This may not be possible without writing some crazy extension method which I do
I'm writing a Firefox extension that's to be used in house. Basically, I need
I'm writing an extension method that takes a string of digits (ie. 4083239382) and
I'm having troubles in writing a static Deflate extension method, that i would use
I am writing an extension to NSURL that will give it a convenience method
Let's say I'm writing a jQuery extension method. This method should climb the ancestor
I need some help creating this extension method. My view inherits from <%@ Page
I am writing a test that depends on the results of an extension method
I am writing some unit tests for an extension method I have written on

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.