C# 设置类的字段';这是一个反省的例子
我有一个与反射相关的问题,我有一个类似的方法:C# 设置类的字段';这是一个反省的例子,c#,coded-ui-tests,C#,Coded Ui Tests,我有一个与反射相关的问题,我有一个类似的方法: [TestMethod()] public void steamAccess() { testRead = new TestRead(); SteamMap a = new SteamMap(); // Preparing the parameters of the CSV actions a.writeMessageParams.
[TestMethod()]
public void steamAccess()
{
testRead = new TestRead();
SteamMap a = new SteamMap();
// Preparing the parameters of the CSV actions
a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();
//Read and Execute the TestMethod
testRead.Read(a, TestContext);
}
这是一个CodedUITest
,SteamMap是一个类(UITestMap
)。
WriteMessageParams
是一个类,实际上真正的方法是WriteMessage
,但是这个类允许我重写WriteMessage
方法在测试中使用的字符串,我计划在Read
方法中使这部分代码更加动态:
a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();
我的问题发生在testRead中。阅读上下文如下:
当此方法运行时,我可以访问各个实例中的所有操作(a
,在我的例子中),如果它们必须使用a.writeMessageParams.UIItemEditText
上下文,我知道,如何获取信息不是问题,问题是如何使前面提到的代码像我尝试的那样动态运行:
/* I've done this because I know that each method that is supposed to end up with Params, for example a method called WriteMessage, it's class is called WriteMessageParams*/
public void Read(object obj, TestContext testContext)
{
//simplified code
//trying to access/get to the current instance's WriteMessageParam class
Object testObj = obj.GetType().GetMember(subMethod.Code + "Param");
//null
MessageBox.Show(testObj.GetType().ToString());
// trying to access the UIItemEditText field ( which is declared as public) and modify it accordingly
FieldInfo subMethodField = testObj.GetType().GetField("UIItemEditText");
subMethodField.SetValue(testObj,testContext.DataRow[subMethod.CsvColumn].ToString());
}
我仔细阅读了这篇文章,并尝试了一些方法
我的问题是我有一个实例的对象,我试图访问这个对象的类并修改该类的字段
我非常感谢你的帮助,
谢谢
编辑1:
这就是我试图访问的类的外观:
public partial class SteamMap
{ //simplified to what classes/methods interest me
public virtual writeMessageParams writeMessageParams
{
get
{
if ((this.mwriteMessageParams == null))
{
this.mwriteMessageParams = new writeMessageParams();
}
return this.mwriteMessageParams;
}
}
public class writeMessageParams
{
#region Fields
/// <summary>
/// Type 'test' in text box
/// </summary>
public string UIItemEditText = "test";
#endregion
}
}
如果我理解你的意思,你有一门课
public partial class SteamMap
{
private writeMessageParams mwriteMessageParams ;
public virtual writeMessageParams writeMessageParams1
{
get
{
if ((this.mwriteMessageParams == null))
{
this.mwriteMessageParams = new writeMessageParams();
}
return this.mwriteMessageParams;
}
}
public class writeMessageParams
{
public string UIItemEditText = "test";
}
}
(您的代码无法编译,因为您将writeMessageParams作为类和属性,因此我已将属性更改为writeMessageParams1)
您想要更改UIItemEditText,您可以这样做
public void UpdateUI(object obj, string newValue)
{
var property = obj.GetType().GetProperty("writeMessageParams1");
var writeMessageParams1 = property.GetValue(obj);
var uiFld = wp.GetType().GetField("UIItemEditText");
uiFld.SetValue(writeMessageParams1, newValue);
}
可以称之为
SteamMap sm = new SteamMap();
Write(sm, "Hello");
关键是对属性使用
.GetProperty
,对字段使用.GetField
。您到底想做什么?设置一个对象的字段?这就是我试图做的问题是我试图设置其字段的类嵌套在上面声明的SteamMap类中。我不知道如何达到它你已经节省了我从一天多的阅读msdn文件的这个问题。显然,它是通过直接搜索我想要的方法来工作的,现在我的CSV值读取是动态工作的。谢谢。
SteamMap sm = new SteamMap();
Write(sm, "Hello");