C
C#10mo 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
Buddy10mo ago
what gives an error and can you screenshot it?
engineertdog
engineertdogOP10mo ago
You can't use a variable as a type
Jimmacle
Jimmacle10mo ago
yes, use reflection to get the correct generic instantiation of the method
engineertdog
engineertdogOP10mo ago
I don't need a method from the type
Jimmacle
Jimmacle10mo ago
you need the correct generic method, which you can't get with a variable using that syntax
engineertdog
engineertdogOP10mo ago
I need to invoke the powershell method with a type that I will only be able to know by name
Jimmacle
Jimmacle10mo 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
engineertdogOP10mo 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
Jimmacle10mo 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
engineertdogOP10mo ago
Yeah, there's 8 I call it with no params powershell.Invoke(); But calling fooMethod.Invoke() with no params throws an error
Jimmacle
Jimmacle10mo ago
you have to pass it the powershell instance since it's not a static method
engineertdog
engineertdogOP10mo ago
fooMethod.Invoke(powershell, new object[] { })
Jimmacle
Jimmacle10mo 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
engineertdogOP10mo ago
I tried null too Ah sorry, this was Framework
Jimmacle
Jimmacle10mo ago
ah, substitute [] for an empty array then no cool syntax allowed in framework
engineertdog
engineertdogOP10mo ago
Yeah, I hate this vendor
engineertdog
engineertdogOP10mo ago
No description
Jimmacle
Jimmacle10mo 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
The Fog from Human Resources
:SCtwocats:
Want results from more Discord servers?
Add your server