C#中的类型转换错误

时间:2013-09-19 22:41:04

标签: c#-4.0 types casting

我试图写一个模拟不同频率信号的类。我希望类能够将我测试的对象类型作为参数,以便测试该调用类中的方法。当我尝试将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()方法由特定接口强制执行,并且几个不同的具体类使用此方法。

1 个答案:

答案 0 :(得分:3)

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

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

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