简体   繁体   English

通过反射和返回类型查找方法

[英]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. 在进行了一些思考之后,设法从GetMethods中获取了一些数据,我终于碰壁了。 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. 基本上,我正在ASP.NET(MVC webapi)中创建RESTFUL Api,并使用反射来查找包含数百个视图/存储过程的DataComponent类的正确方法。 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: 我已经克服了基本的障碍,但是现在当我终于尝试使用参数来查找特定的SQL视图时,我似乎遇到了错误:

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). 根据我的经理的说法,这是由于我们使用了两个不同的视图,它们返回两种不同的DataType(一个是DataReader,另一个是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? 我想问的是HIVEMIND ,如何借助手动检查结果返回类型为DataSet或其他方式将这两个结果缩小到单个结果?

Follow-up issue: 后续问题:

I seem to be unable to put the results in a DataSet as the .Invoke method expects an Object . 我似乎无法将结果放入DataSet中,因为.Invoke方法需要Object I've attempted to set the return to an Object and then casting the object to a DataSet too... 我试图将返回值设置为Object,然后也将该对象转换为DataSet。

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'. 附加信息:无法将类型为“ System.Object”的对象转换为类型为“ 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 System.Type的未知方法GetMethods(string,System.Reflection.BindingFlags)

And

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

Throws this error: 引发此错误:

Unkown type of variable mi 变量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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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