使用通用类型传递给类时,修改IEnumerable <T>或ICollection <T>

问题描述

使用泛型类型时,在修改IEnumerable 值时遇到麻烦。
这是一些从以下代码开始的代码

public class Parameterarray<TEnumerable,TType> : IParameterarray<TEnumerable,TType> 
    where TEnumerable : ICollection<TType>    // Note that I have tried with IEnumerable<TType> instead of ICollection<TType>,but did not really help
{
    public Parameterarray(TEnumerable value,TType minValue,TType maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
        Value = value;
     }

    private TEnumerable _value;
    public TEnumerable Value
    {
        get => _value;
        set
        {
           ICollection<TType> checkedValue;

           // Here I want to check the range before setting the value
           // Note that Value can be of type int[],float[],bool[],byte[]
           
           // I have tried many work around,but for some reasons,I am never able to update _value
           for (int idx = 0; idx < value.Count(); idx++)
           {
               // Backup value after range limiting
               checkedValue.Add(CheckRange(value.ElementAt(idx)));
            }

            // Would like to save these new values in _value,but not sure how to
            _value = checkedValue as TEnumerable;  // Or (TEnumerable)checkedValue;     // !!! Breaking here at runtime - Cannot convert from one type to the other
        }
    }

    public TType CheckRange(TType value)
    {
        TType retValue = value;

        if (value != null)
        {
            // Restrict range to Min/Max
            if (Comparer<TType>.Default.Compare(MaxValue,value) < 0)
                retValue = MaxValue;
            else if (Comparer<TType>.Default.Compare(MinValue,value) > 0)
                retValue = MinValue;
        }
        else
        {
            throw new ArgumentOutOfRangeException($"Value cannot be null: Range is {MinValue} to {MaxValue}");
        }

        return retValue;
    }
}

以某种方式,我无法将ICollection备份到我的_value(这是一个TEnumerable)中。但是,考虑到我已宣布TEnumerable源自ICollection(即TEnumerable:ICollection),我还不太了解这个原因

我尝试了多种方法,但是它们都导致无法将我的结果分配给_value的同一问题。

任何建议都会受到欢迎。
也许我还需要以下东西:其中TEnumerable:ICollection

编辑和解决方

正如@nvoigt所建议的那样,我对我的类进行了重新设置,以使其不使用TEnumerable作为输入。这种额外的抽象层增加了额外的复杂性,使所有工作都难以进行。
这是结果解决方案:

public class Parameterarray<TType> : ParameterBase,IParameterarray<TType> 
{
    public Parameterarray(IEnumerable<TType> value,TType maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
        Value = value;
     }

    private IEnumerable<TType> _value;
    public IEnumerable<TType> Value
    {
        get => _value;
        set
        {
           IList<TType> checkedValue = new List<TType>();
           
           for (int idx = 0; idx < value.Count(); idx++)
           {
               // Range limiting
               checkedValue.Add(CheckRange(value.ElementAt(idx)));
            }

            // Back up the updated value
            _value = checkedValue.ToArray();
        }
    }

    public TType CheckRange(TType value)
    {
        // Unchanged
        ...
    }
}

解决方法


    for i := range tempPkg.cars {
        carIndex := tempPkg.cars[i].cNum
        car,_ := s.QueryCar(ctx,carIndex)
        car.Owner = newOwner
        car.Status = "Handed Over"
        carAsBytes,_ := json.Marshal(*car)
        err := ctx.GetStub().PutState(carIndex,carAsBytes)
        if err != nil {
            return fmt.Errorf("Error in Puting state to data base%s",err.Error())
        }
        tempPkg.cars[i].Owner = newOwner
        tempPkg.cars[i].Status = "Handed Over"
    }

让我们看看。假设我将ckeckedValue as TEnumerable 设为TEnumerable,而int[]ckeckedValue,那么您的编译器是正确的……您不能使用ICollection<int>运算符从中创建数组具有相同基本类型的as。无论是泛型还是无效,这都不起作用。

我真的不能提出一个好的方法。如果是我,我将废弃通用的可枚举部分,并使其成为ICollectionList<>并进行处理。如果您有明确的约束要求需要该部分是通用的,则可能应该向其发布问题,以便我们根据这些约束提出不同的解决方案。