nameof

Using nameof() with Url.Action() and async methods in ASP.NET Core 3.x MVC

给你一囗甜甜゛ 提交于 2020-02-25 01:52:07
问题 Let's say I have a ASP.NET Core 3.0 MVC application, which features a simple controller containing two actions and using attribute based routing: [Route("home")] public class HomeController : Controller { public static string ControllerName { get; } = "Home"; public HomeController() { } string GenerateUrls() { string url1 = Url.Action(nameof(Action1), ControllerName); string url2 = Url.Action(nameof(Action2Async), ControllerName); return $"Action1: '{url1}'\nAction2: '{url2}'"; } [HttpGet("a1

Is there any way for the nameof operator to access method parameters (outside of the same method)?

血红的双手。 提交于 2020-02-01 09:52:25
问题 Take the following class and method: public class Foo public Foo Create(string bar) { return new Foo(bar); } So getting "Create" is obvious: nameof(Foo.Create) Is there any way to get "bar" other than using reflection to read the parameters at run time? 回答1: No. There is no way to get the parameter names from the outside of the method using nameof . nameof doesn't work for method parameters if you want the name on the calling side (for the callee it does work obviously). The other methods you

How do I make MonoDevelop recognize nameof syntax from C# 6.0?

那年仲夏 提交于 2019-12-25 02:24:28
问题 I'm in MonoDevelop v5.9.6. Although it seems to support C# 6.0, the editor doesn't recognize the nameof keyword, and it marks it red, because it tries to recognize it as if it were an identifier. Is there any hack I can use to make it work in the editor, without breaking the compilation somehow? 回答1: This hack works: // hack to make MonoDevelop recognize nameof syntax from C#6.0 using nameof = System.Func<string>; The editor recognizes it as "returning a string" and doesn't give any errors

Why doesn't the compiler give any errors or warnings when using this hack?

烂漫一生 提交于 2019-12-24 18:29:21
问题 In my other question, I found a hack to make this syntax work in MonoDevelop editor: // hack to make MonoDevelop recognize nameof syntax from C#6.0 using nameof = System.Func<string>; The C# compilers (Mono and VS) don't give any warnings or errors, and usages of the nameof keyword also work normally. My question is why. 回答1: I'm not a language lawyer but I believe the reason your code works is that nameof is a contextual keyword Let's take a step back to a more general case. If you try to

Is there a Swift equivalent of C#'s 'nameof()' function to get a variable or member's name?

房东的猫 提交于 2019-12-23 10:11:30
问题 Ok, there's an existing question here on S/O with the following title: Swift: Get Variable Actual Name as String By it's name, it seems that's exactly what I want. However, looking at the accepted answer (and the other non-accepted ones), they are referring to key path manipulation, which isn't what I'm after. (i.e. This is not a duplicate!) In my case, I want the name of one variable to be stored in a second variable of type string. In C#, this is trivial using nameof , like so... int

Getting the calling variable name of a parameter

末鹿安然 提交于 2019-12-22 09:06:38
问题 In relation to the question Get the name of parameters from a calling method and Finding the Variable Name passed to a Function in C# I'm still looking for a way to define the WhatDoesTheAnimalSay_WANTED method: I want to know the name of the variable that's used as a parameter: public class Farm { public readonly string Cow = "Muuuuhh"; public string Cat { get; set; } public void MainFunction() { var dog = "WauWau"; var kiwi = new Bird("QueeeekQueeek"); Cat = "Miiiaaauuuu"; // This one works

Why is nameof(object) not allowed?

无人久伴 提交于 2019-12-19 17:42:38
问题 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? 回答1: The difference is that object is a synonym for the class Object and nameof()

Get class's property name at compile time without object instantiation

匆匆过客 提交于 2019-12-11 08:45:27
问题 Is it possible to get the name of class's property (attention!) at compile time and without object instantiation? With instantiation it can easely be done with nameof(): class DummyClass { public int DummyProperty { get; set; } } void Meth() { //With instantiation var dc = new DummyClass(); var prname = nameof(dc.DummyProperty); } 回答1: You may use nameof(DummyClass.DummyProperty) , if I understood you correctly. There is a similar example for such a use case at docs. Used to obtain the simple

Expression vs nameof

痞子三分冷 提交于 2019-12-08 19:35:58
问题 It is a good idea to use nameof over expressions for extracting property names? //method with expression protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, bool isValid, [param: Localizable(true)] string validationError) { string propertyName = PropertySupport.ExtractPropertyName(propertyExpression); RaisePropertyChanged(propertyName, isValid, validationError); } //the same logic without expression protected void RaisePropertyChanged(string propertyName, [param:

Get the original name of a variable passed as a parameter?

馋奶兔 提交于 2019-12-05 03:55:14
问题 To be clear, this is Not a duplicate of this question. Obviously, I can use the nameof operator to get the name of a variable or a parameter; I know that. But is there a way I can get the original name of a variable that's passed to a method? Currently, I have to do it like this: static void Foo(string someVariable, string variableName) { if (!FulfilsCondition(someVariable)) Console.WriteLine($"{variableName} is bad!"); // More code } And I call it like this: string bar = string.Empty; Foo