0

I am reflecting a DLL. The first part works fine:

    System.Type typeImageInfo= assembly.GetType("MyNameSpace.ImageInfo");
    var imageInfo= System.Activator.CreateInstance(typeImageInfo);
    MethodInfo myMethod = typeYouTube.GetMethod("GetImageInfo");
    var ImageData= myMethod.Invoke(imageInfo, new object[] { "https://www.website.com/image.png" });

I know the code works because with Debug I can see imageData get filled with the proper information.

Now the ImageData class contains a function:

public string GetUri(Func<DelegatingClient> makeClient);

Which i don't know how to access. If it was not a reflection of the DLL, but calling it directly, I would just do:

string imageURL= ImageData.GetUri();

How would I invoke the GetUri() method with reflection from ImageData ?

Dror
  • 1,262
  • 3
  • 21
  • 34
  • Possible duplicate of [How do I use reflection to call a generic method?](https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – Renat Aug 16 '19 at 23:16
  • It seems that I still can't solve it with that question answers. – Dror Aug 16 '19 at 23:33
  • 1
    Do you have static type information for `DelegatingClient` (i.e. can you reference it directly in your code)? If so, you pass the delegate the same way you pass the URL to `GetImageInfo`. – madreflection Aug 16 '19 at 23:33
  • If not, you have to get the `ParameterInfo` from `GetUri`'s `MethodInfo` and close-construct a delegate with that parameter type for the type argument. – madreflection Aug 16 '19 at 23:35
  • Doesn't seem to have static type information for `DelegatingClient`. Sample with `ParameterInfo` would be great. – Dror Aug 16 '19 at 23:39
  • The fact that `DelegatingClient` is the return type makes this rather difficult. Creating the return object and populating any values you need to will require even more reflection, but returning it will require a thunk because you can't cast it to the necessary type. It would help if you could say exactly what DLL this is. It may indicate an easier way. It sounds like you're trying to dig into the internals rather than use the public API. What exactly ar eyou trying to do? – madreflection Aug 16 '19 at 23:48

1 Answers1

1

@madreflection Appreciate your direction! Was able to make it work. The solution was simple than expected.

Starting code:

    System.Type typeImageInfo= assembly.GetType("MyNameSpace.ImageInfo");
    var imageInfo= System.Activator.CreateInstance(typeImageInfo);
    MethodInfo myMethod = typeYouTube.GetMethod("GetImageInfo");
    var ImageData= myMethod.Invoke(imageInfo, new object[] { "https://www.website.com/image.png" });

Soultion:

  MethodInfo myMethod2= ImageData.GetType().GetMethod("GetUri");
  var urlResult = myMethod2.Invoke(ImageData, new object[] {null });

Now I can get the function value.

Dror
  • 1,262
  • 3
  • 21
  • 34