c# - 使用自定义属性跳过body方法

标签 c# methods custom-attributes microsoft-test-manager

我有以下功能:

public void Test(string testString)
{
     //Do Stuff
}

在我的代码中的某些地方,我必须重复检查参数是否为空字符串/null/空格以跳过 body 方法。到目前为止,我通常采用的方法如下:

public void Test(string testString)
{
     if(!string.IsNullOrWhiteSpace(testString))
     {
         //Do Stuff only if string has text in it.
     }
}

或者

public void Test(string testString)
{
     if(string.IsNullOrWhiteSpace(testString)) { return; }
     //Do Stuff only if string has text in it.
}

有没有办法创建一个自定义属性来检查函数的参数是否为空等,以跳过该方法?我对自定义属性有一些经验(基本的东西),但我无法找到一种方法使属性跳过方法体。

实现的理想最终产品如下:

[SkipIfEmptyParameter]
public void Test(string testString)
{
     //Do Stuff only if string has text in it.
}

当然,如果属性实现不可能,任何有助于最小化重复代码的建议都是受欢迎的。

编辑:我想要解决的问题的示例。

我有以下方法。我从 Microsoft 测试管理器获得了我们的测试场景期望的一些参数(值应该是什么)。有一个 SharedStep 实现可以断言用户的信息:

public void AssertUser(UserDTO expectedUserInfo)
{
    VerifyUserName(expectedUserInfo.name);
    VerifyUserSurname(expectedUserInfo.surname);
    VerifyUserAge(expectedUserInfo.age);
    VerifyUserHeight(expectedUserInfo.height);
}

private void VerifyUserName(string name)
{
     //If the string parameter is empty, means the MTM scenario does not
     //want to validate the user's name at this point, so skip the
     //verification below.
     if(string.IsNullOrWhiteSpace(testString)) { return; }

     //Do Stuff only if string has text in it.
}

private void VerifyUserSurname(string surname)
{
     //If the string parameter is empty, means the MTM scenario does not
     //want to validate the user's surname at this point, so skip the
     //verification below.
     if(string.IsNullOrWhiteSpace(testString)) { return; }
     //Do Stuff only if string has text in it.
}

private void VerifyUserAge(string age)
{
     //If the string parameter is empty, means the MTM scenario does not
     //want to validate the user's age at this point, so skip the
     //verification below.
     if(string.IsNullOrWhiteSpace(testString)) { return; }
     //Do Stuff only if string has text in it.
}

private void VerifyUserHeight(string height)
{
     //If the string parameter is empty, means the MTM scenario does not
     //want to validate the user's height at this point, so skip the
     //verification below.
     if(string.IsNullOrWhiteSpace(testString)) { return; }
     //Do Stuff only if string has text in it.
}

“Do Stuff”包含处理 WebElements 的 Selenium 实现,并且可能非常耗时,因此如果我们不想验证该特定值,我们只需跳过整个方法。

现在,在为 Microsoft 测试管理器创建场景时,共享步骤允许测试人员决定将验证页面的哪些元素。如果某些参数为空,则代码将跳过这些 block 并进行用户想要的验证(仍然,该实现适用于用户拥有的每个信息,但我们只需为我们想要测试的每个参数分配值) ,并且每个没有值的参数都会跳过其方法体)。

问题是,如果我想改变跳过该方法的条件,我就必须去每个方法并手动更改 IF 语句。因此,为什么我认为为每个验证信息的方法都有一个属性可能是个好主意。

附注我说的是数百种从一开始就有 IF 实现的方法。

最佳答案

据我所知,可以使用属性来完成此操作的唯一方法是使用像 post Sharp 和方法 interception 这样的产品进行面向方面的编程。 。或者,如果方法是在接口(interface)中定义的,也可以通过使用 RealProxy 来完成。但似乎有点矫枉过正。

关于c# - 使用自定义属性跳过body方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49816886/

相关文章:

c# - 子类中的构造函数顺序

java - 创建实例后如何强制调用方法

php - Magento 2.3.2 - 单击“Pay with paypal express checkout”时删除的自定义属性地址

具有自定义属性的 Android 自定义 View

node.js - Express.js : Object #<IncomingMessage> has no method 'call'

c# - 在 SwashBuckle 中使用 IOperationFilter 删除路由

c# - 我的自定义模型绑定(bind)程序的 AttemptedValue 包含键和值

c# - 使用引用值作为参数,带或不带 "ref"?

c# - 字符串比较问题

php - 静态方法与非静态方法