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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:08:57+00:00 2026-06-11T04:08:57+00:00

I am calling a method from a property. I have to pass property name

  • 0

I am calling a method from a property. I have to pass property name as attribute of that method. My Property is

string lcl_name = string.Empty;

public string Name 
{
get { return lcl_name; }
set 
    {
        if (lcl_name != value) 
        {
            lcl_name = value;
            Foo_Method(GetCorrectPropertyName(MethodBase.GetCurrentMethod().Name));
        }
}
}

and the method is

public string GetCorrectPropertyName(string propertyName)
{
    return propertyName.StartsWith("set_") || propertyName.StartsWith("get_") ? 
           propertyName.Substring(4) : string.Empty;
}

My seniors say that i should not call Reflection and pass direct string to method this way

Foo_Method("Name");

but in that case since it would be hardcoded and if property name is changed, then method call have to be changed accordingly.

So my question is which one of them would be better in terms of efficiency? Is there something else that my seniors are seeing to which I am oblivious to?

  • 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-11T04:08:59+00:00Added an answer on June 11, 2026 at 4:08 am

    Maybe Expression Trees can help you here.

    Instead of Foo_Method taking the property name as string

    void Foo_Method(string name)
    {
    
    }
    

    use a parameter of type Expression to retrieve the property name via a MemberExpression:

    void Foo_Method(Expression<Func<NameOfYourClass, String>> exp)
    {
        var propertyName = ((MemberExpression)exp.Body).Member.Name;
    }
    

    and call it like this

    public string Name 
    {
        get { return lcl_name; }
        set 
        {
            if (lcl_name != value) 
            {
                lcl_name = value;
                // Foo_Method("Foo"); string is bad
                Foo_Method(x => x.Name);
            }
        }   
    }
    

    This way, when renaming Name, you don’t break your code, since Foo_Method(x => x.Name) gets renamed too (when using your IDEs refactoring capabilities for renaming, of course).


    Edit:

    To answer your comment:

    If you really can’t add an overload to Foo_Method, you can of course just create another method:

    if (lcl_name != value) 
    {
        lcl_name = value;
        Foo_Method(GetPropName(x => x.Name));
    }
    
    ...
    
    string GetPropName(Expression<Func<NameOfYourClass, String>> exp)
    {
        return ((MemberExpression)exp.Body).Member.Name;
    }
    

    Edit2:

    To answer your other comment:

    You could create an extension method

    public static class Extensions
    {
        public static string GetPropName<T>(this T t, Expression<Func<T, String>> exp)
        {
            return ((MemberExpression)exp.Body).Member.Name;
        }
    }
    
    var propertyName = yourInstace.GetPropName(y => y.Name);
    

    but you don’t have to, since the expressions work fine without any instance.

    public static class Extensions
    {
        public static string GetPropName<T>(Expression<Func<T, String>> exp)
        {
            return ((MemberExpression)exp.Body).Member.Name;
        }
    }
    
    var propertyName  = Extensions.GetPropName<YourClass>(y => y.Name);
    

    the trick is to use generics here.


    The first method would look like this in VB.Net

    Public Function GetPropName(Of TClass, TProperty)(exp As Expression(Of Func(Of TClass, TProperty))) as String
        Return DirectCast(exp.Body, MemberExpression).Member.Name
    End Function
    
    ...
    
    GetPropName(Function(x) x.Name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with calling an overriden method from java class. I have
from flex, when calling a .net web method that returns a custom class, I
I have a method that will return a list of suggested orders. If the
I have an IPhone app that uses webservices to get data from a server.
I have looked at few examples here Calling a Method from an Expression and
I have a custom tag that I'm trying to pass a String to. It
I have an extension on IQueryable that allows passing in delimited string of property
I am calling a method from another method and keep getting a warning: firstViewController
I'm having an issue calling a method from a separate project within the same
I'm completely stuck with calling a method from a UIView subclass, the method just

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.