gpt4 book ai didi

c# - 无法隐式转换类型 'System.Collections.Generic.List

转载 作者:行者123 更新时间:2023-11-30 20:00:54 25 4
gpt4 key购买 nike

我有这个应用程序,我试图将列表传递给另一个类。我不断收到错误

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 中的私有(private)类)并将其所有用途替换为公共(public)的 GlobalClass.Business

关于c# - 无法隐式转换类型 'System.Collections.Generic.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com