mercredi 30 août 2017

C# Casting an object returned from invocation and invoking a method on that object

I am trying to invoke a method that is within the returned class of another invoked method.

I am trying to call the GetConnectionCost() method from the ConnectionProfile class. The ConnectionProfile object was returned by Invoking the GetInternetConnectionProfile method from NetworkInformation class.

Below is my code so far:

using System.Reflection;

var t = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");

var profile = t.GetTypeInfo().GetDeclaredMethod("GetInternetConnectionProfile").Invoke(null, null);

var cost = profile.GetTypeInfo().GetDeclaredMethod("GetConnectionCost").Invoke(null, null); //This does not work of course since profile is of type object.

I rarely use reflection in my code so I am not an expert in the matter but I am trying to find a way to case the profile object and invoke the GetConnectionCost method on it.

Any suggestions





Aucun commentaire:

Enregistrer un commentaire