C# 别名/重载一个方法,而不明确声明其类型签名。

huangapple go评论115阅读模式
英文:

C# alias/overload a method without explicitly stating its type signature

问题

I gave some methods a shortened alias name.
我给一些方法取了一个缩写的别名。

As the alias is just another way to call the same exact method it has the same signature (return type + arguments type) and always will.
由于别名只是调用相同方法的另一种方式,它具有相同的签名(返回类型 + 参数类型)并且永远都会是这样。

I'd like to avoid in the developing phase to duplicate each modification to the original method on the alias.
在开发阶段,我想避免将每个对原始方法的修改重复到别名上。

Is there a way to declare the alias so that it's just an alternative name?
是否有一种方法可以声明别名,使其仅仅是一个替代名称?

  1. <details>
  2. <summary>英文:</summary>
  3. I gave some methods a shortened alias name.
  4. As the alias is just another way to call the same exact method it has the same signature (return type + arguments type) and always will.
  5. I&#39;d like to avoid in the developing phase to duplicate each modification to the original method on the alias.
  6. Is there a way to declare the alias so that it&#39;s just an alternative name?

//the method it's not the actual one, it just illustrates the point
internal static class Utils {
/---right now---/
public static void ClearScreen(MyType1 Arg1, MyType2 Arg2){
//method body
}
//explicit alias of ClearScreen
public static void cls(MyType1 Arg1, MyType2 Arg2) => ClearScreen(Arg1,Arg2);

  1. /*---what i&#39;d like---*/
  2. //way to call ClearScreen so that cls implicitly inherits its arguments //probably unfeasable
  3. void cls(...args) =&gt; CLearScreen(...args);
  4. //literally assigning ClearScreen to cls //again, probably unfeasable as Delegates are strongly typed
  5. void* cls = &amp;ClearScreen;
  6. //apply some attribute to the original method //i&#39;m more optimistic about this
  7. [AliasedAs(&quot;cls&quot;)]
  8. void ClearScreen(MyType1 Arg1, MyType2 Arg2){ ... }
  9. //instruct the compiler as to treat them as the same identifier
  10. #HeyCompilerSwitchClsWithClearScreenAcrossMyProgram

}

  1. --EDIT--
  2. Answering &quot;WHY do you want to do this?&quot;
  3. it&#39;s my 3rd day on C#, so i&#39;m still assessing what one can and cannot do.
  4. To answer you @Guru Stron, i have an utility class and a method that does &quot;full.qualified.type&quot; =&gt; &quot;type&quot;.
  5. I&#39;ll use it for fast dbg and object inspection while learning, which means it need to be short-named.
  6. But what if i want to insert it in persistent logs or long-lasting features? It&#39;s better for it to be long-named for mantenaibility.
  7. So the plan is using the full-fledged name for &#39;production&#39; uses and the fast one for dbg/learning.
  8. I&#39;ll may or may not need the same pattern for other utilities too.
  9. (p.s. there&#39;s for sure already a built-in method that does that, but i cannot spend days searching/memorizing all the built-ins, i&#39;ll learn them on the way)(EDIT It&#39;s GetType().Name)
  10. (as there surely is a dbg var inspector in Visual Studio)
  11. </details>
  12. # 答案1
  13. **得分**: 1
  14. 我不知道针对这个问题是否有即时解决方案,但我可以考虑一些类似的解决方法:
  15. ```C#
  16. public class Cls
  17. {
  18. public void AliasedClearScreen(params object[] args) =&gt;
  19. GetAliased(nameof(ClearScreen), args);
  20. public void ClearScreen(Type type)
  21. {
  22. Console.WriteLine(type.Name);
  23. }
  24. void GetAliased(string method, params object[] args)
  25. {
  26. var type = Type.GetType(nameof(Cls));
  27. var myMethod = type.GetMethod(method);
  28. myMethod.Invoke(this, args);
  29. }
  30. }

示例用法可能如下:

  1. var test = new Cls();
  2. test.AliasedClearScreen(typeof(string));

但是,这样做将会丢失方法的正确签名。

但是,对您来说,另一个选项可能是源生成器

  1. <details>
  2. <summary>英文:</summary>
  3. I am not aware of any immediate solutions to that problem, but i can think of some sort of workaround:
  4. ```C#
  5. public class Cls
  6. {
  7. public void AliasedClearScreen(params object[] args) =&gt;
  8. GetAliased(nameof(ClearScreen), args);
  9. public void ClearScreen(Type type)
  10. {
  11. Console.WriteLine(type.Name);
  12. }
  13. void GetAliased(string method, params object[] args)
  14. {
  15. var type = Type.GetType(nameof(Cls));
  16. var myMethod = type.GetMethod(method);
  17. myMethod.Invoke(this, args);
  18. }
  19. }

and sample usage would be

  1. var test = new Cls();
  2. test.AliasedClearScreen(typeof(string));

But with this you will loose correct signature of the method.

But another options for you may be Source Generators

huangapple
  • 本文由 发表于 2023年6月26日 18:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定