Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# 动态更改类属性参数_C#_Nunit - Fatal编程技术网

C# 动态更改类属性参数

C# 动态更改类属性参数,c#,nunit,C#,Nunit,如何在以下代码(针对TestFixture和TestConfiguration)中动态更改属性参数(在运行时): [ TestFixture(“设置1”), 测试配置(“http://spiratest,rin,rin,309242577, TestConfigurationAttribute.RunnerName.NUnit) ] 公共类SampleTestFixture { 受保护的静态int-testFixtureState=1; [TestFixtureSetUp] public voi

如何在以下代码(针对TestFixture和TestConfiguration)中动态更改属性参数(在运行时):

[
TestFixture(“设置1”),
测试配置(“http://spiratest,rin,rin,309242577,
TestConfigurationAttribute.RunnerName.NUnit)
]
公共类SampleTestFixture
{
受保护的静态int-testFixtureState=1;
[TestFixtureSetUp]
public void FixureInit()
{
//将状态设置为2
testFixtureState=2;
}
[设置]
公共void Init()
{
//无所事事
}
/// 
///断言失败的样本测试
/// 
[
测试,
测试用例(41681)
]
公共无效\u 01\u样本失败()
{
//验证状态
Assert.AreEqual(2,testFixtureState,“*实际错误*:状态未持久化”);
//失败断言
Assert.AreEqual(1,1,“按预期失败”);
}   
}
我需要在运行时更改TestFixture和TestConfiguration的属性参数。(不使用常量参数)


我怎样才能通过反射或注释来改变它?

我怀疑你想要什么是可能的。只要类、方法或任何成员上有属性,就可以随时使用
GetCustomAttributes
使用反射来处理这些属性

// find the fixtures
// ...
// provide the attributes and create the fixture
var newTestInstance = Activator.CreateInstance(typeof(SampleTestFixture), theParams)
当您使用这些属性调用成员时,您将属性中的信息提供给该成员或构造函数,但是已经使用这些属性提供的值调用了该成员(或构造函数)。因此,您想要的与此类似:

class MyClass {
    int MyInt;
    MyClass(int param)
    {
        MyInt = param;
    }
} 
因此,当您向构造函数提供参数时,其值绑定到
MyInt
。当您更改属性值时,NUnit不会以任何方式通知您,因此它不会重新创建您的测试,甚至不会修改已经存在的测试。两者都是有害的。首先,您将创建一个全新的测试。在第二种情况下,您必须确定哪些测试已经运行,并使用修改后的值重新运行这些测试


那么,当您在运行时更改
TestFixture
的值时会发生什么呢?Shell是否可以使用新值重新运行所有测试?还是只运行到目前为止还没有运行的

要更改测试名称吗?你为什么需要这个?我怀疑这是可能的,因为即使您可以更改值,NUnit也无法识别它们,因为测试已经创建并运行。我需要更改TestFixture中的参数,而不是“Setup 1”。我需要更改TestFixture中的参数,而不是“Setup 1”是将在运行时分配的字符串参数。那么,当您在运行时更改TestFixture的值时会发生什么?Shell是否可以使用新值重新运行所有测试?还是只运行到目前为止还没有运行的?你的问题没有多大意义。可能的重复?所有测试都应该使用新的值运行。答案是“这不可能”-但实际上他可能有一些更有意义的问题应该问,他认为他需要这样做的原因。不管是什么,动态更改代码可能不是答案。:-)
class MyClass {
    int MyInt;
    MyClass(int param)
    {
        MyInt = param;
    }
}