1

我正在尝试编写一个模拟不同频率信号的类。我希望该类将我正在测试的对象类型作为参数,以便测试该调用类中的方法。当我尝试将 myObject 类型转换为 myType 时出现错误。显然我做错了。我已经省略了这个类中很多多余的代码,以专注于我的问题的上下文。

           public MaxFinderTestChannel(TestSignals signal, Object ob)
    {
        myType = ob.GetType();
        myObject =  ob as myType; 

        switch (signal)
        {
            case TestSignals.Linear:
                {
                    constant = 1;
                    for (double dd = 0.0; dd <= 5 * Math.PI; dd += .001)
                    {
                        x = dd;
                        values.Add(x);
                        y = x + constant;
                        values.Add(y);
                    }
                }
                break;
            case TestSignals.NegativeSine:
                {
                    constant = -1;
                    for (double dd = 0.0; dd <= 5 * Math.PI; dd += .001)
                    {
                        x = dd;
                        values.Add(x);
                        y = Math.Sin(25 * x) + constant;
                        values.Add(y);
                    }
                }
                break;
            case TestSignals.Parabola:
                {
                    constant = 2;
                    for (double dd = (-Math.PI); dd <= Math.PI; dd += .1)
                    {
                        x = dd;
                        values.Add(x);
                        y = Math.Pow(x, 2) + constant;
                        values.Add(y);
                    }
                }
                break;
            case TestSignals.Sine:
                {
                    constant = 1;
                    for (double dd = 0.0; dd <= 5 * Math.PI; dd += .001)
                    {
                        x = dd;
                        values.Add(x);
                        y = Math.Sin(25 * x) + constant;
                        values.Add(y);
                    }
                }
                break;
            default:
                throw new Exception("Invalid signal type:" + this.Name.ToString());
        }
        pulseTrain = values.ToArray();
    }


    public virtual bool EnumDataPoints(bool eventsOnly, double startTime, double duration, Collection<ISampleProcessor> sampleProcessors)
    {
            for (double time = timestamp; !breakloop && time < endtime; time += .001)
            {
                time = Math.Round(time, 4);
                if (time == timestamp)
                {
                    if (myObject.Compute(time, ref value) == false) { breakloop = true; }
                    timestamp = Math.Round(pulseTrain[index], 4);
                    if (index < pulseTrain.Length - 1) { index++; }
                    value = Math.Round(pulseTrain[index], 4);
                    if (index < pulseTrain.Length - 1) { index++; }
                }
            }
        return !breakloop;
    }

问题是当我尝试遍历我的时间戳时出现编译错误。Compute() 方法由特定接口强制执行,并且有几个不同的具体类使用此方法。

4

1 回答 1

3

为什么不使用通用方法?:

public MaxFinderTestChannel<T>(TestSignals signal, T ob) where T : ITheInterface 
{
     myObject =  ob ;
    ...

ITheInterface 是定义 Compute() 方法的接口

于 2013-09-19T22:46:14.120 回答