vendredi 15 novembre 2019

C# Reflection accessing field from class mainpage in UWP application

what I would like to be able to do is modify the double voltage field from within class WebSocketHandler using setvalue.

this is what I have come up with in the context below.

a string is sent to a webserver running within the app that string is split and the string parts are used to reference a field.

for example "SET voltage 100" sent to the webserver as a string is split into three parts. SET is used to identify its a command and the voltage / 100 parts become word[1] (the field) and word[2] (the value).

i believe i am having problems with tyeps. any help would be appreciated.

enter code here

namespace App3
{  

public partial class MainPage : Page
{

    public static double voltage;
    public static double current;
    public static double power;

public MainPage()
    {


voltage = somevalue;
}
}

class WebSocketHandler : IWebSocketRequestHandler
{
void OnDataReceived(WebSocket socket, string frame)
    {

 string[] words = frame.Split(" ");
 App3.MainPage  myObject = new App3.MainPage ();
            Type myType = typeof(App3.MainPage);
            FieldInfo myFieldInfo = myType.GetField(words[1],
                BindingFlags.NonPublic | BindingFlags.Instance);

            myFieldInfo.SetValue(myObject, words[2]);

}


} 

 }




Aucun commentaire:

Enregistrer un commentaire