Passing a Parameter Array by ref to C# DLL via Reflection - c#

All, I have a number of C# DLLs that I want to call from my application at runtime using System.Reflection. The core code I use is something like
DLL = Assembly.LoadFrom(Path.GetFullPath(strDllName));
classType = DLL.GetType(String.Format("{0}.{0}", strNameSpace, strClassName));
if (classType != null)
{
classInstance = Activator.CreateInstance(classType);
MethodInfo methodInfo = classType.GetMethod(strMethodName);
if (methodInfo != null)
{
object result = null;
result = methodInfo.Invoke(classInstance, parameters);
return Convert.ToBoolean(result);
}
}
I would like to know how I can pass in the array of parameters to the DLL as ref so that I can extract information from what happened inside the DLL. A clear portrayal of what I want (but of course will not compile) would be
result = methodInfo.Invoke(classInstance, ref parameters);
How can I achieve this?

Changes to ref parameters are reflected in the array that you pass into MethodInfo.Invoke. You just use:
object[] parameters = ...;
result = methodInfo.Invoke(classInstance, parameters);
// Now examine parameters...
Note that if the parameter in question is a parameter array (as per your title), you need to wrap that in another level of arrayness:
object[] parameters = { new object[] { "first", "second" } };
As far as the CLR is concerned, it's just a single parameter.
If this doesn't help, please show a short but complete example - you don't need to use a separate DLL to demonstrate, just a console app with a Main method and a method being called by reflection should be fine.

Related

Method.Invoke failing with Parameter Count Mismatch

I am trying to invoke a generic method. The definition of the method is as follows:
public System.Collections.Generic.IList<T> Query<T>(string query, [string altUrl = ""])
where T : new()
This is from the SalesforceSharp library on github. I am trying to make an additional service layer over this call and am struggling to invoke it. See my code below.
public List<T> Query<T>()
{
//IList<Salesforce.Account> _returnList = null;
IList<T> _returnList = null;
Type _t = typeof(T);
SqlBuilder _sb = new SqlBuilder();
_sb.Table = _t.Name.ToString();
foreach (PropertyInfo p in _t.GetProperties()) _sb.Fields.Add(p.Name.ToString());
MethodInfo method = _Client.GetType().GetMethod("Query");
method = method.MakeGenericMethod(_t);
try
{
object[] _prms = new object[1];
_prms[0] = _sb.SQL;
_returnList = (IList<T>)method.Invoke(_Client, new object[] { _prms });
//_returnList = _Client.Query<Salesforce.Account>(_sb.SQL);
}
catch { }
return (List<T>)_returnList;
}
If I run this i get a Parameter Count Mismatch exception on the method.invoke line, but i am confused because if i bring in the two uncommented lines and execute without the generic call it is working ok. I have tried many combinations of string arrays wrapped in object arrays, strings in strings, etc but can't get it to go. I thought maybe it was treating the second parameter as mandatory? but adding another object to my _prms array didnt work either.
Please help!
Thanks,
Dom
For optional parameters that you don't want to specify a value for, you have to pass Type.Missing like this:
_returnList = (IList<T>)method.Invoke(_Client, new object[] { _sb.SQL, Type.Missing });
Quoting from this reference:
Use the Missing field for invocation through reflection to obtain the default value of a parameter
Please note also that another problem is that you are currently passing an object[] instead of just the query string.
The optional parameters are just syntactic sugars, when you don't supply an optional parameter compiler calls the method with the given default value. But in case of Reflection, you need to do it manually.
object[] _prms = new object[2];
_prms[0] = _sb.SQL;
_prms[1] = "";
_returnList = (IList<T>)method.Invoke(_Client, _prms);

Dynamically reference eConnect dll and create an array of instances with the invoke method

i'm looking to create an array of instances using the invoke method on a constructor in a runtime referenced dll. I have to do this because I am changing which dll's are referenced based on settings chosen by the user.
Here is the code that isn't working
Type IVItemMasterType = EConSerialAssembly.GetType("Microsoft.Dynamics.GP.eConnect.Serialization.IVItemMasterType");
ConstructorInfo IVItemMasterConstructor = IVItemMasterType.GetConstructor(Type.EmptyTypes);
object IV = null;
object[] IVItemMaster = null;
if(IVItemMasterConstructor != null)
{
IV = IVItemMasterConstructor.Invoke(new object[0]);
IVItemMaster = IVItemMasterConstructor.Invoke(new object[0])[1];
if (IV != null)
{
//good
IVItemMaster[0] = IV;
}
}
I want to create an array of instances as the IVItemMaster but i don't know how to create an array of instances with the invoke method
Thanks for the help
use the array.createinstance method
This method has two parameters:
param 1 = Type (use your IVItemMasterType here)
param 2 = array size
So, instantiate a dynamic object called IVItemMasters and then reference this object like IVItemMasters[count] = a single instance of the IVItemMaster Type
I hope this works.

