So I use the following utility to get the name of a field/property from an instance of a class...
public static string FieldName<T>(Expression<Func<T>> Source)
{
return ((MemberExpression)Source.Body).Member.Name;
}
This allows me to do the following:
public class CoolCat
{
public string KaratePower;
}
public class Program
{
public static Main()
{
public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower);
}
}
This is great for serialization and other times when I need a string representation of the field name.
But now, I want to be able to get the field name WITHOUT having an instance of the class - for instance, if I am setting up a table and want to dynamically link the FieldNames of the columns to actual fields in a class (so refactorings, etc. will not break it).
Basically, I feel like I just don't quite get the syntax of how to accomplish this, but I imagine that it will look something like this:
public static string ClassFieldName<T>(Func<T> PropertyFunction)
{
// Do something to get the field name? I'm not sure whether 'Func' is the right thing here - but I would imagine that it is something where I could pass in a lambda type expression or something of the sort?
}
public class Program
{
public static Main()
{
string CatsPowerFieldName = ClassFieldName<CoolCat>((x) => x.KaratePower);
// This 'CatsPowerFieldName' would be set to "KaratePower".
}
}
I hope that makes sense - I'm not very good with the vocab around this subject so I know that the question is a little vague.
What you're trying to do is one of the reasons Microsoft created System.Reflection Try this:
using System.Reflection; // reflection namespace
public static List<Type> GetClassPropertyNames(Type myClass)
{
PropertyInfo[] propertyInfos;
propertyInfos = myClass.GetProperties(BindingFlags.Public);
List<Type> propertyTypeNames = new List<Type>();
// write property names
foreach (PropertyInfo propertyInfo in propertyInfos)
{
propertyTypeNames .Add(propertyInfo.PropertyType);
}
return propertyNames;
}
I have two methods I use to do this.
The first is an extension method that can be used on any object.
public static string GetPropertyName<TEntity, TProperty>(this TEntity entity, Expression<Func<TEntity, TProperty>> propertyExpression)
{
return propertyExpression.PropertyName();
}
Which is used like
public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = Jimmy.GetPropertyName(j => j.KaratePower);
The second I use when I don't have an object.
public static string PropertyName<T>(this Expression<Func<T, object>> propertyExpression)
{
MemberExpression mbody = propertyExpression.Body as MemberExpression;
if (mbody == null)
{
//This will handle Nullable<T> properties.
UnaryExpression ubody = propertyExpression.Body as UnaryExpression;
if (ubody != null)
{
mbody = ubody.Operand as MemberExpression;
}
if (mbody == null)
{
throw new ArgumentException("Expression is not a MemberExpression", "propertyExpression");
}
}
return mbody.Member.Name;
}
This can be used like so
string KaratePowerField = Extensions.PropertyName<CoolCat>(j => j.KaratePower);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With