Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何在一次调用时将参数传递给不同的方法,避免每次都传递相同的参数?_C# 4.0_Tdd - Fatal编程技术网

C# 4.0 如何在一次调用时将参数传递给不同的方法,避免每次都传递相同的参数?

C# 4.0 如何在一次调用时将参数传递给不同的方法,避免每次都传递相同的参数?,c#-4.0,tdd,C# 4.0,Tdd,我有TDD Sql数据库项目设置和调用Assert方法定义 Assert.AreEqual(someObject.GetValue<int>(Constants.SomeValue), anotherObject.GetValue<int>(dt, Constants.SomeValue), "some message") Assert.AreEqual(someObject.GetValue(Constants.SomeValue), 另一个object.Ge

我有TDD Sql数据库项目设置和调用
Assert
方法定义

Assert.AreEqual(someObject.GetValue<int>(Constants.SomeValue),
    anotherObject.GetValue<int>(dt, Constants.SomeValue), "some message")
Assert.AreEqual(someObject.GetValue(Constants.SomeValue),
另一个object.GetValue(dt,Constants.SomeValue),“somemessage”)

问题:我需要为每个调用传递
常量.SomeValue
GetValue
方法。我们如何才能消除这种情况。我们可以在外部方法(即
Assert
)中定义它,以便内部引用的方法获取参数值。我可以在string类上创建一个扩展方法,但我需要传递所有对象并构建assert方法,然后从扩展方法返回。关于这一点有什么想法或简单的方法吗。

不是100%确定您得到了什么

看起来您倾向于使用新的辅助方法进行断言。将公共部分隔离到helper方法中,并仅将不同调用的部分作为参数传递给该方法

public void PleaseNameMeBetter<T>(Constants columnName)
{
   Assert.AreEqual(someObject.GetValue<T>(columnName),
                   anotherObject.GetValue<T>(dt, columnName), 
                   "some message")
}
public void PleaseNameBetter(常量列名称)
{
Assert.AreEqual(someObject.GetValue(columnName),
另一个Object.GetValue(dt,columnName),
“一些信息”)
}

不清楚您想要实现什么目标。请提供一个您希望代码看起来像什么的示例。让我详细说明一下,我通过调用GetValue(string)方法将对象参数传递给Assert方法。GetValue参数是类中定义的常量字符串,这些字符串是硬编码的数据表列名。我正在断言来自testContext datarow的值和来自source的datable(TDD)。您可以看到,每当调用GetValue()方法时,都需要传递Constant.SomeValue。我们是否可以在某个地方(比如在Assert或Anyymous中)定义它,以便在其中引用它。再次:请按照您希望的方式提供代码。类似这样的内容:-AssertHelper.Execute((Constant.Name)=>Assert.AreEqual(此处应使用this.GetValue(---Constants.Name),此处应使用ds.GetValue(---Constants.Name),Constants.Messages.PortfolioName));我更喜欢类型安全常量,而不是字符串。。不那么脆弱。将每个常量定义为
publicstaticreadonly ColumnX=newconstants()