Setting a class instance field with reflection

I have the following reflection related problem, I have a method that looks like this:

  [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);
        }

      

That CodedUITest

, SteamMap is a class ( uiTest map

). WriteMessageParams

- class, actually a real method WriteMessage

, but this class allows me to override the string that is used in my tests by the method WriteMessage

, and I plan to make this part of the code more dynamic into the method Read

.:

   a.writeMessageParams.UIItemEditText = TestContext.DataRow["SearchQuery"].ToString();

      

My problem occurs in the context of testRead.Read as follows:

When this method is run, I have access to all actions from the corresponding instance ( a

in my case) and if they should use the context a.writeMessageParams.UIItemEditText

that I know how I get the information isn The problem is how to get the previous code to work dynamically like I have tried:

/* 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 class is called WriteMessageParams*/

public void Read(object obj, TestContext testContext)
{
//simplified code
//trying to access/get to the current instance 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());
}

      

I read this article and tried a few things https://msdn.microsoft.com/en-us/library/6z33zd7h%28v=vs.110%29.aspx

My problem is that I have an instance object and I am trying to access this object class and modify this class field.

Any help would be appreciated, Thanks

Edit 1: This is how the class I'm trying to access looks like this:

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
    }
    }

      

Edit 2 - I tried using GetNestedType, still didn't work ...

Object testObj = obj.GetType().GetNestedType("writeMessageParams",BindingFlags.Public);
 MessageBox.Show(testObj.GetType().ToString());

      

enter image description here

+3


source to share


1 answer


If I understand you, you have a class like

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";       
    }
}

      

(your code doesn't compile because you have writeMessageParams as a class and property, so I changed the property to writeMessageParams1)

And you want to change the UIItemEditText which you can do like



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);

}

      

which can be called like

SteamMap sm = new SteamMap();
Write(sm, "Hello");  

      

The key is to use .GetProperty

for the property and .GetField

for the field.

+1


source







All Articles