nameof

Why is nameof(object) not allowed?

陌路散爱 提交于 2019-12-01 16:54:29
In C# 6.0 you can write this: var instance = default(object); var type = typeof(object); They have the same result of: var instance = default(System.Object); var type = typeof(System.Object); But you can't write this: var name = nameof(object); It generates the following error: Invalid expression term 'object'. But you can still write this: var name = nameof(System.Object); Why nameof(object) does not compile? The difference is that object is a synonym for the class Object and nameof() doesn't work on synonyms. Same applies to nameof(int) vs nameof(Int32) 来源: https://stackoverflow.com

nameof equivalent in Java

℡╲_俬逩灬. 提交于 2019-11-30 16:37:52
问题 C# 6.0 introduced the nameof() operator, that returns a string representing the name of any class / function / method / local-variable / property identifier put inside it. If I have a class like this: class MyClass { public SomeOtherClass MyProperty { get; set; } public void MyMethod() { var aLocalVariable = 12; } } I can use the operator like this: // with class name: var s = nameof(MyClass); // s == "MyClass" // with properties: var s = nameof(MyClass.OneProperty); // s == "OneProperty" //

Why does nameof return only last name?

孤街浪徒 提交于 2019-11-30 14:29:11
问题 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? Edit: For example, such behavior is

Get the name of a variable (like nameof operator in C#) [duplicate]

余生长醉 提交于 2019-11-30 09:12:59
This question already has an answer here: Variable name as a string in Javascript 14 answers Let's assume we have the following code: const myVariable = { age: 7, name: "Hunter" }; I want to know the name of the variable, not just the value of it, and put it in a string or log it or: const s = nameof(myVariable) + ': ' + JSON.stringify(myVariable); And the result I want to see is sth like: "myVariable: {"age":7,"name":"Hunter"}" Looking forward to see your solution. This is my own answer in Q&A style which comes with a bit of a compromise. The trick is here to pass the parameter to the

Get the name of a variable (like nameof operator in C#) [duplicate]

百般思念 提交于 2019-11-29 12:43:09
问题 This question already has answers here : Variable name as a string in Javascript (14 answers) Closed 3 years ago . Let's assume we have the following code: const myVariable = { age: 7, name: "Hunter" }; I want to know the name of the variable, not just the value of it, and put it in a string or log it or: const s = nameof(myVariable) + ': ' + JSON.stringify(myVariable); And the result I want to see is sth like: "myVariable: {"age":7,"name":"Hunter"}" Looking forward to see your solution. 回答1:

How to change language version in Visual Studio 2015

纵然是瞬间 提交于 2019-11-29 03:01:38
I want to use the nameof operator in my C# project in Visual Studio 2015 but the compiler complains with the following message. Feature 'nameof operator' is not available in C# 5. Please use language version 6 or greater. I want to know how I can change the C# language version from Visual Studio 2015. Try this.. Project -> Properties -> Build -> Advanced -> Language Version Go to Project → Properties → Build → Advanced → Language Version → OK as shown with detail steps and screen shots below: Follow these steps to change the language version of your project 1: Open your project with Visual

How to change language version in Visual Studio 2015

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 17:16:51
问题 I want to use the nameof operator in my C# project in Visual Studio 2015 but the compiler complains with the following message. Feature 'nameof operator' is not available in C# 5. Please use language version 6 or greater. I want to know how I can change the C# language version from Visual Studio 2015. 回答1: Try this.. Project -> Properties -> Build -> Advanced -> Language Version 回答2: Go to Project → Properties → Build → Advanced → Language Version → OK as shown with detail steps and screen

nameof expression in .net framework 4

一世执手 提交于 2019-11-27 13:57:58
"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 =

What is the purpose of nameof?

↘锁芯ラ 提交于 2019-11-27 02:37:13
Version 6.0 got a new feature of nameof , but I can't understand the purpose of it, as it just takes the variable name and changes it to a string on compilation. I thought it might have some purpose when using <T> but when I try to nameof(T) it just prints me a T instead of the used type. Any idea on the purpose? What about cases where you want to reuse the name of a property, for example when throwing exception based on a property name, or handling a PropertyChanged event. There are numerous cases where you would want to have the name of the property. Take this example: switch (e.PropertyName

nameof expression in .net framework 4

泪湿孤枕 提交于 2019-11-26 16:33:16
问题 "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. 回答1: 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>>