views:

56

answers:

1

I am trying to get the value of local path by doing the following:

  Dim bar As WebProxy = WebProxy.GetDefaultProxy
  Dim scriptEngine = bar.GetType().GetProperty("ScriptEngine", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
  Dim acs As PropertyInfo = scriptEngine.PropertyType().GetProperty("AutomaticConfigurationScript", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
  Dim localPath As PropertyInfo = acs.PropertyType().GetProperty("LocalPath", Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
  Dim value As String = localPath.GetValue(acs, Nothing).ToString

I am pretty sure that the problem here is that I am passing a PropertyInfo object into localPath.GetValue, not the actual object itself. The only problem is that i can not cast the result of

Dim acs As PropertyInfo = scriptEngine.PropertyType().GetProperty("AutomaticConfigurationScript", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)

into a System.Uri AutomaticConfigurationScript and pass that in, therefore I get an error "Object does not match target type".

Any ideas?

P.S I realise this is not a c# question, but not wanting to limit the possible answers I have tagged it as such as it is a .Net question and If i receive an answer in C# i can translate.

+1  A: 

You need to get the actual object at each step, and use it to get the next property:

Dim bar As WebProxy = WebProxy.GetDefaultProxy

Dim scriptEngineProperty = bar.GetType().GetProperty("ScriptEngine", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
Dim scriptEngineObject as Object = scriptEngineProperty.GetValue(bar, Nothing)

Dim acsProperty As PropertyInfo = scriptEngineObject.GetType().GetProperty("AutomaticConfigurationScript", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
Dim acsObject as Object = acsProperty.GetValue(scriptEngineObject, Nothing)

Dim localPathProperty As PropertyInfo = acsObject.GetType().GetProperty("LocalPath", Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
Dim value As String = localPath.GetValue(acsObject, Nothing).ToString
Reed Copsey
Brilliant, Thanks Reed. I should have known that but I'm in a bit of a time limit panic at the moment :)
Ben
@Ben: Glad it helps ;)
Reed Copsey