Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义对象迭代泛型列表时出现问题_C# - Fatal编程技术网

C# 使用自定义对象迭代泛型列表时出现问题

C# 使用自定义对象迭代泛型列表时出现问题,c#,C#,代码如下: ProductList products = xxx.GetCarProducts(productCount); List<CarImageList> imageList = new List<CarImageList>(); foreach(Product p in products) { string imageTag = HttpUtility.HtmlEncode(string.Format(@"&l

代码如下:

    ProductList products = xxx.GetCarProducts(productCount);
    List<CarImageList> imageList = new List<CarImageList>();


    foreach(Product p in products)
    {
        string imageTag = HttpUtility.HtmlEncode(string.Format(@"<img src=""{0}"" alt="""">", ImageUrl(p.Image, false)));

        imageList.Add(new CarImageList{ImageTag = imageTag});
        i++;
    }
ProductList products=xxx.GetCarProducts(productCount);
List imageList=新列表();
foreach(产品中的产品p)
{
字符串imageTag=HttpUtility.HtmlEncode(string.Format(@“”,ImageUrl(p.Image,false));
添加(新的CarImageList{ImageTag=ImageTag});
i++;
}
在封面下,ProductList的定义如下:

public class ProductList : List<Product>
{
    static Random rand = new Random();

    public ProductList Shuffle()
    {
        ProductList list = new ProductList();
        list.AddRange(this);

        for (int i = 0; i < list.Count; i++)
        {
            int r = rand.Next(list.Count - 1);
            Product swap = list[i];
            list[i] = list[r];
            list[r] = swap;
        }

        return list;
    }

    public Product FindById(int id)
    {
        foreach (Product p in this)
            if (p.Id == id) return p;

        return null;
    }

    public Product GetRandom()
    {
        int r = rand.Next(this.Count - 1);
        return this[r];
    }
}
公共类产品列表:列表
{
静态随机兰德=新随机();
公共产品列表洗牌()
{
ProductList=新产品列表();
list.AddRange(本);
for(int i=0;i
那么,当我试图通过ProductList实例foreach时,为什么会出现错误呢


我得到的错误是无法将类型“xxx.Product”转换为“Product”。但是,如果ProductList真的是List,为什么它在转换方面会有问题呢?

在我看来,第一个示例中的
产品
与第二个示例中的
产品
类型不同。您确定已整理好名称空间吗?

听起来您有两个不同的类,都称为“产品”,但在不同的名称空间/类中。(公共)嵌套类是个坏主意,因为它们会加剧这类问题。。。我会检查您对
产品的两个引用是否实际上是同一个类

编辑:

void Main()
{
    ProductList products = new ProductList();
    products.Add(new Product("foo"));
    products.Add(new Product("bar"));

    foreach(Product p in products)
    {
        Console.WriteLine(p.Name);
    }
}

// Define other methods and classes here
public class ProductList : List<Product>
{ /* [snip] the other stuff is irrelevant */ }

public class Product 
{
    public Product(string name)
    { Name = name; }
    public string Name;
}
void Main()
{
ProductList products=新产品列表();
产品。添加(新产品(“foo”);
产品。添加(新产品(“酒吧”);
foreach(产品中的产品p)
{
Console.WriteLine(p.Name);
}
}
//在此处定义其他方法和类
公共类产品列表:列表
{/*[snip]其他东西是不相关的*/}
公共类产品
{
公共产品(字符串名称)
{Name=Name;}
公共字符串名称;
}

您可能在不同的名称空间中有两种不同的
产品
类型,或者您可能缺少使用
语句的


另一方面,自定义集合类(如
ProductList
)应该继承自
可能返回对
List
的引用,该引用的定义比
ProductList
类的定义少,这意味着在GetCarProducts方法中,您可能执行new
List
而不是
new ProductList()


如果您能发布
GetCarProducts

的正文,那么它们的名称空间相同,因此没有问题。确切的错误消息是什么?它是在运行时还是在编译时?运行时:无法将类型“xxnamespace.ProductList”转换为“System.Collections.Generic.List”,我将鼠标移到ProductList类定义中的Product上,位于:List之后,它确实与ProductList所在的命名空间相同,并且与Product.cs中的命名空间相同对于同一类,上面的代码非常适合我。。。你确定没有名称空间冲突吗?除了公共嵌套类之外,你认为应该怎么做?我绝对肯定,在我的web项目中使用的所有程序集中,有一个而且只有一个定义包含“公共类产品”。我不明白的是,要解决这个问题,我必须在“产品”前面加上一个前缀使用名称空间,即使我用using语句包含了该名称空间,并且我100%确定类产品只有一个定义,而且它肯定在该名称空间中?您正在使用LinqToSQL吗?如果是这样,则错误可能出现在GetCarProducts中,但在您尝试迭代集合之前不会显示。GetCarProducts返回ProductList类型。不,不使用LINQtried,但没有运气,表示无法转换ProductList products=GetCarProducts(productCount);列出产品=产品;您可以在错误列表中按Ctrl+C复制错误消息。能否粘贴错误的确切文本?运行时:无法将类型“xxnamespace.ProductList”转换为“System.Collections.Generic.List”