使用反射设置 class 实例的字段
Setting a field of a class's instance with reflection
我有以下与反射相关的问题,我有一个如下所示的方法:
[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是一个class(uiTest map
)。
WriteMessageParams
是一个 class,实际上真正的方法是 WriteMessage
但是这个 class 允许我覆盖 WriteMessage
方法在我的测试中使用的字符串,我打算在 Read
方法中使这部分代码更加动态。 :
a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();
我的问题发生在 testRead.Read 上下文中,如下所示:
当此方法为 运行ning 时,我可以访问相应实例的所有操作(在我的例子中为 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
我的问题是我有一个实例的对象,我尝试访问这个对象的 class 并修改 class 的字段。
如果有任何帮助,我将不胜感激,
谢谢
编辑 1:
这就是我尝试访问的 class 的样子:
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());
如果我理解你,你有一个class喜欢
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 作为 class 和 属性,所以我已将 属性 更改为 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
。
我有以下与反射相关的问题,我有一个如下所示的方法:
[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是一个class(uiTest map
)。
WriteMessageParams
是一个 class,实际上真正的方法是 WriteMessage
但是这个 class 允许我覆盖 WriteMessage
方法在我的测试中使用的字符串,我打算在 Read
方法中使这部分代码更加动态。 :
a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();
我的问题发生在 testRead.Read 上下文中,如下所示:
当此方法为 运行ning 时,我可以访问相应实例的所有操作(在我的例子中为 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
我的问题是我有一个实例的对象,我尝试访问这个对象的 class 并修改 class 的字段。
如果有任何帮助,我将不胜感激, 谢谢
编辑 1: 这就是我尝试访问的 class 的样子:
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());
如果我理解你,你有一个class喜欢
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 作为 class 和 属性,所以我已将 属性 更改为 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
。