How do you get a C# property name as a string with reflection?
Solution 1
You can use Expressions to achieve this quite easily. See this blog for a sample.
This makes it so you can create an expression via a lambda, and pull out the name. For example, implementing INotifyPropertyChanged can be reworked to do something like:
public int MyProperty {
get { return myProperty; }
set
{
myProperty = value;
RaisePropertyChanged( () => MyProperty );
}
}
In order to map your equivalent, using the referenced "Reflect" class, you'd do something like:
string propertyName = Reflect.GetProperty(() => SomeProperty).Name;
Viola - property names without magic strings.
Solution 2
This approach will be way faster than using Expression
public static string GetName<T>(this T item) where T : class
{
if (item == null)
return string.Empty;
return typeof(T).GetProperties()[0].Name;
}
Now you can call it:
new { MyClass.SomeProperty }.GetName();
You can cache values if you need even more performance. See this duplicate question How do you get a variable's name as it was physically typed in its declaration?
Solution 3
You can get a list of properties of an object using reflection.
MyClass o;
PropertyInfo[] properties = o.GetType().GetProperties(
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
);
Each Property has a Name attribute which would get you "SomeProperty"
Jim Mitchener
Updated on June 14, 2022Comments
-
Jim Mitchener almost 2 years
Possible Duplicate:
c# - How do you get a variable’s name as it was physically typed in its declaration?I'm looking for a way to get a property name as a string so I can have a "strongly-typed" magic string. What I need to do is something like MyClass.SomeProperty.GetName() that would return "SomeProperty". Is this possible in C#?
-
Jim Mitchener over 14 yearsThe syntax here is a bit nasty, but it does do what I needed it to. Thanks!
-
Paul almost 10 yearsBest answer here. I don't want to loop properties, I want to get a property name based on an actual property. Using the anonymous type with the first (only) property being the one you want the name from is genius.
-
nawfal almost 10 years@Paul I'm not the genius behind it. Secondly, it's not very refactor friendly. I mean if you change the name of the property, the anonymous type preserves the original name. In this case, if you change
SomeProperty
toSomeProperty1
, the anonymous class changes tonew { SomeProperty = SomeProperty1 }
, which will print "SomeProperty", instead of the updated name "SomeProperty1". I will mention it in the answer. -
Paul almost 10 yearsDarn. I don't anticipate ever changing a property name, but if I ever did, I would want the refactoring to handle it. Thanks for letting me know.
-
Rakuen42 about 9 yearsThis will also fail if MyClass is null at the time of execution. Expression approaches will succeed. In the vast majority of cases that won't be an issue, because you'd be getting the null reference exception somewhere else, but it's probably worth pointing out.
-
nawfal almost 9 years@Pedro77 you have to write the extension method in a static class first.