【问题标题】:Is there ready to use method to rename C# operator names to symbols是否准备好使用方法将 C# 运算符名称重命名为符号
【发布时间】:2015-04-17 03:49:21
【问题描述】:

C# 将所有运算符定义为静态方法。这也是他们在反思中得到的名字。出于显示目的,我正在寻找一种重命名方法,它将所有这些字符串转换为它们的 c# 代码表示形式,如 +、-、*、/、>>、

编辑:当然它很容易编码。但我正在寻找官方的 .NET 方法或其他解决方案。

op_Implicit
op_explicit
op_Addition
op_Subtraction
op_Multiply
op_Division
op_Modulus
op_ExclusiveOr
op_BitwiseAnd
op_BitwiseOr
op_LogicalAnd
op_LogicalOr
op_Assign
op_LeftShift
op_RightShift
op_SignedRightShift
op_UnsignedRightShift
op_Equality
op_GreaterThan
op_LessThan
op_Inequality
op_GreaterThanOrEqual
op_LessThanOrEqual
op_MultiplicationAssignment
op_SubtractionAssignment
op_ExclusiveOrAssignment
op_LeftShiftAssignment
op_ModulusAssignment
op_AdditionAssignment
op_BitwiseAndAssignment
op_BitwiseOrAssignment
op_Comma
op_DivisionAssignment
op_Decrement
op_Increment
op_UnaryNegation
op_UnaryPlus
op_OnesComplement

【问题讨论】:

  • 您可以使用字典将名称映射到运算符吗?
  • 为什么你期望有一个内置方法返回一个运算符方法名称的运算符符号?按照建议使用字典。
  • @SriramSakthivel - 辨别 op_GreaterThan 和 op_RightShift 怎么样?
  • @HenkHolterman:OP 没有说他现在需要按符号的方法名称,而是按名称获取符号。
  • 对,我看错了这里想要的方向。

标签: c# operators rename


【解决方案1】:

毕竟只有 38 项 (reference for all symbols)

var map = new Dictionary<string,string>{
{"op_Implicit",""},
{"op_explicit",""},
{"op_Addition","+"},
{"op_Subtraction","-"},
{"op_Multiply","*"},
{"op_Division","/"},
{"op_Modulus","%"},
{"op_ExclusiveOr","^"},
{"op_BitwiseAnd","&"},
{"op_BitwiseOr","|"},
{"op_LogicalAnd","&&"},
{"op_LogicalOr","||"},
{"op_Assign","="},
{"op_LeftShift","<<"},
{"op_RightShift",">>"},
{"op_SignedRightShift",""},
{"op_UnsignedRightShift",""},
{"op_Equality","=="},
{"op_GreaterThan",">"},
{"op_LessThan","<"},
{"op_Inequality","!="},
{"op_GreaterThanOrEqual",">="},
{"op_LessThanOrEqual","<="},
{"op_MultiplicationAssignment","*="},
{"op_SubtractionAssignment","-="},
{"op_ExclusiveOrAssignment","^="},
{"op_LeftShiftAssignment","<<="},
{"op_ModulusAssignment","%="},
{"op_AdditionAssignment","+="},
{"op_BitwiseAndAssignment","&="},
{"op_BitwiseOrAssignment","|="},
{"op_Comma",","},
{"op_DivisionAssignment","/="},
{"op_Decrement","--"},
{"op_Increment","++"},
{"op_UnaryNegation","-"},
{"op_UnaryPlus","+"},
{"op_OnesComplement","~"}
};

【讨论】:

  • 好吧,很公平。但我仍然问自己是否在.NET 框架的某个地方隐藏了一个方法。不过还是谢谢...
  • 你去吧,这个列表有错误,你混合了逻辑和位运算符......这就是我的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2012-01-30
  • 2018-09-06
相关资源
最近更新 更多