我有以下与反射相关的问题,我有一个类似下面的方法:
[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是一个类(uiTest map
)。WriteMessageParams
是一个类,实际上真正的方法是WriteMessage
,但是该类允许我覆盖WriteMessage
方法在测试中使用的字符串,并且我计划在其中更动态地制作这部分代码Read
方法。 : a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();
我的问题发生在testRead.Read上下文中,如下所示:
当此方法运行时,我可以访问相应实例中的所有操作(在我的情况下为
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());
}
我已经阅读了这篇文章,并尝试了一些尝试
https://msdn.microsoft.com/en-us/library/6z33zd7h%28v=vs.110%29.aspx
我的问题是我拥有实例的对象,并且尝试访问该对象的类并修改该类的field。
我将不胜感激,
谢谢
编辑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
}
}
编辑2-我尝试使用GetNestedType,仍然没有成功。
Object testObj = obj.GetType().GetNestedType("writeMessageParams",BindingFlags.Public);
MessageBox.Show(testObj.GetType().ToString());
最佳答案
如果我了解你,你会有一个像
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
。