Right, say I have something like:
myClass.myClassIntegerVariable=23
Is there an easy way of getting not the value of myClassIntegerVariable, but it's name as a String? Otherwise I'd have to create redundant data like:
myClass.myClassIntegerVariable=23;
myClass.myClassIntegerVariableName="myClassIntegerVariableName";
Get name of class property?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Dipal ChoksiRank #199 · 12K reputationPosted Oct 29, 2011, 10:13 PM EDT
http://www.c-sharpcorner.com/UploadFile/kirtan007/787/
http://www.csharp-examples.net/reflection-property-names/
Jaish MathewsRank #225 · 11K reputationPosted Oct 29, 2011, 10:42 PM EDT
You didn't mention scenarios like
- Do you need to access it in side same class or in side a separate class?
- Want to know whether your class is static?
Below is the code if it's same class where specified field need to access. Please refactor it.Assembly assembly = Assembly.GetExecutingAssembly();
MemberInfo[] memberInfo = assembly.GetType("Web.foo._Default").GetMembers();
IEnumerator infoCollection = memberInfo.GetEnumerator();
while (infoCollection.MoveNext())
{
MemberInfo info = (MemberInfo)infoCollection.Current;
if (info.MemberType.Equals(MemberTypes.Field))
{
//Here you will get all fileds in side the loaded type and use .Name property for the name
}
}
yuriy thebest7 reputationPosted Oct 30, 2011, 2:31 AM EDT