How to invoke a method using Reflection

for (int tsid = 1; tsid < controller.getRowCount(currentTest); tsid++)
{
// values from xls
keyword = controller.getCellData(currentTest, "Keyword", tsid);
//object=controller.getCellData(currentTest, "Object", tsid);
currentTSID = controller.getCellData(currentTest, "TSID", tsid);
stepDescription = controller.getCellData(currentTest, "Description", tsid);
Console.WriteLine("Keyword is:" + keyword);
try
{
// --this is equivalent java code
//MethodInfo method= Keywords.class.getMethod(keyword);
MethodInfo method= method.GetMethodBody(keyword);
String result = (String)method.Invoke(method);
if(!result.StartsWith("Fail")) {
ReportUtil.addKeyword(stepDescription, keyword, result,null);
}
}
catch (...) { ... }
}
Here from excel sheet we are reading the Keyword and we need to call that specific method using Reflection:
MethodInfo method= method.GetMethodBody(keyword);
String result = (String)method.Invoke(method);
But these two lines of code are throwing me some syntax error. I have used using System.Reflection; at the top of the file, but the error persists.
In C# you don't use Type.class, instead you use typeof(Type).
You can use this in combination with GetMethod(string methodName) to get a specific MethodInfo, which you can then Invoke(object instance, object[] parameters). For static classes object instance should be null.
For example:
typeof(Console).GetMethod("ReadLine").Invoke(null, new object[] { });
Don't pass the MethodInfo Object method to the invoke call but instead the object on which you want to call the method. I can't see the object you probably could do this on.
Furthermore Invoke has two parameters (see MSDN). So the syntax error is probably that you forgot to pass the parameters.
As far as I understand your code you have an Excel sheet holding some method names which you want to call dynamically. Right?
But you can't just get a .NET Object from a Excel cell.
If you need an object to call the method on, you'll need to create one and establish the correct state to call it. So you could probably add some more data to your excel sheet and use it to set up the object.
May be for a Future reader, can use something like this..
keyWordHolder program = new keyWordHolder();
MethodInfo[] methods = typeof(keyWordHolder).GetMethods();
foreach (MethodInfo meth in methods)
{
if (meth.Name == keywords)
{
meth.Invoke(program, null);
}

Dynamic invoke of a method using named parameters

We're currently using .NET 3.5 and part of our application uses dynamic invocation (using MethodBase.Invoke)
I am wondering if it is possible to mix in Named Parameters (in .NET 4) with dynamic invocation, to perform something similar to:
// Dictionary that holds parameter name --> object mapping
var parameters = new Dictionary<string, object>();
// Add parameters ....
// Invoke where each parameter will match the one from the method signature.
methodInfo.Invoke(obj, parameters);
Is there any API that allows this option out of the box? If not, is it possible to develop some solution to perform this?
EDIT:
Rethinking of this problem, it sounds similar to how the compiler may actually need to match method calls based on argument lists. Perhaps there's some Compiler API (or the new Roslyn project) that allows doing just this easily? (without coding it myself which may be prone to errors).
You can use code like this:
public static class ReflectionExtensions {
public static object InvokeWithNamedParameters(this MethodBase self, object obj, IDictionary<string, object> namedParameters) {
return self.Invoke(obj, MapParameters(self, namedParameters));
}
public static object[] MapParameters(MethodBase method, IDictionary<string, object> namedParameters)
{
string[] paramNames = method.GetParameters().Select(p => p.Name).ToArray();
object[] parameters = new object[paramNames.Length];
for (int i = 0; i < parameters.Length; ++i)
{
parameters[i] = Type.Missing;
}
foreach (var item in namedParameters)
{
var paramName = item.Key;
var paramIndex = Array.IndexOf(paramNames, paramName);
if (paramIndex >= 0)
{
parameters[paramIndex] = item.Value;
}
}
return parameters;
}
}
And then call it like this:
var parameters = new Dictionary<string, object>();
// Add parameters ...
methodInfo.InvokeWithNamedParameters(obj, parameters);
you can get your paramter names with the help of this article How can you get the names of method parameters? and then you can reorder them to invoke them as described here Reflection: How to Invoke Method with parameters
With .net4, I have an opensource framework ImpromptuInterface (found in nuget) that makes it easy to use the DLR apis for late invocation including named/optional parameters.
var result = Impromptu.InvokeMember(target, "MyMethod", parameters.Select(pair=> InvokeArg.Create(pair.Key, pair.Value)).Cast<object>().ToArray());

Invoking methods with optional parameters through reflection

I've run into another problem using C# 4.0 with optional parameters.
How do I invoke a function (or rather a constructor, I have the ConstructorInfo object) for which I know it doesn't require any parameters?
Here is the code I use now:
type.GetParameterlessConstructor()
.Invoke(BindingFlags.OptionalParamBinding |
BindingFlags.InvokeMethod |
BindingFlags.CreateInstance,
null,
new object[0],
CultureInfo.InvariantCulture);
(I've just tried with different BindingFlags).
GetParameterlessConstructor is a custom extension method I wrote for Type.
According to MSDN, to use the default parameter you should pass Type.Missing.
If your constructor has three optional arguments then instead of passing an empty object array you'd pass a three element object array where each element's value is Type.Missing, e.g.
type.GetParameterlessConstructor()
.Invoke(BindingFlags.OptionalParamBinding |
BindingFlags.InvokeMethod |
BindingFlags.CreateInstance,
null,
new object[] { Type.Missing, Type.Missing, Type.Missing },
CultureInfo.InvariantCulture);
Optional parameters are denoted by an ordinary attribute and are handled by the compiler.
They have no effect (other than a metadata flag) on the IL, and are not directly supported by reflection (except for the IsOptional and DefaultValue properties).
If you want to use optional parameters with reflection, you'll need to manually pass their default values.
I'll just add some code... because. The code isn't pleasent, I agree, but it is fairly straight forward. Hopefully this will help someone who stumbles accross this. It is tested, though probably not as well as you would want in a production environment:
Calling method methodName on object obj with arguments args:
public Tuple<bool, object> Evaluate(IScopeContext c, object obj, string methodName, object[] args)
{
// Get the type of the object
var t = obj.GetType();
var argListTypes = args.Select(a => a.GetType()).ToArray();
var funcs = (from m in t.GetMethods()
where m.Name == methodName
where m.ArgumentListMatches(argListTypes)
select m).ToArray();
if (funcs.Length != 1)
return new Tuple<bool, object>(false, null);
// And invoke the method and see what we can get back.
// Optional arguments means we have to fill things in.
var method = funcs[0];
object[] allArgs = args;
if (method.GetParameters().Length != args.Length)
{
var defaultArgs = method.GetParameters().Skip(args.Length)
.Select(a => a.HasDefaultValue ? a.DefaultValue : null);
allArgs = args.Concat(defaultArgs).ToArray();
}
var r = funcs[0].Invoke(obj, allArgs);
return new Tuple<bool, object>(true, r);
}
And the function ArgumentListMatches is below, which basically takes the place of the logic probably found in GetMethod:
public static bool ArgumentListMatches(this MethodInfo m, Type[] args)
{
// If there are less arguments, then it just doesn't matter.
var pInfo = m.GetParameters();
if (pInfo.Length < args.Length)
return false;
// Now, check compatibility of the first set of arguments.
var commonArgs = args.Zip(pInfo, (margs, pinfo) => Tuple.Create(margs, pinfo.ParameterType));
if (commonArgs.Where(t => !t.Item1.IsAssignableFrom(t.Item2)).Any())
return false;
// And make sure the last set of arguments are actually default!
return pInfo.Skip(args.Length).All(p => p.IsOptional);
}
Lots of LINQ, and this has not been performance tested!
Also, this will not handle generic function or method calls. That makes this significantly more ugly (as in repeated GetMethod calls).
All questions disappear as you see your code decompiled:
c#:
public MyClass([Optional, DefaultParameterValue("")]string myOptArg)
msil:
.method public hidebysig specialname rtspecialname instance void .ctor([opt]string myOptArg) cil managed
As you see, optional parameter is a real separate entity that is decorated with specific attributes and has to be respected accordingly when invoking via reflection, as described earlier.
With the opensource framework ImpromptuInterface as of version 4 you can use the DLR in C# 4.0 to invoke constructors in a very late bound way and it's totally aware of constructors with named/optional arguments, this runs 4 times faster than Activator.CreateInstance(Type type, params object[] args) and you don't have to reflect the default values.
using ImpromptuInterface;
using ImpromptuInterface.InvokeExt;
...
//if all optional and you don't want to call any
Impromptu.InvokeConstructor(type)
or
//If you want to call one parameter and need to name it
Impromptu.InvokeConstructor(type, CultureInfo.InvariantCulture.WithArgumentName("culture"))
I know this is an old thread but just want to add this.
If you're not sure how many parameters exist for the method, you can do this dynamically instead:
var method = obj.GetType().GetMethod("methodName");
int? parameters = method?.GetParameters().Length;
var data = method?.Invoke(prop, (object?[]?)(parameters.HasValue ? Enumerable.Repeat(Type.Missing, parameters.Value).ToArray() : Array.Empty<object>()));
Hope this helps.

Categories

Resources