Link to home
Start Free TrialLog in
Avatar of victoriaharry
victoriaharry

asked on

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
Avatar of chaau
chaau
Flag of Australia image

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)
Avatar of victoriaharry
victoriaharry

ASKER

ok thanks,

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

Appreciate your assistance

Thanks
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial