user222427
user222427

Reputation:

Creating and passing parameters in by name

I'm doing a foreach loop on the parameters of a method.

MethodInfo objMethoda = typMyService.GetMethod(
                                        "GetBySystemWarrantyId",
                                        Type.EmptyTypes);
ParameterInfo[] pars = objMethoda.GetParameters();
foreach (ParameterInfo p in pars)
    Console.WriteLine("{0} {1}",p.ParameterType , p.Name);

When I try to invoke it, I get parameters that don't match.

My method has 5 parameters:

 System.String whereClause
 System.String orderBy
 System.Int32 start
 System.Int32 pageLength
 out System.Int32& totalCount

Can I create those arguments and pass it into the method?

var dataTableObjecta = objMethoda.Invoke(oMyServicea, null);

For example: the parameters, in the same order as above, would be

"1=1"
""
0
100000
out totalCount

Upvotes: 2

Views: 93

Answers (3)

Shaun Luttin
Shaun Luttin

Reputation: 141662

You are specifying the wrong method signature. Type.EmptyTypes indicates a method with no parameters. You need to specify any array of the proper types or specify no types at all.

Here it is as a DotNetFiddle.

using System;
using System.Reflection;

public class MyService
{
    public void GetBySystemWarrantyId(string whereClause, string orderBy, 
        int start, int pageLength, out int totalCount)
    {
        totalCount = 1234567890;
    }
}

public class Program
{
    public static void Main()
    {
        MethodInfo objMethoda = typeof(MyService)
             .GetMethod("GetBySystemWarrantyId", new Type[]
             {
                typeof (string), typeof (string), 
                typeof (int), typeof (int), typeof (int).MakeByRefType()
             });

        MethodInfo objMethodb = typeof(MyService)
             .GetMethod("GetBySystemWarrantyId");

        int count= -1;
        object[] args = new object[] { "", "", 1, 1, count };

        objMethoda.Invoke(new MyService(), args);
        Console.WriteLine(args[4]);

        objMethodb.Invoke(new MyService(), args);
        Console.WriteLine(args[4]);
    }
}

Upvotes: 1

Ian CT
Ian CT

Reputation: 1411

In order to invoke using the parameters, do the following:

var parameters = new [] {"1=1", "", 0, 100000,  totalCount};
var dataTableObjecta = objMethoda.Invoke(oMyServicea, parameters);

The value of parameters[4] will be changed according to the function.

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166486

Have a look at the below example

Method

public void GetBySystemWarrantyId(string whereClause, string orderBy, int start, int end, out int totalCount )
{
    totalCount = 3;
}

Usage

MethodInfo objMethoda = this.GetType().GetMethod("GetBySystemWarrantyId");
int cnt = 0;
object[] parameters = {"1=1", "", 0, 100000, cnt};
objMethoda.Invoke(this, parameters);
int parsedNumber = (int)parameters[4];

What I found was that if you don't pass the parameters in as a predeclared array of objects but merely as below

objMethoda.Invoke(this, new object[] { "1=1", "", 0, 100000, cnt });

the return value is not stored in cnt

Upvotes: 2

Related Questions