在Interface中初始化值

Max*_*eel 2 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有一个暴露接口的DLL,看起来像这样:

public Interface IClientGroup
{
IQueryable ClientsGroup {get;}
void Activate(ClientGroup clientgroup);
//many other members and functions
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器类中,它在构造函数中传递,如下所示:

public ControllerClass(IClientGroup clientgroup)
{
  var _clientgroup = clientgroup
}

//later _clientgroup used to access everything in Interface
Run Code Online (Sandbox Code Playgroud)

现在,当我调试时,我看到它被传递给了构造函数,这些值已经初始化了,所以我假设我可以简单地传入IClientgroup clientgroup任何函数并且它已经被初始化但是它是null,每次如果我在使用之前声明它并且说it is type but used as variable如果我diectly传递给函数在consturctor完成.

public UseValues(IclientGroup clientgroup)
{
  //error: IClientGroup is type but used as variable
}
Run Code Online (Sandbox Code Playgroud)

如何在初始化值的情况下使用客户端组?我无法从dll中看到确切的实现.

Sri*_*vel 5

public ControllerClass(IClientGroup clientgroup)
{
  var _clientgroup = clientgroup
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将clientgroup参数存储到局部变量,而不是实例字段.您需要将其存储在实例字段中,以便以后使用它.

class ControllerClass
{
    private IClientGroup _clientgroup;
    public ControllerClass(IClientGroup clientgroup)
    {
       if(clientgroup == null)
       {
           //Don't allow null values
           throw new ArgumentNullException("clientgroup");
       }
       this._clientgroup = clientgroup
    }

    void SomeMethod()
    {
        //Use this._clientgroup here
    }
}
Run Code Online (Sandbox Code Playgroud)

除了问题,你真的需要一个初学者教程