为什么不能使用static readonly object 属性?
Why can not use static readonly object property?
这是常量class:
public static class Constants
{
public const string USER_ID = "conduent";
public const string PASSWORD = "593becd1-02f6-46f0-bf34-25b393ad041b";
public static readonly Uri BASE_URI = new Uri("https://staging.test-476b.com");
public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");
}
这是用法
public class DanestreetHttp
{
private AuthToken authToken = null;
private readonly HttpClient httpClient = new HttpClient()
{
BaseAddress = Constants.BASE_URI
};
}
在屏幕截图上您可以看到错误,在我将 BaseAddress = Constants.BASE_URI
更改为 BaseAddress = new System.Uri("https://staging.test-476b.com")
后错误消失了。静态只读初始化有什么问题?
屏幕
PS。我目前的解决方案:BaseAddress = new Uri(Constants.BaseAddress)
问题是 Constants
中的 2 或 URI 无效,阻止此 class 初始化 属性。如果您替换
它应该可以工作
public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");
和
public static readonly Uri GET_TOKEN_URI = new Uri("http://api/session");
public static readonly Uri SEND_CASE_URI = new Uri("http://api/referral_request");
(或 https)
这是常量class:
public static class Constants
{
public const string USER_ID = "conduent";
public const string PASSWORD = "593becd1-02f6-46f0-bf34-25b393ad041b";
public static readonly Uri BASE_URI = new Uri("https://staging.test-476b.com");
public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");
}
这是用法
public class DanestreetHttp
{
private AuthToken authToken = null;
private readonly HttpClient httpClient = new HttpClient()
{
BaseAddress = Constants.BASE_URI
};
}
在屏幕截图上您可以看到错误,在我将 BaseAddress = Constants.BASE_URI
更改为 BaseAddress = new System.Uri("https://staging.test-476b.com")
后错误消失了。静态只读初始化有什么问题?
屏幕
PS。我目前的解决方案:BaseAddress = new Uri(Constants.BaseAddress)
问题是 Constants
中的 2 或 URI 无效,阻止此 class 初始化 属性。如果您替换
public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");
和
public static readonly Uri GET_TOKEN_URI = new Uri("http://api/session");
public static readonly Uri SEND_CASE_URI = new Uri("http://api/referral_request");
(或 https)