通过反射和返回类型查找方法 [英] Finding method through reflection and return type

查看:73
本文介绍了通过反射和返回类型查找方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行了一些反射并设法从GetMethods中获取了一些数据之后,我终于碰壁了.我试图找到任何有关此消息的消息,但无济于事.

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.

基本上,我正在ASP.NET(MVC webapi)中创建RESTFUL Api,并使用反射来查找包含数百个视图/存储过程的DataComponent类的正确方法.我已经克服了基本的障碍,但是现在当我终于尝试使用参数来查找特定的SQL视图时,我似乎遇到了错误:

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);

但是结果是两种不同的方法.

据我的经理说,这是由于我们使用了两个不同的视图,这些视图返回了两个不同的DataType(一个是DataReader,另一个是DataSet).

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).

我想问的是 HIVEMIND 是如何通过手动检查返回的返回类型为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?

后续问题:

我似乎无法将结果放入DataSet中,因为.Invoke方法需要一个 Object .我试图将返回值设置为Object,然后也将该对象转换为DataSet ...

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

其他信息:无法转换类型为'System.Object'的对象键入"System.Data.DataSet".

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

问题继续:

试图实施答案提供的解决方案

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.

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

引发错误

未知的方法GetMethods(string,System.Reflection.BindingFlags)的System.Type

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

还有

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

引发此错误:

变量mi的未知类型

Unkown type of variable mi

谢谢

推荐答案

对于您的后续问题,您需要创建一个调用该方法所用的特定类型的对象.对于具有默认(无参数)构造函数的类型,可以执行以下操作:

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 ;

这篇关于通过反射和返回类型查找方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