初始化接口中的值

本文关键字:接口 初始化 | 更新日期: 2023-09-27 18:15:45

我有一个dll,它公开了如下所示的接口:

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

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

public ControllerClass(IClientGroup clientgroup)
{
     var _clientgroup = clientgroup
}
//later _clientgroup used to access everything in Interface

现在,当我调试时,我看到它在传递给构造函数时,值已经初始化,所以我假设我可以简单地在任何函数中传递IClientgroup clientgroup,它将已经初始化,但它是空的,每次如果我在使用之前声明它,并说it is type but used as variable,如果我直接传递给构造函数中的函数。

public UseValues(IclientGroup clientgroup)
{
     //error: IClientGroup is type but used as variable
}

如何使用已初始化值的客户端组?我无法从dll中看到确切的实现。

初始化接口中的值

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

上面的代码将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
    }
}

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