我在尝试将此列表传递给另一个类的应用程序中。我不断收到错误
Cannot implicitly convert type 'System.Collections.Generic.List<eko_app.LoginForm.Business>' to 'System.Collections.Generic.List<eko_app.GlobalClass.Business>'
该列表是从Json生成的。
登录课程
private void Connect()
{
try
{
serverUrl = "https://eko-app.com/Users/login/username:" + usernameEntered + "/password:" + passwordEntered + ".json";
var w = new WebClient();
var jsonData = string.Empty;
// make the login api call
jsonData = w.DownloadString(serverUrl);
if (!string.IsNullOrEmpty(jsonData))
{
// deserialize the json to c# .net
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);
// Get the sessionId
GlobalClass.SessionId = string.Empty;
GlobalClass.SessionId = response.response.SessionId;
if (GlobalClass.SessionId != null)
{
var businesses = response.response.Businesses;
GlobalClass.Username = "";
GlobalClass.Username = usernameEntered;
GlobalClass.Businesses = null;
GlobalClass.Businesses = businesses; **<-----error thrown here**
HomeForm homeForm = new HomeForm();
this.Hide();
homeForm.Show();
}
else
{
Console.WriteLine("Login failed");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private class Business
{
public string businessID { get; set; }
public string businessName { get; set; }
}
private class Response
{
[JsonProperty("sessionId")]
public string SessionId { get; set; }
[JsonProperty("businesses")]
public List<Business> Businesses { get; set; }
}
private class Messages
{
public string sessionId { get; set; }
public List<Business> businesses { get; set; }
}
private class RootObject
{
public Response response { get; set; }
public Messages messages { get; set; }
}
我还有另一个班:全球班
namespace eko_app
{
static class GlobalClass
{
...some code
public class Business
{
private string businessID { get; set; }
private string businessName { get; set; }
}
private static List<Business> businesses = null;
public static List<Business> Businesses
{
get { return businesses; }
set { businesses = value; }
}
}
}
我究竟做错了什么?该错误在
GlobalClass.Businesses = businesses;
的第一类中引发 最佳答案
看来您有两个称为Business
的相同类-一个嵌套在GlobalClass
内,另一个嵌套在LoginForm
内。
尽管两个类看起来相同,但是它们不能彼此分配。而且,基于这些类的集合也不能彼此分配。
考虑删除这些类之一(例如LoginForm
中的私有类),并将其所有用途替换为公共的GlobalClass.Business
。