Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get an object property name string without creating the object instance?

A string representation of an object instance property can be taken with Expression<Func<T>>:

string propertyName = ((MemberExpression) property.Body).Member.Name;

But what if I don't have (don't want to create) the instance? How do I get the property name in this case?

Explained

I need a string representation of a property name of some object.

Let's say there is an entity

public class Customer
{
    public int ID;
    public string Name;
}

Now I want to pass the key expression of this entity to some other function, thus I need the string "ID", but I don't want to hardcode the string like SomeOtherFunction("ID"), instead I use the expression SomeOtherFunction(ExpressionReader.GetString(() => CustomerInstance.ID)). For this to work I need to supply the entity instance.

Now I want to do the same without creating the instance.

like image 517
net_prog Avatar asked Nov 15 '11 12:11

net_prog


People also ask

How do you get the properties name of an object in typescript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .

How to access property of an object php?

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .

What is a JavaScript object property?

A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects.


2 Answers

This is possible with a method signature like the following:

private static string GetPropertyName<TModel, TProperty>(Expression<Func<TModel, TProperty>> property)
{
    MemberExpression memberExpression = (MemberExpression)property.Body;

    return memberExpression.Member.Name;
}

You can call this method without an instance of the customer class:

string propertyName = GetPropertyName((Customer c) => c.ID);

Of course you should add some checks for correct expression types before you cast to MemberExpression and access memberExpression.Member.Name.

like image 176
Peter Avatar answered Oct 05 '22 23:10

Peter


No, you dont need an instance to decompose the Expression. Even if the expression expects one, you will never invoke it.

In fact, you do it just like you did in first code snippet.

You would change you code to look like this then:

SomeOtherFunction(ExpressionReader<Customer>.GetString(c => c.ID))

Does that make sense to you?

Alternatively:

Customer c = null; // null intentionally

SomeOtherFunction(ExpressionReader.GetString(() => c.ID));

Still no problem as you are not invoking the expression.

like image 34
leppie Avatar answered Oct 06 '22 00:10

leppie