英文:
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'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?
//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'd like---*/
//way to call ClearScreen so that cls implicitly inherits its arguments //probably unfeasable
void cls(...args) => CLearScreen(...args);
//literally assigning ClearScreen to cls //again, probably unfeasable as Delegates are strongly typed
void* cls = &ClearScreen;
//apply some attribute to the original method //i'm more optimistic about this
[AliasedAs("cls")]
void ClearScreen(MyType1 Arg1, MyType2 Arg2){ ... }
//instruct the compiler as to treat them as the same identifier
#HeyCompilerSwitchClsWithClearScreenAcrossMyProgram
}
--EDIT--
Answering "WHY do you want to do this?"
it's my 3rd day on C#, so i'm still assessing what one can and cannot do.
To answer you @Guru Stron, i have an utility class and a method that does "full.qualified.type" => "type".
I'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's better for it to be long-named for mantenaibility.
So the plan is using the full-fledged name for 'production' uses and the fast one for dbg/learning.
I'll may or may not need the same pattern for other utilities too.
(p.s. there's for sure already a built-in method that does that, but i cannot spend days searching/memorizing all the built-ins, i'll learn them on the way)(EDIT It'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) =>
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) =>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论