C
C#4mo ago
engineertdog

Variable type passed to generic

Is there any way to get around this being an error?
c#
Type type = Type.GetType("OSIsoft.PI.Net, PIThreadInfo");

return powershell.Invoke<type>()
c#
Type type = Type.GetType("OSIsoft.PI.Net, PIThreadInfo");

return powershell.Invoke<type>()
19 Replies
Buddy
Buddy4mo ago
what gives an error and can you screenshot it?
engineertdog
engineertdog4mo ago
You can't use a variable as a type
Jimmacle
Jimmacle4mo ago
yes, use reflection to get the correct generic instantiation of the method
engineertdog
engineertdog4mo ago
I don't need a method from the type
Jimmacle
Jimmacle4mo ago
you need the correct generic method, which you can't get with a variable using that syntax
engineertdog
engineertdog4mo ago
I need to invoke the powershell method with a type that I will only be able to know by name
Jimmacle
Jimmacle4mo ago
yes, i know what you're trying to do you can't do it without more reflection you need to get the method info for the open generic powershell.Invoke method then make the generic method using the type variable so you can call it something roughly like
var method = powershell.GetType().GetMethod("Invoke").MakeGenericMethod(type);
method.Invoke(powershell);
var method = powershell.GetType().GetMethod("Invoke").MakeGenericMethod(type);
method.Invoke(powershell);
engineertdog
engineertdog4mo ago
Okay, so that is similar to what I had seen, it just looked like it wasn't what I needed
c#
Type type = Type.GetType("OSIsoft.PI.Net.PIThreadInfo");
var fooMethod = powershell.GetType().GetMethod("Invoke").MakeGenericMethod(new[] { type });

return (fooMethod.Invoke(null, null) as Collection<PSObject>).Select(psobject => psobject.BaseObject).ToList();
c#
Type type = Type.GetType("OSIsoft.PI.Net.PIThreadInfo");
var fooMethod = powershell.GetType().GetMethod("Invoke").MakeGenericMethod(new[] { type });

return (fooMethod.Invoke(null, null) as Collection<PSObject>).Select(psobject => psobject.BaseObject).ToList();
[Error: Ambiguous match found.] { name: 'System.Reflection.AmbiguousMatchException' } Close, but should be able to get there
Jimmacle
Jimmacle4mo ago
you'll have to provide enough information to GetMethod to make it not ambiguous i don't know what type powershell is but i'd guess there are multiple overloads of Invoke
engineertdog
engineertdog4mo ago
Yeah, there's 8 I call it with no params powershell.Invoke(); But calling fooMethod.Invoke() with no params throws an error
Jimmacle
Jimmacle4mo ago
you have to pass it the powershell instance since it's not a static method
engineertdog
engineertdog4mo ago
fooMethod.Invoke(powershell, new object[] { })
Jimmacle
Jimmacle4mo ago
not sure you need to pass it an empty array, i haven't had to do reflection like this in a bit .GetMethod("Invoke", BindingFlags.Public | BindingFlags.Instance, []); should find the parameterless overload you want if you didn't get that yet
engineertdog
engineertdog4mo ago
I tried null too Ah sorry, this was Framework
Jimmacle
Jimmacle4mo ago
ah, substitute [] for an empty array then no cool syntax allowed in framework
engineertdog
engineertdog4mo ago
Yeah, I hate this vendor
engineertdog
engineertdog4mo ago
No description
Jimmacle
Jimmacle4mo ago
maybe the api is different :PepeHmmm: ultimately you want to give it the empty type array, so whatever overload lets you do that with the least amount of extra stuff
Waffles from Human Resources
:SCtwocats: