简体   繁体   English

将另一个类中的方法注册到静态类?

[英]Register a method in another class to a static class?

Is there any possibilities to register a method in another class to a static class in C#, so that i can call that class using the static class name. 是否有可能将另一个类中的方法注册到C#中的static类中,以便我可以使用static类名来调用该类。

Example

public static class StaticClass
{

 }


public static class Sample
{
public static string method1()
{return "";}  
} 

I want to call like this, StaticClass.method1() . 我想这样调用, StaticClass.method1()

First of all,IS IT VALID or Not? 首先,它是否有效? or Is there any possible way to do that? 有什么可能的方法吗?

First of all,IS IT VALID or Not? 首先,它是否有效?

Sure it's valid; 确定有效; just not the way you're portraying it. 而不是您描绘的方式。

Is there any possible way to do that? 有什么可行的方法吗?

One approach would be to build a method on StaticClass called method1 . 一种方法是在StaticClass上构建一个名为method1的方法。 Maybe you want to encapsulate some default behavior: 也许您想封装一些默认行为:

public static class StaticClass
{
    public static string method1()
    {
        // do what you need

        Sample.method1();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM