简体   繁体   中英

Finding method through reflection and return type

After having worked with reflection some and managed to get some data back from GetMethods I've finally hit a wall. I've tried to find any sources on this, but to no avail.

Basically I'm creating a RESTFUL Api in ASP.NET (MVC webapi) and using reflection to find the correct method of a DataComponent class that contains hundreds of views / stored procedures. I've gotten past the basic hurdles, but now when I'm finally attempting to use a parameter to find a specific SQL-view I seem to be getting an error:

Ambiguous match found.

I'm assuming this is because I'm attempting to find a single method through

MethodInfo theMethod = myType.GetMethod(toCheck);

But the result is two different methods .

According to my manager it's due to the fact that we are using two different views that return two different DataTypes (one a DataReader, the other a DataSet).

What I want to ask the HIVEMIND is how I can narrow down these two results to a single result either with the help of manually checking for the resulting returntype being DataSet or any other way?

Follow-up issue:

I seem to be unable to put the results in a DataSet as the .Invoke method expects an Object . I've attempted to set the return to an Object and then casting the object to a DataSet too...

Type myType = (typeof(myClass));
            MethodInfo[] arrayToCheck = myType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            Object result = new Object();
            foreach (MethodInfo mi in arrayToCheck)
            {
                if (mi.Name.Equals(param) && mi.ReturnType == typeof(DataSet))
                {
                    result = mi.Invoke(mi, arr);
                }
            }
DataSet ds = (DataSet)result; // Error here

Additional information: Unable to cast object of type 'System.Object' to type 'System.Data.DataSet'.

Continuation of issue:

Attempted to implement the solution provided by the answer

 String[] arr = {"", conStr, ""};
            var myType = (typeof(JaberoDC.JaberoDC.JaberoDC));


            var method = myType.GetMethods(param, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Single(mi => mi.ReturnType == typeof(DataSet));
            var subject = Activator.CreateInstance(myType);
            var result = method.Invoke(subject, arr);


            DataSet ds = (DataSet)result;

However, it doesn't seem to work as intended.

The line

  var method = myType.GetMethods (param, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) .Single(mi => mi.ReturnType == typeof(DataSet));

Throws the error

Unkown method GetMethods(string, System.Reflection.BindingFlags) of System.Type

And

mi => mi.ReturnType == typeof(DataSet)); 

Throws this error:

Unkown type of variable mi

Thanks

For your follow-up issue, you need to create an object of the specific type you're calling the method on. For types that have default (no-parameter) constructors, you can do this:

        Object result = Activator.CreateInstance(myType);

So your overall code could look like this:

var myType = typeof(myClass);


var method = myType
    .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    .Single(mi => mi.Name == param && mi.ReturnType == typeof(DataSet));
var subject = Activator.CreateInstance(myType);
var result = method.Invoke(subject, arr);


Dataset ds = (DataSet)result ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM