Shorten this reflection property name helper method? - c#

I want a static helper method to remove magic strings. Of course, I could pack the property method inside the TestContainer, which would remove the need to provide the TestContainer as argument. This is done nicely here.
But I would like to have the helper method in one static class, in case I later decide to optimize my code and remove the method. I managed to get it down to this, but its a little bit ugly (having to provide the string type, looks not very nice).
Any "expression god" knowing a better way. Keep in mind, the static class should be kept universal and not know anything about the TestContainer (otherwise it would be as easy as the link provided)
internal class PropertyNameResolving
{
internal class TestContainer
{
public string LastName { get; set; }
}
internal static class BindingHelper
{
public static string PropertyName<TObject, TValue>(Expression<Func<TObject, TValue>> propertySelector)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null) return memberExpression.Member.Name;
else
throw new Exception("Something went wrong");
}
}
internal static void Test()
{
var t = new TestContainer {LastName = "Hans"};
Console.WriteLine(BindingHelper.PropertyName<TestContainer, string>(x => x.LastName));
Console.ReadLine();
}
}
Btw, the output is "LastName" and can be used to set bindings.
And one more question, can I remove the NULL check safely?

3 options for you:
make the class generic, and use inference on the method for the value
use an example object for inference on the method
forget about the object type entirely and just use an example
These are all shown below. Re the null safety - I would be tempted to check the expression-type (you can also handle methods, btw) - some more code shown here.
using System;
using System.Linq.Expressions;
internal class TestContainer
{
public string LastName { get; set; }
}
static class Program
{
static void Main()
{
var t = new TestContainer {LastName = "Hans"};
string s1 = BindingHelper<TestContainer>
.PropertyName(x => x.LastName);
string s2 = BindingHelper.PropertyName(t, x => x.LastName);
string s3 = BindingHelper.PropertyName(() => t.LastName);
}
}
internal static class BindingHelper
{
public static string PropertyName<TObject, TValue>(TObject template,
Expression<Func<TObject, TValue>> propertySelector)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null) return memberExpression.Member.Name;
else
throw new Exception("Something went wrong");
}
public static string PropertyName<TValue>(
Expression<Func<TValue>> propertySelector)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null) return memberExpression.Member.Name;
else
throw new Exception("Something went wrong");
}
}
internal static class BindingHelper<TObject>
{
public static string PropertyName<TValue>(
Expression<Func<TObject, TValue>> propertySelector)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null) return memberExpression.Member.Name;
else
throw new Exception("Something went wrong");
}
}

Because the compiler can imply the type arguments, you shouldn't need to provide at all in the Test() method without changing anything.
Use BindingHelper.PropertyName(x => x.LastName) and it should work fine as is.
You can't remove the null check safely as your expression could be anything, include method call, which wouldn't count as a MemberExpression.

Related

C# getting more than just last part of name from nameof [duplicate]

nameof(order.User.Age) return only Age instead of order.User.Age
What is the reason to do it in more restricted way?
If we want only last name we could do something like
public static GetLastName(this string x) {
return string.Split(x, '.').Last();
}
nameof(order.User.Age).GetLastName()
And with one operator we could get both, Age and order.User.Age. But with current implementation we can only get Age. Is there some logic behind this decision? For example, such behavior is necessary for MVC binding
Html.TextBox(nameof(order.User.Age))
Note that if you need/want the "full" name, you could do this:
$"{nameof(order)}.{nameof(User)}.{nameof(Age)}".GetLastName();
as long as all of these names are in the current scope.
Obviously in this case it's not really all that helpful (the names won't be in scope in the Razor call), but it might be if you needed, for example, the full namespace qualified name of a type for a call to Type.GetType() or something.
If the names are not in scope, you could still do the somewhat more clunky:
$"{nameof(order)}.{nameof(order.User)}.{nameof(order.User.Age)}".GetLastName();
-- although chances are at least one of those should be in scope (unless User.Age is a static property).
I had the same problem and implemented a class that acts as a replacement of the nameof() keyword in order to get the full name of the expression being supplied. It's greatly inspired from OK HOSTING answer. It's just all baked and ready to use:
public static class NameOf<TSource>
{
#region Public Methods
public static string Full(Expression<Func<TSource, object>> expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
{
var unaryExpression = expression.Body as UnaryExpression;
if (unaryExpression != null && unaryExpression.NodeType == ExpressionType.Convert)
memberExpression = unaryExpression.Operand as MemberExpression;
}
var result = memberExpression.ToString();
result = result.Substring(result.IndexOf('.') + 1);
return result;
}
public static string Full(string sourceFieldName, Expression<Func<TSource, object>> expression)
{
var result = Full(expression);
result = string.IsNullOrEmpty(sourceFieldName) ? result : sourceFieldName + "." + result;
return result;
}
#endregion
}
Using it in your code would look like:
class SpeciesFamily
{
public string Name { get; set; }
}
class Species
{
public SpeciesFamily Family { get; set; }
public string Name { get; set; }
}
class Cat
{
public Species Species { get; set; }
}
// Will return a string containing "Species.Family.Name".
var fullName = NameOf<Cat>.Full(c => c.Species.Family.Name);
// Will return a string containing "cat.Species.Name".
var fullNameWithPrefix = NameOf<Cat>.Full("cat", c => c.Species.Name);
Because it is exactly what for it've been invented. As you can read in already linked discussions, here you using the nameof operator as nameof(member-access), of the form E.I<A1…AK>, which will return:
These cases are all resolved using the rules for simple name lookup $7.6.2 or member access $7.6.4. If they succeed in binding, they must bind to one of:
A method-group. This produces an error "To specify the name of a method, you must provide its arguments".
A variable, value, parameter, constant, enumeration-member, property-access, field, event, type-parameter, namespace or type. In this case the result of the nameof operator is simply "I", which is generally the name of the symbol that the argument bound to. There are some caveats…
So in this case it, by its definition, have to evaluate all expressions before all the dots, step by step, and after that evaluate the last one to get its Name:
order.User.Age --> User.Age --> Age
Take a look at this method taken from:
https://github.com/okhosting/OKHOSTING.Data/blob/master/src/PCL/OKHOSTING.Data/Validation/MemberExpression.cs
public static string GetMemberString(System.Linq.Expressions.Expression<Func<T, object>> member)
{
if (member == null)
{
throw new ArgumentNullException("member");
}
var propertyRefExpr = member.Body;
var memberExpr = propertyRefExpr as System.Linq.Expressions.MemberExpression;
if (memberExpr == null)
{
var unaryExpr = propertyRefExpr as System.Linq.Expressions.UnaryExpression;
if (unaryExpr != null && unaryExpr.NodeType == System.Linq.Expressions.ExpressionType.Convert)
{
memberExpr = unaryExpr.Operand as System.Linq.Expressions.MemberExpression;
if(memberExpr != null)
{
return memberExpr.Member.Name;
}
}
}
else
{
//gets something line "m.Field1.Field2.Field3", from here we just remove the prefix "m."
string body = member.Body.ToString();
return body.Substring(body.IndexOf('.') + 1);
}
throw new ArgumentException("No property reference expression was found.", "member");
}
Some of the important purposes of using nameof is to get the last "name" in the expression.
For example nameof parameter when throwing ArgumentNullException:
void Method(string parameter)
{
if (parameter == null) throw new ArgumentNullException(nameof(parameter));
}
MVC Action links
<%= Html.ActionLink("Sign up",
#typeof(UserController),
#nameof(UserController.SignUp))
%>
INotifyPropertyChanged
int p {
get { return this._p; }
set { this._p = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.p)); }
}
More information: https://roslyn.codeplex.com/discussions/570551

Getting property names of a class using expressions, static methods, and a base object

I have a common problem, that I am trying to get round in a specific way.
Basically with Winforms, I am trying to set the "DisplayMember" and "ValueMember" of controls in a form. You would normally set it like so:
this.testCombobox.DisplayMember = "PropertyOne";
this.testCombobox.ValueMember = "PropertyTwo";
I want to rewrite this as follows:
this.testCombobox.DisplayMember = ClassOne.GetPropertyName(c => c.PropertyOne);
this.testCombobox.ValueMember = ClassOne.GetPropertyName(c => c.PropertyTwo);
(NOTE: the 2 method calls need to be static, to save creating objects here)
All of my classes that I am trying to do this, inherit from a base class "BaseObject", so I have added a method to it as follows:
public static string GetPropertyName<T, P>(Expression<Func<T, P>> action) where T : class
{
MemberExpression expression = action.Body as MemberExpression;
return expression.Member.Name;
}
However, in order to use this, I need to write the following code instead:
this.testCombobox.DisplayMember = BaseObject.GetPropertyName((ClassOne c) => c.PropertyOne);
My question is, how would I rewrite the method BaseObject.GetPropertyName to achieve what I want? I feel I am very close but cannot think how to change it.
Your current GetPropertyName method leaves open the generic T. Hence, the compiler cannot resolve it compile-time since you do not specify it when you use the method.
However, if you make your base class generic, AND specify the T in the derived class, AND specify to use the method of the derived class (and not the base class), then it works.
Like this:
public class BaseClass<T> {
public static string GetPropertyName<P>(Expression<Func<T, P>> action) {
MemberExpression expression = action.Body as MemberExpression;
return expression.Member.Name;
}
}
public class ClassOne : BaseClass<ClassOne> {
public string PropertyOne { get; set; }
}
public static class Test {
public static void test() {
var displayMember = ClassOne.GetPropertyName(x => x.PropertyOne);
}
}
I created a Helper class to extract Proprty Name
public static class PropertySupport
{
public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("Invalide Expression", "propertyExpression");
}
return memberExpression.Member.Name;
}
}
And you can use it as Follows :
PropertySupport.ExtractPropertyName(() => DateTime.Now)
it will return "Now"
EDIT
Here is a test console app:
public static class PropertySupport
{
public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("", "propertyExpression");
}
return memberExpression.Member.Name;
}
}
public class MyClass
{
public MyClass PropertyOne { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(PropertySupport.ExtractPropertyName(() => new MyClass().PropertyOne));
Console.ReadKey();
}
}
I would suggest the following in generic helper:
public static class GenericHelper<TEntity> {
public static string GetPropertyName<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) {
return propertyExpression.GetPropertyName();
}
}
and the following in Extensions for common usage:
public static class ReflectionExtensions {
public static PropertyInfo GetProperty<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> source) {
//TODO: check TEntity, if needed. Now it's ignored
var member = source.Body as MemberExpression;
if (member == null) {
throw new ArgumentException(String.Format(
"Expression '{0}' refers to a method, not a property.", source));
}
var propertyInfo = member.Member as PropertyInfo;
if (propertyInfo == null) {
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.", source));
}
return propertyInfo;
}
public static string GetPropertyName<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> source) {
return source.GetProperty().Name;
}
}

Getting the owning object of a property from a Property Expression

I'm working on a bit of code which has an end purpose of letting you use a property expression to set the value of a property with similar syntax to passing a variable as an out or ref parameter.
Something along the lines of:
public static foo(()=>Object.property, value);
And Object.Property will be assigned the value of value.
I'm using the following code to get the owining object of the property:
public static object GetOwningObject<T>(this Expression<Func<T>> #this)
{
var memberExpression = #this.Body as MemberExpression;
if (memberExpression != null)
{
var fieldExpression = memberExpression.Expression as MemberExpression;
if (fieldExpression != null)
{
var constExpression = fieldExpression.Expression as ConstantExpression;
var field = fieldExpression.Member as FieldInfo;
if (constExpression != null) if (field != null) return field.GetValue(constExpression.Value);
}
}
return null;
}
So this would, when used on a property expression like ()=>Object.Property, give back the instance of 'Object'. I'm somewhat new to using property expressions, and there seems to be many different ways to accomplish things, but I want to extend what I have so far, so that given an expression such as ()=>Foo.Bar.Baz it will give the Bar, not Foo. I always want the last containing object in the expression.
Any ideas? Thanks in advance.
What you have to do is to traverse through property chain to the most outer object.
The sample below is rather self explanatory and shows that the extension method would work for chained fields as well as properties:
class Foo
{
public Bar Bar { get; set; }
}
class Bar
{
public string Baz { get; set; }
}
class FooWithField
{
public BarWithField BarField;
}
class BarWithField
{
public string BazField;
}
public static class LambdaExtensions
{
public static object GetRootObject<T>(this Expression<Func<T>> expression)
{
var propertyAccessExpression = expression.Body as MemberExpression;
if (propertyAccessExpression == null)
return null;
//go up through property/field chain
while (propertyAccessExpression.Expression is MemberExpression)
propertyAccessExpression = (MemberExpression)propertyAccessExpression.Expression;
//the last expression suppose to be a constant expression referring to captured variable ...
var rootObjectConstantExpression = propertyAccessExpression.Expression as ConstantExpression;
if (rootObjectConstantExpression == null)
return null;
//... which is stored in a field of generated class that holds all captured variables.
var fieldInfo = propertyAccessExpression.Member as FieldInfo;
if (fieldInfo != null)
return fieldInfo.GetValue(rootObjectConstantExpression.Value);
return null;
}
}
[TestFixture]
public class Program
{
[Test]
public void Should_find_root_element_by_property_chain()
{
var foo = new Foo { Bar = new Bar { Baz = "text" } };
Expression<Func<string>> expression = () => foo.Bar.Baz;
Assert.That(expression.GetRootObject(), Is.SameAs(foo));
}
[Test]
public void Should_find_root_element_by_field_chain()
{
var foo = new FooWithField { BarField = new BarWithField { BazField = "text" } };
Expression<Func<string>> expression = () => foo.BarField.BazField;
Assert.That(expression.GetRootObject(), Is.SameAs(foo));
}
}
If your project is an MVC 5 project and you have a reference to the assembly System.Web.Mvc You could use the following:
Some time ago I wrote an extension method to easily create a multiselect dropdown (based on bootstrap 4) and it looked something like this:
public static MvcHtmlString MultiSelectFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
/*The challenge I faced here was that the expression you passed could very well be nested, so in order overcome this, I decompiled the dll to see how MVC does it, and I found this piece of code.*/
string expressionText = System.Web.Mvc.ExpressionHelper.GetExpressionText((LambdaExpression)expression);
System.Web.Mvc.ModelMetadata metadata = System.Web.Mvc.ModelMetadata.FromStringExpression(expressionText, htmlHelper.ViewData);
}
The metadata object has a Property called PropertyName and another property called Container which is a reference to the instance of the container object.

Another get property name in C# (this one is static)

