我有以下代码:
public interface IParameter
{
ParameterName Name { get; set; }
}
public interface IParameter<T> : IParameter
{
T Value { get; set; }
T LLimit { get; }
T RLimit { get; }
}
public class IntegerParameter : IParameter<int>
{
public ParameterName Name { get; set; }
public int Value { get; set; }
public int LLimit { get; private set; }
public int RLimit { get; private set;}
public IntegerParameter(ParameterName name, int value, int llimit, int rlimit)
{
Name = name;
Value = value;
LLimit = llimit;
RLimit = rlimit;
}
}
public class DoubleParameter : IParameter<double>
{
public ParameterName Name { get; set; }
public double Value { get; set; }
public double LLimit { get; private set; }
public double RLimit { get; private set; }
public DoubleParameter(ParameterName name, double value, double llimit, double rlimit)
{
Name = name;
Value = value;
LLimit = llimit;
RLimit = rlimit;
}
}
// ...
该代码是我正在整理的物理项目的一部分。现在这是我的问题。我希望能够根据 ParameterName 在字典中添加参数,但仍然能够调用 Value。
var A = new IntegerParameter(ParameterName.X1, 10, 0, 100);
var B = new DoubleParameter(ParameterName.X2, 56.4, 0, 78.6);
var set = new Dictionary<ParameterName,IParameter>();
set.Add(A.Name, A);
set.Add(B.Name, B);
set[A.Name].Value; // I cannot do it since the Value is expressed only in the IParameter<T> part