简体   繁体   English

在C#中使用setter作为参数? (也许与代表?)

[英]Using a setter as a parameter in C#? (Maybe with delegate?)

We're working on an API for some hardware and I'm trying to write some tests for it in C#. 我们正在为一些硬件开发API,我正在尝试用C#编写一些测试。 try-catch blocks for repetitive tasks were making my code bloated and repetitive so for getters I was able to wrap like this: 用于重复性任务的try-catch块使我的代码变得臃肿和重复,所以对于getter我能够像这样包装:

TestGetter(Func<int> method, double expectedVal)
{
    int testMe = 0;
    try
    {
        testMe = method();
        PassIfTrue(testMe == expectedVal);
    }
    catch (Exception e)
    {
         Fail(e.Message);
    }
}

So I query the hardware for some known value and compare. 所以我查询硬件的某些已知值并进行比较。 I can call with: 我可以打电话:

TestGetter( () => myAPI.Firmware.Version, 24); //Or whatever.

Which works quite well. 哪个效果很好。 But I'm not sure how to do the same with setters. 但我不确定如何对setter做同样的事情。 Ie to ensure that the API actually set a value (and doesn't timeout or whatever when I try to set). 即确保API实际设置一个值(并且在我尝试设置时不会超时或任何其他情况)。 I'd like to pass the setter to the test method and invoke it in there. 我想将setter传递给test方法并在那里调用它。

Bonus question: Is there a way to do this with a generic type? 奖金问题:有没有办法用通用类型做到这一点? There are some custom types defined in the API and I'm not sure of a good way to write these test wrappers for them without writing a new overloaded method for every type. API中定义了一些自定义类型,我不确定为它们编写这些测试包装器的好方法,而无需为每种类型编写新的重载方法。 Thanks for any and all help! 感谢您的帮助!

You could pass the getter and the setter to the function: 你可以将getter和setter传递给函数:

void TestSetter<T>(Func<T> getter, Action<T> setter, T value)
{
    try
    {
        setter(value);
        PassIfTrue(EqualityComparer<T>.Default.Equals(getter(), value));
    }
    catch (Exception e)
    {
         Fail(e.Message);
    }       
}

This sets the value, then gets it and compares to the value passed to the setter. 这将设置值,然后获取它并与传递给setter的值进行比较。

You'd have to call it like: 你必须把它称为:

TestSetter(() => myAPI.Firmware.Version, v => myAPI.Firmware.Version = v, 24);

You can make them generic like Reeds, but you need to use different comparison methods: 您可以像Reeds一样使它们具有通用性,但您需要使用不同的比较方法:

    public static void TestGetter<T>(Func<T> method, T expectedVal)
    {       
        try
        {
            T actual = method();
            PassIfTrue(expectedVal.Equals(actual));
        }
        catch (Exception ex)
        {
            Fail(ex.Message);
        }
    }

    public static void TestSetter<T>(Action setMethod, Func<T> getMethod, T expectedVal)
    {
        try
        {
            setMethod();
            T actual = getMethod();
            PassIfTrue(expectedVal.Equals(actual));
        }
        catch (Exception ex)
        {
            Fail(ex.Message);
        }
    }

You could also pass in a Comparer action to test them if you don't think the Equals method would work for the expected types. 如果您认为Equals方法不适用于预期类型,您也可以传入Comparer操作来测试它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM