nameof

Difference between nameof and typeof

心不动则不痛 提交于 2021-02-17 15:17:05
问题 Correct me if I am wrong, but doing something like var typeOfName = typeof(Foo).Name; and var nameOfName = nameof(Foo); should give you exactly the same output. One of the understandable reasons according to this source: https://msdn.microsoft.com/en-us/library/dn986596.aspx is that "Using nameof helps keep your code valid when renaming definitions" If you want to get the class instance as string it is not possible to do something like that: var fooInstance = new Foo(); var nameOfName =

Difference between nameof and typeof

旧时模样 提交于 2021-02-17 15:14:40
问题 Correct me if I am wrong, but doing something like var typeOfName = typeof(Foo).Name; and var nameOfName = nameof(Foo); should give you exactly the same output. One of the understandable reasons according to this source: https://msdn.microsoft.com/en-us/library/dn986596.aspx is that "Using nameof helps keep your code valid when renaming definitions" If you want to get the class instance as string it is not possible to do something like that: var fooInstance = new Foo(); var nameOfName =

Difference between nameof and typeof

我与影子孤独终老i 提交于 2021-02-17 15:11:41
问题 Correct me if I am wrong, but doing something like var typeOfName = typeof(Foo).Name; and var nameOfName = nameof(Foo); should give you exactly the same output. One of the understandable reasons according to this source: https://msdn.microsoft.com/en-us/library/dn986596.aspx is that "Using nameof helps keep your code valid when renaming definitions" If you want to get the class instance as string it is not possible to do something like that: var fooInstance = new Foo(); var nameOfName =

How to handle nameof(this) to report class name

江枫思渺然 提交于 2021-02-06 09:55:44
问题 I'd like to use the following C#6 code var joe = new Self(); Console.WriteLine(joe); ... and get the following output: joe The following attempt class Self { public string Name { get; set; } = nameof(this); public override string ToString() { return Name; } } fails as nameof cannot be applied to this . Is it there a workaround for this problem? EDIT . The scenario I'm working with assures that no two references point to the same Self object. 回答1: No, nameof is designed to refer to the compile

F# nameof operator not a first-class function

元气小坏坏 提交于 2021-01-27 07:52:02
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

F# nameof operator not a first-class function

Deadly 提交于 2021-01-27 07:50:49
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

Continued “Is there an equivalent of C#'s nameof(..) in F#?”

丶灬走出姿态 提交于 2020-12-10 17:01:33
问题 With reference to Is there an equivalent of C#'s nameof(..) in F#? how can the nameof function used or extended for the following case? let nameof (q:Expr<_>) = match q with | Patterns.Let(_, _, DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _))) -> mi.Name | Patterns.PropertyGet(_, mi, _) -> mi.Name | DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _)) -> mi.Name | _ -> failwith "Unexpected format" let any<'R> : 'R = failwith "!" let s = _nameof <@ System.Char.IsControl @> //OK type A<'a>()

Continued “Is there an equivalent of C#'s nameof(..) in F#?”

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-10 17:01:24
问题 With reference to Is there an equivalent of C#'s nameof(..) in F#? how can the nameof function used or extended for the following case? let nameof (q:Expr<_>) = match q with | Patterns.Let(_, _, DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _))) -> mi.Name | Patterns.PropertyGet(_, mi, _) -> mi.Name | DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _)) -> mi.Name | _ -> failwith "Unexpected format" let any<'R> : 'R = failwith "!" let s = _nameof <@ System.Char.IsControl @> //OK type A<'a>()

Continued “Is there an equivalent of C#'s nameof(..) in F#?”

妖精的绣舞 提交于 2020-12-10 17:00:19
问题 With reference to Is there an equivalent of C#'s nameof(..) in F#? how can the nameof function used or extended for the following case? let nameof (q:Expr<_>) = match q with | Patterns.Let(_, _, DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _))) -> mi.Name | Patterns.PropertyGet(_, mi, _) -> mi.Name | DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _)) -> mi.Name | _ -> failwith "Unexpected format" let any<'R> : 'R = failwith "!" let s = _nameof <@ System.Char.IsControl @> //OK type A<'a>()

Continued “Is there an equivalent of C#'s nameof(..) in F#?”

柔情痞子 提交于 2020-12-10 16:56:12
问题 With reference to Is there an equivalent of C#'s nameof(..) in F#? how can the nameof function used or extended for the following case? let nameof (q:Expr<_>) = match q with | Patterns.Let(_, _, DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _))) -> mi.Name | Patterns.PropertyGet(_, mi, _) -> mi.Name | DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _)) -> mi.Name | _ -> failwith "Unexpected format" let any<'R> : 'R = failwith "!" let s = _nameof <@ System.Char.IsControl @> //OK type A<'a>()