C#编译器:无法在非静态上下文中访问静态方法 [英] C# Compiler : cannot access static method in a non-static context

查看:75
本文介绍了C#编译器:无法在非静态上下文中访问静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public class Anything
{
    public int Data { get; set;}
}

public class MyGenericBase<T>
{
    public void InstanceMethod(T data)
    {
        // do some job
    }

    public static void StaticMethod(T data)
    {
        // do some job
    }

    // others members...
}

public sealed class UsefulController : MyGenericBase<Anything>
{
    public void ProxyToStaticMethod()
    {
        StaticMethod(null);
    }

    // others non derived members...
}

public class Container
{
    public UsefulController B { get; set; }
}

public class Demo
{
    public static void Test()
    {
        var c = new Container();
        c.B.InstanceMethod(null);   // Works as expected.
        c.B.StaticMethod(null);     // Doesn't work. 
                                    // Static method call on object rather than type. 
                                    // How to get the static method on the base type ?
        c.B.ProxyToStaticMethod();  // Works as expected.
    }
}

编译器非常生气……我理解错误消息,但是我不知道如何解决.我试图获取类型而不是对象来进行静态方法调用,但是我找不到正确执行此方法的方法.而且,这导致一点都不优雅.

The compiler is very angry... I understand the error message but I don't know how to solve this. I was trying to get a type rather than an object to make my static method call, but I don't find the way to do it correctly. Moreover this results in something not elegant at all.

基本上,GenericBase是框架中的类,具有许多静态方法和一些实例方法.控制器正在输入此类并对其进行扩展.

Basically, the GenericBase is a class from a framework with a lot of static methods and some instance methods. The controller is typing this class and extending it.

容器是一组与逻辑相关的控制器.

The container is a group of logical related controllers.

有趣的事情:此代码的Java版本可正确编译,但带有警告.执行也是正确的.

Interesting thing : a Java version of this code compiles correctly, but with a warning. The execution is correct, too.

是否存在解决此问题的设计模式?

Does it exist a design pattern to solve this ?

感谢您的输入!

由于您的回答,我找到了解决该问题的方法.似乎可行,但我不能确定是否存在副作用.

I found a way to get rid of this problem, thanks to your answers. It seems to work, but I can not tell if there are side effects right know.

    public class GenericBase<T> : MyGenericBase<T>
{
    // Create instance calls here for every base static method.
}

public sealed class UsefulController : GenericBase<Anything>
{
    // others non derived members...
}

推荐答案

对静态方法的调用将被编译为在特定类上调用特定的静态方法.换句话说,它不会使用B的内容来确定要调用的静态方法.

A call to a static method will be compiled to call a specific static method on a specific class. In other words, it won't use the contents of B to determine which static method to call.

因此,该调用必须在编译时可解析,因此它会抱怨,因为就其所知,您可以用多个具体类型替换该属性的内容,这意味着对static方法的调用必须在这两个类中都可以解析为静态方法.

So the call has to be resolvable at compile time, hence it complains, because for all it knows, you could replace the contents of that property with multiple concrete types, which would mean that the call to the static method would have to be resolved to a static method in either of these classes.

编译器没有像虚拟或抽象静态方法那样的东西,因此对于一个编译器,您不能保证所有这些类都具有该静态方法.而且由于调用必须在编译时就可以解决,所以它不会那样工作.

The compiler does not have anything like a virtual or abstract static method, so for one you can't guarantee that all of those classes have that static method. And since the call has to be resolvable at compile time, it won't work like that.

您已经注意到,可以调用对象的实例方法,该对象又调用静态方法.这不会使上述规则无效,因为当编译器编译该实例方法时,它将调用的静态方法是恒定且已知的.

You can, as you've noticed, call an instance method of the object, which in turn calls the static method. This does not invalidate the above rules since when the compiler compiles that instance method, which static method it will call is constant and known.

这篇关于C#编译器:无法在非静态上下文中访问静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