C# - including string variable in code (bypass intellisense)

Hi,

I have a line of code which I will not know the operation name until runtime when the user passes it in via a parameter.

The code example is

this.CodeAcvtivity10.Output.ResultMessage = resultMessage;

Open in new window


The part after Output. (ResultMessage in the example above) is what can change. Is it possible to use a variable in the line of code so it will dynamically run depending on what the user passes into the application. For example, something like below

this.CodeAcvtivity10.Output.myVariable = resultMessage;

I hope this makes sense. Basically I'm automating some testing using UFT which is written C# and I have a common component which the users can setup there own input and output parameters. The Output parameter can be called anything so I need a way for the code to write to the output parameter entered by the user. The first code example above will write to an output parameter called 'ResultMessage'

Thanks

Gavin
victoriaharryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
It is possible via a technique called Reflection. There is a GetMember() method that allows to take the class member by name:
        MemberInfo[] myMembers = this.CodeAcvtivity10.Output.GetType().GetMember("ResultMessage");
        if(myMembers.Length > 0)
        {
                // do something
        }

Open in new window

Read here how to check what type the member is (method, property, etc)
victoriaharryAuthor Commented:
ok thanks,

If the member exist how would I then save a string to it ( this.CodeAcvtivity10.Output.ResultMessage = resultMessage;)

Appreciate your assistance

Thanks
chaauCommented:
Use the MemberType property to confirm that the requested member is indeed a property. Then use GetProperty() to get the property and then use the SetValue() to set the value:
MemberInfo[] myMembers = this.CodeAcvtivity10.Output.GetType().GetMember("ResultMessage");
if(myMembers.Length > 0)
{
  if(myMembers[0].MemberType ==  System.Reflection.MemberTypes.Property)
  {
     PropertyInfo pinfo = this.CodeAcvtivity10.Output.GetType().GetProperty("ResultMessage");
     pinfo.SetValue( this.CodeAcvtivity10.Output, resultMessage, null);
  }
}

Open in new window

But I actually hoped that you would want to try to learn it yourself;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.