c#静态公共方法

c#静态公共方法,c#,public-method,C#,Public Method,在名为Security的类中,有一个方法: public static bool HasAccess(string UserId, string ModuleID) 如何调用此方法,使其返回bool结果 我尝试了以下方法,但没有成功: Security security = new Security(); bool result = security.HasAccess("JKolk","Accounting"); 您只需使用类名。不需要创建实例 Security.H

在名为Security的类中,有一个方法:

    public static bool HasAccess(string UserId, string ModuleID)
如何调用此方法,使其返回bool结果

我尝试了以下方法,但没有成功:

    Security security = new Security();
    bool result = security.HasAccess("JKolk","Accounting");

您只需使用类名。不需要创建实例

Security.HasAccess( ... )
要调用静态方法,您不需要实例化调用它的对象

请注意,您可以混合和匹配静态和非静态成员,例如:

public class Foo
{
    public static bool Bar() { return true; }
    public bool Baz() { return true; }

    public static int X = 0;
    public int Y = 1;
}

Foo f = new Foo();
f.Y = 10; // changes the instance
f.Baz(); // must instantiate to call instance method

Foo.X = 10; // Important: other consumers of Foo within the same AppDomain will see this value
Foo.Bar(); // call static methods without instantiating the type

由于它是一种静态方法,您应该执行以下操作

 Security.HasAccess(("JKolk","Accounting"); 

如果它是一个静态方法,那么调用它的方法如下:

bool result = Security.HasAccess("JKolk","Accounting");
您不会使用
安全性
类的实例,而是使用
安全性
类的定义

bool result = Security.HasAccess("JKolk","Accounting");