C#For each语句不能操作';列表';因为没有';获取枚举数';,如何修复此代码?
我有一些方法和类,但它会抛出一个错误,因为 “列表”不包含“GetEnumerator”的公共实例定义 下面的第一种方法显示了所使用的foreach:C#For each语句不能操作';列表';因为没有';获取枚举数';,如何修复此代码?,c#,visual-studio,serialization,document,opencvsharp,C#,Visual Studio,Serialization,Document,Opencvsharp,我有一些方法和类,但它会抛出一个错误,因为 “列表”不包含“GetEnumerator”的公共实例定义 下面的第一种方法显示了所使用的foreach: public static string Execute(string imagePath) { var img = Cv2.ImRead(imagePath); List<string> result = new List<string>(); List<KeyValuePair<st
public static string Execute(string imagePath)
{
var img = Cv2.ImRead(imagePath);
List<string> result = new List<string>();
List<KeyValuePair<string, double>> featureScores = new List<KeyValuePair<string, double>>();
List features = GetFeatures();
AKAZE kaze = AKAZE.Create();
Mat desc = new Mat();
kaze.DetectAndCompute(img, null, out KeyPoint[] keyPoints, desc);
foreach (var feature in features)
{
var train = new Mat(feature.Rows, feature.Cols, feature.ImgType, feature.ImgData);
var matches = GetMatches(desc, train);
var score = GetScore(matches);
if (score == double.MaxValue)
continue;
featureScores.Add(new KeyValuePair<string, double>(feature.Category, score));
}
return featureScores.Count > 0 ? featureScores.OrderByDescending(x => (x.Value)).ToList()[0].Key : "Unknown";
}
以及存储的信息
private static ExtractedFeature ExtractFeatures(string image)
{
Mat img = Cv2.ImRead(image);
string category = Path.GetFileNameWithoutExtension(image).Split('.')[0];
Console.WriteLine(category);
OpenCvSharp.AKAZE kaze = OpenCvSharp.AKAZE.Create();
Mat desc = new Mat();
kaze.DetectAndCompute(img, null, out KeyPoint[] keyPoints, desc);
ExtractedFeature result = new ExtractedFeature()
{
ImgData = desc.ToBytes(),
Cols = desc.Cols,
Rows = desc.Rows,
ImgType = desc.Type(),
Category = category
};
return result;
}
GetFeatures()返回一个列表
而不是列表
您应该为
List
指定一个类型,如string或int
,该类型对应于列表包含的内容您试图将对象强制转换为类型列表,但您只能在您的基础类型确实是列表或其实现了列表实现的所有接口时执行此操作
因此,我建议首先用
obj is List
显然不是
因此,我建议您必须尝试将FileStream反序列化为类型化对象,或者尝试推断方法的正确输出类型反序列化您可以将
List
的代码添加到问题中吗?为什么要混合使用泛型和非泛型?在.NET中没有非泛型的List
类型。这是从哪里来的?请提供一个。您的代码无法编译,因为1)找不到类型或命名空间名称“ExtractedFeature”;2)使用泛型类型“System.Collections.generic.List”需要1个类型参数。请参阅:。我怀疑您从中的注释中错误复制了一些格式错误的代码,需要返回一个列表
而不是一个不存在的列表
。但是如果没有一个好消息,我们真的帮不了你。请参阅:。
obj is List