是否可以在 C# 中将变量用作默认参数?
Is it possible to use a variable as a default parameter in C#?
我正在使用 "BrowserSession" 并添加了 2 个变量来跟踪用户名和密码以登录网站。
private string _username;
private string _password;
public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)
{
if ((string.IsNullOrEmpty(username)) || (string.IsNullOrEmpty(password))
{
//throw Exception("Password Or Username Not Set");
}
BrowserLogin(username, password);//In case user is not logged in
StoredAccountInfo retdata = new StoredAccountInfo();
//populate retdata
return retdata;
我还添加了这个设置 _username 和 _password 变量的函数
public void BrowserLogin(string username, string password)
{
if (Cookies == null)
{
//Do Site Login Here If not Logged In
Get(constants.BaseUrl);
FormElements["UserName"] = username;
FormElements["PassWord"] = password;
Post(constants.loginUrl);
_username = username;
_password = password;
}
}
但是
public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)
给我语法错误
“default parameter value for 'username' must be a compile-time constant
”
有人知道解决方法或解决方案吗?我不想每次调用 RefreshAccountData 方法时都传递用户名和密码,但是如果它在第一次浏览器登录之前被调用,我希望能够从中设置用户名和密码变量。
我可能只是完全错过了一个非常简单的方法来做到这一点。但是我好像想不出来了。
您可以默认为 null 并在传递 null 时使用这些变量:
public StoredAccountInfo RefreshAccountData(string username = null, string password = null)
{
username = username ?? _username;
password = password ?? _password;
if ((string.IsNullOrEmpty(username)) ||
(string.IsNullOrEmpty(password))
{
//throw Exception("Password Or Username Not Set");
}
或者您可以提供第二种方法,它不采用这些参数并将变量传递给您的主方法,这样您就可以在调用者实际尝试传递恰好为 null 的用户名或密码时捕捉到抛出错误。
public StoredAccountInfo RefreshAccountData()
{
RefreshAccountData(_username, _password);
}
您不能将变量用作可选参数。您必须声明该方法的重载。好难过! :(
public StoredAccountInfo RefreshAccountData() {
return RefreshAccountData (_username, _password);
}
不幸的是,没有办法为此使用可选参数。
让我解释一下为什么你不能。可选参数值被放入编译代码中。所以如果你改变变量的值,编译后的代码不会改变,对吧?长话短说,编译后的代码无法检测到变量的值已更改。
重载方法:
public void BrowserLogin(string username, string password)
{
...
}
public void BrowserLogin(string username)
{
BrowserLogin(username, _password);
}
public void BrowserLogin()
{
BrowserLogin(_username, _password);
}
参数对象:
public class LoginParams
{
public string UserName { get; set; }
public string Password { get; set; }
}
public void BrowserLogin(LoginParams aParams)
{
if (aParams.UserName == null)
<use this._username>
if (aParams.Password == null)
<use this._password>
...
}
BrowserLogin(new LoginParams());
BrowserLogin(new LoginParams() { UserName = username });
BrowserLogin(new LoginParams() { UserName = username, Password = password });
我正在使用 "BrowserSession" 并添加了 2 个变量来跟踪用户名和密码以登录网站。
private string _username;
private string _password;
public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)
{
if ((string.IsNullOrEmpty(username)) || (string.IsNullOrEmpty(password))
{
//throw Exception("Password Or Username Not Set");
}
BrowserLogin(username, password);//In case user is not logged in
StoredAccountInfo retdata = new StoredAccountInfo();
//populate retdata
return retdata;
我还添加了这个设置 _username 和 _password 变量的函数
public void BrowserLogin(string username, string password)
{
if (Cookies == null)
{
//Do Site Login Here If not Logged In
Get(constants.BaseUrl);
FormElements["UserName"] = username;
FormElements["PassWord"] = password;
Post(constants.loginUrl);
_username = username;
_password = password;
}
}
但是
public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)
给我语法错误
“default parameter value for 'username' must be a compile-time constant
”
有人知道解决方法或解决方案吗?我不想每次调用 RefreshAccountData 方法时都传递用户名和密码,但是如果它在第一次浏览器登录之前被调用,我希望能够从中设置用户名和密码变量。
我可能只是完全错过了一个非常简单的方法来做到这一点。但是我好像想不出来了。
您可以默认为 null 并在传递 null 时使用这些变量:
public StoredAccountInfo RefreshAccountData(string username = null, string password = null)
{
username = username ?? _username;
password = password ?? _password;
if ((string.IsNullOrEmpty(username)) ||
(string.IsNullOrEmpty(password))
{
//throw Exception("Password Or Username Not Set");
}
或者您可以提供第二种方法,它不采用这些参数并将变量传递给您的主方法,这样您就可以在调用者实际尝试传递恰好为 null 的用户名或密码时捕捉到抛出错误。
public StoredAccountInfo RefreshAccountData()
{
RefreshAccountData(_username, _password);
}
您不能将变量用作可选参数。您必须声明该方法的重载。好难过! :(
public StoredAccountInfo RefreshAccountData() {
return RefreshAccountData (_username, _password);
}
不幸的是,没有办法为此使用可选参数。
让我解释一下为什么你不能。可选参数值被放入编译代码中。所以如果你改变变量的值,编译后的代码不会改变,对吧?长话短说,编译后的代码无法检测到变量的值已更改。
重载方法:
public void BrowserLogin(string username, string password)
{
...
}
public void BrowserLogin(string username)
{
BrowserLogin(username, _password);
}
public void BrowserLogin()
{
BrowserLogin(_username, _password);
}
参数对象:
public class LoginParams
{
public string UserName { get; set; }
public string Password { get; set; }
}
public void BrowserLogin(LoginParams aParams)
{
if (aParams.UserName == null)
<use this._username>
if (aParams.Password == null)
<use this._password>
...
}
BrowserLogin(new LoginParams());
BrowserLogin(new LoginParams() { UserName = username });
BrowserLogin(new LoginParams() { UserName = username, Password = password });