gpt4 book ai didi

c# - 使用反射设置类实例的字段

转载 作者:行者123 更新时间:2023-11-30 17:44:16 25 4
gpt4 key购买 nike

我有以下与反射相关的问题,我有一个看起来像这样的方法:

  [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 context 我知道,我如何获取信息不是问题,问题是如何使前面提到的代码像我尝试的那样动态运行:

/* 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

我的问题是我有一个实例的对象,我试图访问这个对象的类并修改那个类的字段。

如果有任何帮助,我将不胜感激,谢谢

编辑 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());

enter image description here

最佳答案

如果我理解你的话,你有一个类似的类

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

关于c# - 使用反射设置类实例的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com