Click here to Skip to main content
13,597,485 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.3K views
15 bookmarked
Posted 18 Aug 2015
Licenced CPOL

How to Get Name of the Member (Property Name, etc.) of a .NET Class Without Using Hard-coded/ Magic Strings

, 20 May 2018
Rate this:
Please Sign up or sign in to vote.
How to get name of the member (property name, etc.) of a .NET class without using hard-coded/ magic strings

Consider the following sample class:

public class City()
{
	public int CityId { get; set; }
	public string CityName { get; set; }
}

For various reasons, we need the name of the properties/ field of the class in a string variable in our code. (For example, while binding ID and Text fields in DropDown controls, etc.).

One option which is quite easy is to use the hard-coded strings directly.

For example:

string idField = "CityId";

But the biggest disadvantage with the above approach is that whenever a field name or property name is changed, you will need to manually search and edit such string values. If you forget, it will not give you any compile error and you will only come to know when some exception will be thrown from your code.

In order to get rid of such issues, we should avoid using hard-coded values.

.NET 3.5 or higher provides support of encapsulating methods using Func helper which we can use to find a name of the property/field.

using System;
using System.Linq.Expressions;
string idField = ((MemberExpression)((Expression<Func<City, int>>)(c => c.CityId)).Body).Member.Name;
string textField = ((MemberExpression)((Expression<Func<City, string>>)
			(c => c.CityName)).Body).Member.Name;

That means, now whenever some changes property "CityId" to "City_Id" or whatever, s/he must need to change the above code, otherwise it will always give a compile error.

Hope this helps!

Updates

  • 21/May/2018: As pointed out by many in the comments section - if you are using C# 6.0 or higher, then the above approach is not recommended. Instead, use nameof operator.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

nirman b doshi
Unknown
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
tbayart22-May-18 22:41
membertbayart22-May-18 22:41 
QuestionWhy not use Reflection? Pin
Graham Lemon (UK)22-May-18 2:14
professionalGraham Lemon (UK)22-May-18 2:14 
AnswerRe: Why not use Reflection? Pin
tbayart22-May-18 22:45
membertbayart22-May-18 22:45 
GeneralMy vote of 5 Pin
rbratton19-Aug-15 8:14
professionalrbratton19-Aug-15 8:14 
GeneralRe: My vote of 5 Pin
William E. Kempf19-Aug-15 11:27
memberWilliam E. Kempf19-Aug-15 11:27 
GeneralMy vote of 4 Pin
muralinarisetty18-Aug-15 23:15
membermuralinarisetty18-Aug-15 23:15 
SuggestionSimpler approach Pin
Mario Z18-Aug-15 20:45
professionalMario Z18-Aug-15 20:45 
GeneralMessage Closed Pin
21-May-18 7:25
memberDaniel da Silva s21-May-18 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180621.3 | Last Updated 21 May 2018
Article Copyright 2015 by nirman b doshi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid