You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中ColorSpecification类报错:非泛型类型不可使用类型实参

解决C#中ColorSpecification实例化的泛型错误

你编写的代码中,ColorSpecification是一个非泛型类,它已经明确实现了ISpecification<Product>接口,因此实例化时不需要额外指定<Product>类型参数,这就是报错的核心原因。

原报错代码片段

// 错误写法:给非泛型类添加了类型实参
var cs = new ColorSpecification<Product>(Color.Green);

解决方案一:直接修正实例化代码

去掉<Product>类型参数,直接实例化ColorSpecification即可:

public static void Main(string[] args)
{   
    var bf = new BetterFilter();
    // 修正:移除多余的<Product>类型参数
    var cs = new ColorSpecification(Color.Green);

    foreach (var p in bf.Filter(products, cs))
    {
        Console.WriteLine($"Green products(new) {p.Name}");
    }
}

解决方案二:将ColorSpecification改为泛型类(提升灵活性)

如果希望这个规格类能适配其他拥有Color属性的实体类型,可以把ColorSpecification改成泛型类,同时通过接口约束限定泛型类型必须包含Color属性:

// 泛型版本的ColorSpecification
public class ColorSpecification<T> : ISpecification<T> where T : IHasColor
{
    private Color color;

    public ColorSpecification(Color color)
    {
        this.color = color;
    }

    public bool IsSatisfied(T item)
    {
        return item.Color == color;
    }
}

// 定义约束接口,确保泛型类型具备Color属性
public interface IHasColor
{
    Color Color { get; }
}

// 让Product类实现IHasColor接口
public class Product : IHasColor
{
    public string Name { get; set; }
    public Color Color { get; set; }
}

此时实例化就可以正确使用<Product>类型参数:

var cs = new ColorSpecification<Product>(Color.Green);

完整修正后的代码(方案一版本)

using System;
using System.Collections.Generic;

public enum Color { Red, Green, Blue }

public class Product
{
    public string Name { get; set; }
    public Color Color { get; set; }
}

public interface ISpecification<T>
{
    bool IsSatisfied(T t);
}

public interface IFilter<T>
{
    IEnumerable<T> Filter(IEnumerable<T> items, ISpecification<T> specification);
 }

public class ColorSpecification : ISpecification<Product>
{
    private Color color;

    public ColorSpecification(Color color)
    {
        this.color = color;
    }

    public bool IsSatisfied(Product product)
    {
        return product.Color == color;
    }
}

/// <summary>
/// new way, implements open close design pattern
/// </summary>
public class BetterFilter : IFilter<Product>
{
    public IEnumerable<Product> Filter(IEnumerable<Product> items, ISpecification<Product> specification)
    {
        foreach (var i in items)
        {
            if (specification.IsSatisfied(i)) 
                yield return i;
        }
    }
}
    
public class Program
{
    public static void Main(string[] args)
    {   
        // 模拟测试用的产品集合
        var products = new List<Product>
        {
            new Product { Name = "Green Apple", Color = Color.Green },
            new Product { Name = "Red Cherry", Color = Color.Red },
            new Product { Name = "Green Grape", Color = Color.Green }
        };
        
        var bf = new BetterFilter();
        var cs = new ColorSpecification(Color.Green);

        foreach (var p in bf.Filter(products, cs))
        {
            Console.WriteLine($"Green products(new) {p.Name}");
        }
    }
}

内容的提问来源于stack exchange,提问作者Murat Ishenbaev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:58:22