nameof special operator names #2617
Replies: 10 comments
-
One of the purposes of nameof is to be able to rename variables/members more easily. Given that these won't change, why not define constants? |
Beta Was this translation helpful? Give feedback.
-
As I can see it, there is 1 major reasons to use nameof. It enabled much better tooling support for renaming, usage analysis, and other things. Right now it is limited to elements which CAN be renamed, which then obviously limits the usage to renaming, but I don't see why that limitation exists and thus limits what the feature can be used for. |
Beta Was this translation helpful? Give feedback.
-
What would your use case be? What would nameof give you over Roslyn providing a nuget package with these constants supplied? |
Beta Was this translation helpful? Give feedback.
-
Try this: public static class Names {
class Add { public static Add operator + (Add a, Add b) => default; }
public static string OpAdd => typeof (Add).GetMethods ()[0].Name; // = "op_Addition"
} |
Beta Was this translation helpful? Give feedback.
-
Yeah, I've wanted this before for things like getting the name of indexers. typeof(string).GetDefaultMembers(); // returns MemberInfo[]{Char Chars[Int32]}
public class F
{
this[int x]=> 0;
}
typeof(F).GetDefaultMembers() // returns MemberInfo[0] Unfortunately, I was told tough. |
Beta Was this translation helpful? Give feedback.
-
private static readonly MethodInfo _getConversionMethodInfo =
typeof(BooleanConverter).GetMethod("op_Implicit", ..., null, new[] {typeof(bool)}, null);
private static void EmitConvertFromBool(ILGenerator il) => il.Emit(OpCodes.Call, _getConversionMethodInfo ); Now, if I go to the implicit operator and remove it from code, it will fail at RUNTIME, rather than COMPILE time. If I am diligent and first use the tooling support to check for all usage prior to removing it, I can still not see it is being used anywhere. In fact, the reason I made this report in the first place was because i made a constructor private for a class to convert it into a singleton, but that constructor was used in the IL generation. If I had used nameof(class.ctor), I wouldn't have had to deal with that, the compiler would have detected the issue. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@qrli That argumentation is also true for current implementation of nameof, so don't see why that should prevent nameof to cover compiler named entities. |
Beta Was this translation helpful? Give feedback.
-
The limitations of I'm not saying that these changes shouldn't happen, just giving some backstory as to why |
Beta Was this translation helpful? Give feedback.
-
Not to mention open generics: #702 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
nameof currently only support "obvious" names. but it would be nice if you can also reference operators, indexers etc.
e.g. nameof(myclass.operator==) => 'op_Equality', or
nameof(myclass.implicit) => 'op_Implicit'.
It would also be acceptable to have to give the generated name nameof(myclass.op_Equality) => 'op_Equality', since the main point in my case would be to track usage of code used in IL generation.
Beta Was this translation helpful? Give feedback.
All reactions