C# 将对象传递到多维数组的问题
尝试将坐标传递到多维数组时出现问题。引发的错误: 1在var newArray=item.To2dArray处;在GetInstructions方法中: 列表不包含TO2DRARY的定义,也不包含接受ist类型的第一个参数的可扩展方法TO2DRARY 2在公共部分类SiteMaster:将方法public static Coords[,]添加到2darray时的母版页此列表已添加 扩展方法必须在非泛型静态类中定义 我的列表结构C# 将对象传递到多维数组的问题,c#,asp.net,json,list,multidimensional-array,C#,Asp.net,Json,List,Multidimensional Array,尝试将坐标传递到多维数组时出现问题。引发的错误: 1在var newArray=item.To2dArray处;在GetInstructions方法中: 列表不包含TO2DRARY的定义,也不包含接受ist类型的第一个参数的可扩展方法TO2DRARY 2在公共部分类SiteMaster:将方法public static Coords[,]添加到2darray时的母版页此列表已添加 扩展方法必须在非泛型静态类中定义 我的列表结构 public class Route {
public class Route
{
public string status_message { get; set; }
public string route_geometry { get; set; }
public int status { get; set; }
//route_instructions is what I'm interested in
public List<List<object>> route_instructions { get; set; }
}
public class Coords
{
public int Lat { get; set; }
public int Lon { get; set; }
public Coords(string a, string b)
{
this.Lat = Convert.ToInt32(a);
this.Lon = Convert.ToInt32(b);
}
}
List<Coords> Coordinates = new List<Coords>();
如果方法是静态的并且位于静态类中,则只能将其用作扩展。 必须将2darray的方法移动到一个额外的静态类。 这就是以下信息的含义: 扩展方法必须在非泛型静态类中定义 另一个问题是,方法的签名不适合: 您正在遍历route.route\u指令,因此项的类型为List,但您的方法需要List
您是否已通过此步骤确定问题?
private void GetInstructions()
{
string strurltest = String.Format("https://developers.onemap.sg/privateapi/routingsvc/route?start="+
startLat+","+ startLon +"&end="+ destinationLat +","+ destinationLon+"&"+
"routeType="+ transportType + "&token="+token);
WebRequest requestObjGet = WebRequest.Create(strurltest);
requestObjGet.Method = "GET";
HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
string strresulttest = null;
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresulttest = sr.ReadToEnd();
sr.Close();
}
Route route = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
route_geometry = route.route_geometry;
//display route instructions
foreach (var item in route.route_instructions)
{
var newArray = item.To2dArray();
System.Diagnostics.Debug.WriteLine(item[3]);
TextBox3.Text = TextBox3.Text + Environment.NewLine + item[9];
}
}
public static Coords[,] To2dArray(this List<List<Coords>> list)
{
if (list.Count == 0 || list[0].Count == 0)
throw new ArgumentException("The list must have non-zero dimensions.");
var result = new Coords[list.Count, list[0].Count];
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list[i].Count; j++)
{
if (list[i].Count != list[0].Count)
throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
result[i, j] = list[i][j];
}
}
return result;
}
1.315396,103.764419
1.314333,103.763455
1.312906,103.766496
1.312109,103.772234
foreach (var item in route.route_instructions)
{
var newArray = item.To2dArray();
///...