"nameof" expression is introduced in Visual Studio 2015 and c# 6
nameof (C# and Visual Basic Reference)
How can u use it or write a similar method in older versions like .net framework 4.
If you're talking about an equivalent for C# before C#6, this will get the job done (in a hacky way) for properties. It can probably be expanded upon to include fields, methods, etc.
public static class TestExtension
{
public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor)
{
if (propertyAccessor.Body.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = propertyAccessor.Body as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
return null;
}
}
Just whipped this up quickly, so there's a lot to be improved, but you use it like this:
public class myClass
{
public string myProp { get; set; }
}
var a = new myClass();
var result = a.nameof(b => b.myProp);
Result contains 'myProp'
Update:
More comprehensive (though still not that pretty)
public static class TestExtension
{
public static String nameof<T, TT>(this Expression<Func<T, TT>> accessor)
{
return nameof(accessor.Body);
}
public static String nameof<T>(this Expression<Func<T>> accessor)
{
return nameof(accessor.Body);
}
public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor)
{
return nameof(propertyAccessor.Body);
}
private static String nameof(Expression expression)
{
if (expression.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = expression as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
return null;
}
}
Accessing static properties/fields:
TestExtension.nameof(() => myClass.MyOtherField)
Accessing parameters within functions:
void func (int a) {
TestExtension.nameof(() => a);
}
To my knowledge there are three options to not have to use a magic string
nameof which requires Visual Studio 2015 (But can be compiled to other versions of the .net framework)
nameof(this.Property)
use a method that takes an expression and returns the property name as found in this post "Get string name of property using reflection"
var propertyName = GetPropertyName(
() => myObject.AProperty); // returns "AProperty"
CallerMemberNameAttribute - (Only available in .net framework 4.5, included because original post said older versions like .net framework 4.0 which I guess includes 4.5) The draw back of this method is it is only useful when you need to string representation of the current method you are operating in.
public string IsChecked {
set{
Console.WriteLine(GetCurrentMemberName()); // prints "IsChecked"
}
}
string GetCurrentMemberName([CallerMemberName] string memberName = "")
{
return memberName;
}
nameOf - Gets resolved at Compiletime - if you decompile, you will see that the compiler just translated the classname (without the Namespace(!)) into a constant string instead.
So be aware!
If you want to get a classe's name use typeof() or GetType() to get the specific (maybe derived) type at Runtime and read the value of
the .Name-Property in .net < C#6.
Read more at MSDN
The nameof operator returns a string representation of the variable you have passed it so nameof(var1) will return "var1", its useful in avoiding code where we have to specificy variable names as strings like in argument exceptions.
In previous versions you could achieve a similar effect using reflection, or expression trees.
#Rob, thank you so much, but the static member access could be for the non-static class members too.
public static class TestExtension {
public static String nameof<T>(this Expression<Func<T, object>> accessor)
{
return nameof(accessor.Body);
}
}
class MyClass {
public Guid MyOtherField;
}
TestExtension.nameof<MyClass>(o=>o.MyOtherField);
and the good news is the Class won't be instantiated. It is useful to access domain layer DTOs.
Related
I am currently creating a custom way of deep-copying my objects. I use a static class for this functionality.
public static class CopyServer
{
public static int CopyDeep(int original)
{
return original;
}
//not shown: same for all other value types I use (long, float,...)
public static T CopyDeep<T>(T original) where T: ICopyAble
{
if (original == null)
return default;
if (original is ICopyAutofields)
return CopyAutofields(original);
return (T)original.CopyDeep();
}
private static T CopyAutofields<T>(T original)
{
Delegate del;
if (!_copyFunctions.TryGetValue(typeof(T), out del))
{
//not shown: Building expression for parameter etc.
foreach (var fieldInfo in typeof(T).GetFields())
{
//not shown: checking options set by custom attributes
MethodInfo methodInfo = typeof(CopyServer).GetMethod("CopyDeep", new[] { fieldInfo.FieldType });
//I can't remove the second param without getting an AmbiguousMatchException
if (methodInfo == null)
{
throw new Exception($"CopyDeep not defined for type {fieldInfo.FieldType}");
}
if (methodInfo.IsGenericMethod)
methodInfo = methodInfo.MakeGenericMethod(fieldInfo.FieldType);
Expression call = Expression.Call(methodInfo, readValue);
//not shown: Assign Expression
}
//not shown: return Expression and compiling
}
return ((Func<T, T>)del)(original);
}
}
I use T CopyAutofields<T> to build functions (by building and compiling Expression Trees) so I don't have to create copy-functions for each and every class I want to copy by hand. I control the copy-behaviour with Custom Attributes (I left this part in the code above since it is not relevant for my problem).
The code works fine as long as long as only fields with types for which a non-generic function exists are used. But it is not able to retrieve my generic function T CopyDeep<T>.
Example:
//This works:
public class Manager : ICopyAble,ICopyAutofields
{
public string FirstName;
public string LastName;
}
//This doesn't
//Meaning: typeof(CopyServer).GetMethod("copyDeep", new[] { fieldInfo.FieldType });
//in T copyAutofields<T> returns null for the Manager-field and my exception gets thrown
public class Employee : ICopyAble,ICopyAutofields
{
public string FirstName;
public string LastName;
public Manager Manager;
}
//This is what I was using before I started using the ICopyAutofields.
//This approach works, but its' too much too write since my classes usually
//have way more than three fields and I occasionally forget to update
//copyDeep()-function if I add new ones.
public class Employee : ICopyAble,ICopyAutofields
{
public string FirstName;
public string LastName;
public Manager Manager;
public IModable CopyDeep()
{
var result = new Employee();
result.FirstName = CopyServer.copyDeep(FirstName);
result.LastName= CopyServer.copyDeep(LastName);
result.Manager= CopyServer.copyDeep(Manager);
return result;
}
}
Long story short: I need a way of getting a matching function for a type T if both generic and non-generic functions with the right name exist.
In .NET 4.7.1 you need to use method GetMethods and filter the results:
class MyClass
{
public T M<T>(T t) { return default(T); }
public int M(int t) { return 0; }
}
var m = typeof(MyClass).GetMethod("M", new[] { typeof(string) }); // null
var m1 = typeof(MyClass).GetMethods()
.Where(mi => mi.Name == "M" && mi.GetGenericArguments().Any())
.First(); // returns generic method
In .NET Standard 2.1 (and .NET Core since 2.1) there is another way to resolve generic type arguments - Type.MakeGenericMethodParameter, like you can see it in this answer.
Also as workaround you can move your copyAutofields<T> method to generic class like CopyAutoFieldServer<T>:
public static class CopyAutoFieldServer<T>
{
public static T copyAutofields(T original) { ... }
}
I know there are a few answers on the site on this and i apologize if this is in any way duplicate, but all of the ones I found does not do what I am trying to do.
I am trying to specify method info so I can get the name in a type safe way by not using strings.
So I am trying to extract it with an expression.
Say I want to get the name of a method in this interface:
public interface IMyInteface
{
void DoSomething(string param1, string param2);
}
Currently I can get the name using THIS method:
MemberInfo GetMethodInfo<T>(Expression<Action<T>> expression)
{
return ((MethodCallExpression)expression.Body).Method;
}
I can call the helper method as follows:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething(null, null));
Console.WriteLine(methodInfo.Name);
But I am looking for the version that I can get the method name without specifying the parameters (null, null)
like this:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething);
But all attempts fail to compile
Is there a way to do this?
x => x.DoSomething
In order to make this compilable I see only two ways:
Go non-generic way and specify it's parameter as Action<string, string>
Specify Action<string, string> as your target delegate type by yourself: GetMethodInfo<IMyInteface>(x => new Action<string,string>(x.DoSomething))
if you are ok to go with second one, which allows you to omit arguments then you can write your GetMethodInfo method as follows:
MemberInfo GetMethodInfo<T>(Expression<Func<T, Delegate>> expression)
{
var unaryExpression = (UnaryExpression) expression.Body;
var methodCallExpression = (MethodCallExpression) unaryExpression.Operand;
var methodInfoExpression = (ConstantExpression) methodCallExpression.Arguments.Last();
var methodInfo = (MemberInfo) methodInfoExpression.Value;
return methodInfo;
}
It works for your interface, but probably some generalization will be required to make this working with any method, that's up to you.
The following is compatible with .NET 4.5:
public static string MethodName(LambdaExpression expression)
{
var unaryExpression = (UnaryExpression)expression.Body;
var methodCallExpression = (MethodCallExpression)unaryExpression.Operand;
var methodCallObject = (ConstantExpression)methodCallExpression.Object;
var methodInfo = (MethodInfo)methodCallObject.Value;
return methodInfo.Name;
}
You can use it with expressions like x => x.DoSomething, however it would require some wrapping into generic methods for different types of methods.
Here is a backwards-compatible version:
private static bool IsNET45 = Type.GetType("System.Reflection.ReflectionContext", false) != null;
public static string MethodName(LambdaExpression expression)
{
var unaryExpression = (UnaryExpression)expression.Body;
var methodCallExpression = (MethodCallExpression)unaryExpression.Operand;
if (IsNET45)
{
var methodCallObject = (ConstantExpression)methodCallExpression.Object;
var methodInfo = (MethodInfo)methodCallObject.Value;
return methodInfo.Name;
}
else
{
var methodInfoExpression = (ConstantExpression)methodCallExpression.Arguments.Last();
var methodInfo = (MemberInfo)methodInfoExpression.Value;
return methodInfo.Name;
}
}
Check this sample code on Ideone.
Note, that Ideone does not have .NET 4.5.
The problem with this is that x.DoSomething represents a method group. And you have to somehow explicitly specify what delegate type do you want to convert that method group into, so that the correct member of the group can be selected. And it doesn't matter if that group contains only one member.
The compiler could infer that you mean that one, but it doesn't do that. (I think it's this way so that your code won't break if you add another overload of that method.)
Snowbear's answer contains good advice on possible solutions.
This is a new answer to an old question, but responds to the "verbose" complaint of the accepted answer. It requires more code, but the result is a syntax like:
MemberInfo info = GetActionInfo<IMyInterface, string, string>(x => x.DoSomething);
or, for methods with a return value
MemberInfo info = GetFuncInfo<IMyInterface, object, string, string>(x => x.DoSomethingWithReturn);
where
object DoSomethingWithReturn(string param1, string param2);
Just like the framework provides Action<> and Func<> delegates up to 16 parameters, you have to have GetActionInfo and GetFuncInfo methods that accept up to 16 parameters (or more, although I'd think refactoring is wise if you have methods with 16 parameters). A lot more code, but an improvement in the syntax.
If you are ok with using the nameof() operator you can use the following approach.
One of the benefits is not having to unwrap an expression tree or supply default values or worry about having a non-null instance of the type with the method.
// As extension method
public static string GetMethodName<T>(this T instance, Func<T, string> nameofMethod) where T : class
{
return nameofMethod(instance);
}
// As static method
public static string GetMethodName<T>(Func<T, string> nameofMethod) where T : class
{
return nameofMethod(default);
}
Usage:
public class Car
{
public void Drive() { }
}
var car = new Car();
string methodName1 = car.GetMethodName(c => nameof(c.Drive));
var nullCar = new Car();
string methodName2 = nullCar.GetMethodName(c => nameof(c.Drive));
string methodName3 = GetMethodName<Car>(c => nameof(c.Drive));
If your application would allow a dependency on Moq (or a similar library), you could do something like this:
class Program
{
static void Main(string[] args)
{
var methodName = GetMethodName<IMyInteface>(x => new Action<string,string>(x.DoSomething));
Console.WriteLine(methodName);
}
static string GetMethodName<T>(Func<T, Delegate> func) where T : class
{
// http://code.google.com/p/moq/
var moq = new Mock<T>();
var del = func.Invoke(moq.Object);
return del.Method.Name;
}
}
public interface IMyInteface
{
void DoSomething(string param1, string param2);
}
I have a method and I want to add this method as an extension method to properties of my class.
This method give an expression as input parameter. The method is like below :
public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
I want to use this method like below example :
string propertyName = MyClass.Property1.GetPropertyName();
Is it possible? if yes, what is the solution?
No, it's not possible to do that. It's not clear whether MyClass is the name of a class (and Property1 is a static property) or whether it's an instance property and MyClass.Property1 simply isn't a valid member access. If it's the latter, you probably want to change your method to something like:
public static string GetPropertyName<TSource, TResult>(
Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
and call it as:
string propertyName = GetPropertyName<MyClass, string>(x => x.Property1);
Or you could use a generic class with a generic method, so that string can be inferred:
string propertyName = PropertyUtil<MyClass>.GetPropertyName(x => x.Property1);
That would be something like:
public static class PropertyUtil<TSource>
{
public static string GetPropertyName<TResult>(
Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}
Extension methods are type-specific (even when they are generic), you can't have an extension method for a property without having the same extension method available to all items that are of that type.
If you have an extension method for a specific type, it won't matter if it's a Property or a local variable or a class member, the extension method will still be availabe.
You can't create "static" extension methods.
Extension methods are "instance" methods.
You could just provide a helper class:-
string propertyName = PropertyHelper.GetPropertyName(() => instanceOfMyClass.Property1);
I'm clearly not an expert like Jon but this seems to me that what you want and is fairly simple (that's why I doubt, since Jon is clearly a reference person ! ;-)) :
class Program
{
static void Main(string[] args)
{
MyClass<int> myClass = new MyClass<int>();
string property1Name = myClass.Property1.GetPropertyName();
string property2Name = myClass.Property2.GetPropertyName();
}
}
public static class Extensions
{
public static string GetPropertyName<T>(this Expression<Func<T>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}
public class MyClass<T>
{
public Expression<Func<T>> Property1; //Sample with MyClass being generic
public Expression<Func<string>> Property2; //Sample which works anyway, MyClass being generic or not
}
It compiles and meet the requirements, from what I can see. You probably should have found it yourself, since you spoke about an extension method from start...
//ModelFor(person =>person.Name);
public void ModelFor<TModel, TValue>(
Expression<Func<TModel, TValue>> expression)
{
//Result should be "Name"
string nameOfTValue = ????;
}
EDIT: After your edit, I think you are want the name of the member involved in the expression, assuming of course that the expression is a member-expression in the first place.
((MemberExpression)expression.Body).Member.Name
To be more robust, you can do:
var memberEx = expression.Body as MemberExpression;
if (memberEx == null)
throw new ArgumentException("Body not a member-expression.");
string name = memberEx.Member.Name;
(Not relevant anymore):
To get a System.Type that represents the type of the TValue type-argument, you can use the typeof operator.
You probably want:
typeof(TValue).Name
But also consider the FullName and AssemblyQualifiedName properties if appropriate.
This really has nothing to do with expression-trees; you can use this technique to get the type of a type-argument for any generic method.
#Ani: I don't think this is right, I think that he wants the name of the parameter in the expression of type TValue
If this is true... this works 1 level deep only but might be handy anyway:
var nameOfTValue = ((MemberExpression)expression.Body).Member.Name;
Here is smarter implementation that should be able to deal with multiple levels:
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));
}
}
Usage:
var fieldName=PropertyName.For<Customer>(x=>x.Address.Region);
//fieldName==Address.Region
Another trick, this can be combined with reflection nicely:
public static T Set<T,TProp>(this T o,
Expression<Func<T,TProp>> field,TProp value){
var fn=((MemberExpression)field.Body).Member.Name;
o.GetType().GetProperty(fn).SetValue(o,value,null);
return o;
}
Allows to directly set properties with ease, can be useful for test fixtures:
var customer=new Customer("firstName","lastName");
customer.Set(x=>x.Name, "different firstName");
This probably applies to other places, but in WinForms, when I use binding I find many methods want to take the name of the property to bind to. Something like:
class Person
{
public String Name { get { ... } set { ... } }
public int Age { get { ... } set { ... } }
}
class PersonView
{
void Bind(Person p)
{
nameControl.Bind(p,"Name");
ageControl.Bind(p,"Age");
}
}
The big problem I keep having with this is that "Name" and "Age" are specified as strings. This means the compiler is no help if someone renames one of Person's properties. The code will compile fine, but the bindings will be broken.
Is there a standard way of solving this that I've missed? It feels like I need some keyword, maybe called stringof to match the existing typeof. You could use it something like:
ageControl.Bind(p,stringof(p.Age).Name);
stringof could return some class that has properties for getting the full path, part of the path, or the string so you can parse it up yourself.
Is something like this already do-able?
Have a look at this code snippet I've posted in another question, it can help you! (But only, if you are using .NET 3.5)
Best Regards
Oliver Hanappi
You can do that with expression trees, as explained in this question
protected static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> expression)
{
if (expression.NodeType == ExpressionType.Lambda && expression.Body.NodeType == ExpressionType.MemberAccess)
{
PropertyInfo prop = (expression.Body as MemberExpression).Member as PropertyInfo;
if (prop != null)
{
return prop.Name;
}
}
throw new ArgumentException("expression", "Not a property expression");
}
...
ageControl.Bind(p, GetPropertyName((Person p) => p.Age));
You can use Expressions to get compiler-checked bindings.
For example, in one of current projects we set up bindings like this:
DataBinder
.BindToObject(this)
.ObjectProperty(c => c.IsReadOnly)
.Control(nameTextBox, n => n.ReadOnly)
.Control(addressControl, n => n.ReadOnly)
Code supporting this style is separated into several classes:
public static class DataBinder
{
public static DataBinderBindingSourceContext<TDataSource> BindToObject<TDataSource>(TDataSource dataSource)
{
return new DataBinderBindingSourceContext<TDataSource>(dataSource);
}
}
public class DataBinderBindingSourceContext<TDataSource>
{
public readonly object DataSource;
public DataBinderBindingSourceContext(object dataSource)
{
DataSource = dataSource;
}
public DataBinderControlContext<TDataSource, TProperty> ObjectProperty<TProperty>(Expression<Func<TDataSource, TProperty>> property)
{
return new DataBinderControlContext<TDataSource, TProperty>(this, property);
}
}
public class DataBinderControlContext<TDataSource, TProperty>
{
readonly DataBinderBindingSourceContext<TDataSource> BindingSourceContext;
readonly string ObjectProperty;
public DataBinderControlContext
(
DataBinderBindingSourceContext<TDataSource> bindingSourceContext,
Expression<Func<TDataSource, TProperty>> objectProperty
)
{
BindingSourceContext = RequireArg.NotNull(bindingSourceContext);
ObjectProperty = ExpressionHelper.GetPropertyName(objectProperty);
}
public DataBinderControlContext<TDataSource, TProperty> Control<TControl>(TControl control, Expression<Func<TControl, TProperty>> property)
where TControl : Control
{
var controlPropertyName = ExpressionHelper.GetPropertyName(property);
control.DataBindings.Add(controlPropertyName, BindingSourceContext.DataSource, ObjectProperty, true);
return this;
}
}
public static class ExpressionHelper
{
public static string GetPropertyName<TResult>(Expression<Func<TResult>> property)
{
return GetMemberNames(((LambdaExpression)property).Body).Skip(1).Join(".");
}
public static string GetPropertyName<T, TResult>(Expression<Func<T, TResult>> property)
{
return GetMemberNames(((LambdaExpression)property).Body).Join(".");
}
static IEnumerable<string> GetMemberNames(Expression expression)
{
if (expression is ConstantExpression || expression is ParameterExpression)
yield break;
var memberExpression = (MemberExpression)expression;
foreach (var memberName in GetMemberNames(memberExpression.Expression))
yield return memberName;
yield return memberExpression.Member.Name;
}
}
public static class StringExtentions
{
public static string Join(this IEnumerable<string> values, string separator)
{
if (values == null)
return null;
return string.Join(separator, values.ToArray());
}
}
You could use reflection to find the name ;-)
This of course would be a circular reference, you'd use the name that you think it is to find the same name (or to not find anything, meaning the property was renamed... But there's an idea (or rather, a trick) : by making a do-nothing reference to the property you wish to use, you'd get compile time confirmation that it is still there. Only problem is if someone merely swap various property names around; in that case, the names still exist (no compile-time error), but have different application-level semantics (possible surprises in the application's output)