I looked and everywhere are many examples how to do property name resolution, but I didn't find that would solve my usage.
My idea of User looks like this:
class Entity<T> where T : class
{
public static String GetName<T>(Expression<Func<T, object>> expr)
{
return ((MemberExpression)expr.Body).Member.Name;
}
}
class User : Entity<User>
{
public String UserName { get; set; }
public DateTime LastLoggedOn { get; set; }
}
Question: How to implement property name resolution if I want to use it like this?
Debug.Assert("UserName" == User.GetField(x => x.UserName));
Debug.Assert("LastLoggedOn" == User.GetField(x => x.LastLoggedOn));
Any help would be appreciated. Thanks.
Notes: I could do var u = new User(); and then u.GetName(() => u.UserName) but in my case, I don't have an instance of the entity
EDIT 1: Thanks to Darin, I updated my code. I need to get LastLoggedOn work too.
Debugging shows, that value of expr is {x => Convert(x.LastLoggedOn)} (don't know what the Convert means)
InvalidCastException was unhandled
Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'.
EDIT 2/Answer: After some debugging I've composed this "solution". I don't like it, but it seems to work.
public static string GetName(Expression<Func<T, object>> expression)
{
MemberExpression memberExp = expression.Body as MemberExpression;
if (memberExp != null)
return memberExp.Member.Name;
// for DateTime
UnaryExpression unaryExp = expression.Body as UnaryExpression;
if (unaryExp != null)
{
memberExp = unaryExp.Operand as MemberExpression;
if (memberExp != null)
return memberExp.Member.Name;
}
throw new ArgumentException("'expression' should be a member expression or a method call expression.", "expression");
}
Just remove the <T> from the GetName static method and you are good to go (the compiler should have warned you about this by the way):
public class Entity<T> where T : class
{
public static string GetName(Expression<Func<T, object>> expr)
{
return ((MemberExpression)expr.Body).Member.Name;
}
}
public class User: Entity<User>
{
public String UserName { get; set; }
}
Now you can write:
string name = User.GetName(x => x.UserName);
You can call your static method on Entity<T>:
string name = Entity<User>.GetName<User>(u => u.UserName);
You have made both the method and the class generic, both taking a type parameter. You probably only want the class to be generic, because it looks like that is how you are using it.

Get string property name from expression

I'm trying to write a strongly typed helper
which would be something like this:
Html.Lookup(x => x.FooId);
for now I have this:
public static MvcHtmlString Lookup<T,TReturn>(this HtmlHelper<T> html, Func<T, TReturn> expression)
{
// get string "FooId" here
}
Anybody knows how to get this ?
public static class ExpressionsExtractor
{
public static string Lookup<T, TProp>(this HtmlHelper<T> html, Expression<Func<T, TProp>> expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
}
You would then call it with:
var propName = Html.Lookup(x => x.FooId);
Yet another code.
public MvcHtmlString Lookup<T, TReturn>(this HtmlHelper<T> html, Expression<Func<T, TReturn>> expression)
{
return MvcHtmlString.Create(ExpressionHelper.GetExpressionText(expression));
}
Use ExpressionHelper class.
Func is delegate, Expression is generate ExpressionTree at compile time.
Expression.Compile() return delegate, but Func don't get ExpressionTree at runtime.
Currently using this class when I need this functionality outside of web project where System.Web.Mvc reference shouldn't exist:
namespace Interreg.Domain{
using System;
using System.Linq.Expressions;
public class PropertyName{
public static string For<T>(
Expression<Func<T,object>> expression){
var body=expression.Body;
return GetMemberName(body);
}
public static string For(
Expression<Func<object>> expression){
var body=expression.Body;
return GetMemberName(body);
}
public static string GetMemberName(
Expression expression){
if(expression is MemberExpression){
var memberExpression=(MemberExpression)expression;
if(memberExpression.Expression.NodeType==
ExpressionType.MemberAccess)
return GetMemberName(memberExpression.Expression)+"."+memberExpression.Member.Name;
return memberExpression.Member.Name;
}
if(expression is UnaryExpression){
var unaryExpression=(UnaryExpression)expression;
if(unaryExpression.NodeType!=ExpressionType.Convert)
throw new Exception(string.Format("Cannot interpret member from {0}",expression));
return GetMemberName(unaryExpression.Operand);
}
throw new Exception(string.Format("Could not determine member from {0}",expression));
}
}
}
Good thing about this one is - it does not lose dots when going deeper than just one level.
a bit late but I am posting a simple solution that's working for me in .Net 4. It has handling for value types on line 4
public PropertyInfo GetPropertyInfo<TSource>(Expression<Func<TSource, object>> propertyLambda) {
var member = propertyLambda.Body as MemberExpression;
if (member == null) {// value types return Convert(x.property) which can't be cast to MemberExpression
var expression = propertyLambda.Body as UnaryExpression;
member = expression.Operand as MemberExpression;
}
return member.Member as PropertyInfo;
}

Categories

Resources