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

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

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?
是否有一种方法可以声明别名,使其仅仅是一个替代名称?


<details>
<summary>英文:</summary>

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&#39;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&#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);

/*---what i&#39;d like---*/
//way to call ClearScreen so that cls implicitly inherits its arguments //probably unfeasable
void cls(...args) =&gt; CLearScreen(...args);

//literally assigning ClearScreen to cls //again, probably unfeasable as Delegates are strongly typed
void* cls = &amp;ClearScreen; 

//apply some attribute to the original method //i&#39;m more optimistic about this
[AliasedAs(&quot;cls&quot;)]  
void ClearScreen(MyType1 Arg1, MyType2 Arg2){ ... }

//instruct the compiler as to treat them as the same identifier
#HeyCompilerSwitchClsWithClearScreenAcrossMyProgram 

}


--EDIT--  
Answering &quot;WHY do you want to do this?&quot; 

it&#39;s my 3rd day on C#, so i&#39;m still assessing what one can and cannot do.  
To answer you @Guru Stron, i have an utility class and a method that does &quot;full.qualified.type&quot; =&gt; &quot;type&quot;.  
I&#39;ll use it for fast dbg and object inspection while learning, which means it need to be short-named.  

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.  
So the plan is using the full-fledged name for &#39;production&#39; uses and the fast one for dbg/learning.

I&#39;ll may or may not need the same pattern for other utilities too.
(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)  
(as there surely is a dbg var inspector in Visual Studio)

</details>


# 答案1
**得分**: 1

我不知道针对这个问题是否有即时解决方案,但我可以考虑一些类似的解决方法:
```C#
public class Cls
{
    public void AliasedClearScreen(params object[] args) =&gt;
        GetAliased(nameof(ClearScreen), args);

    public void ClearScreen(Type type)
    {
        Console.WriteLine(type.Name);
    }

    void GetAliased(string method, params object[] args)
    {
        var type = Type.GetType(nameof(Cls));
        var myMethod = type.GetMethod(method);
        myMethod.Invoke(this, args);
    }
}

示例用法可能如下:

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

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

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


<details>
<summary>英文:</summary>

I am not aware of any immediate solutions to that problem, but i can think of some sort of workaround:
```C#
public class Cls
{
    public void AliasedClearScreen(params object[] args) =&gt;
        GetAliased(nameof(ClearScreen), args);

    public void ClearScreen(Type type)
    {
        Console.WriteLine(type.Name);
    }

    void GetAliased(string method, params object[] args)
    {
        var type = Type.GetType(nameof(Cls));
        var myMethod = type.GetMethod(method);
        myMethod.Invoke(this, args);
    }
}

and sample usage would be

var test = new Cls();
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:

确定