使用具有“params”参数的反射来调用方法。无参数的关键字 [英] Invoke a method using reflection with the "params" keyword without arguments
问题描述
就像这个问题我遇到了调用具有params关键字的方法的问题。我不断收到TargetParameterCountException异常。 参数计数不匹配。
目标是不带参数调用此方法:
IList< T> List(params Expression< Func< T,object>> [] includeProperties);
这是我到目前为止:
//获取泛型类型
var entityType = typeof(Applicant).Assembly.GetType(string.Format(Permet.BackEnd.ETL.Domain.Models。{0} ,tableName));
//创建将接收泛型类型的服务
var constructedIService = typeof(IService<>)。MakeGenericType(entityType);
//为我们调用的方法创建参数
var paramsType = typeof(Expression<> ;. MakeGenericType(typeof(Func<,>)。MakeGenericType(entityType,typeof (对象)))MakeArrayType();
//使用Unity实例化服务(todo:fix singleton)
var serviceInstance = UnitySingleton.Container.Resolve(constructedIService,);
//通过传递无参数,但告诉它要使用的签名(它没有重载)来调用服务方法List
//我没有列出params,没有超载但有相同的异常
//我得到异常参数计数不匹配
dynamic data = serviceInstance.GetType()。GetMethod(List,new Type [] {paramsType})。Invoke(serviceInstance,新对象[] {});
请注意,我试过只传递null,并使用重载GetMethod(字符串名称)同样的结果。
尝试使用单个参数 null
调用它,因为C#编译器将方法签名从方法(params object []参数)
重写为方法(object []参数)
以及对该方法的调用。
dynamic data = serviceInstance.GetType()。GetMethod(List,new Type [ ] {paramsType})。Invoke(serviceInstance,new object [] {null});
Just like this question I'm having issues invoking a method that has the "params" keyword. I keep getting TargetParameterCountException exceptions. "Parameter count mismatch". The goal is to call this method with no parameters:
IList<T> List(params Expression<Func<T, object>>[] includeProperties);
Here is what I have so far:
//Get generic type
var entityType = typeof(Applicant).Assembly.GetType(string.Format("Permet.BackEnd.ETL.Domain.Models.{0}", tableName));
//create service that will receive the generic type
var constructedIService = typeof(IService<>).MakeGenericType(entityType);
//create the argument for the method that we invoke
var paramsType = typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(entityType, typeof(object))).MakeArrayType();
//instantiate the service using Unity (todo: fix singleton)
var serviceInstance = UnitySingleton.Container.Resolve(constructedIService, "");
//Invoke the service method "List" by passing it no parameters but telling it the signature to use (it has no overloads)
//I tried without listing the params since it has no overload but same exception
//I get exception Parameter count mismatch here
dynamic data = serviceInstance.GetType().GetMethod("List", new Type[] { paramsType }).Invoke(serviceInstance, new object[] { });
Please note that I have tried just passing null and using the overload GetMethod(string name) with exactly the same result.
Try invoking it with a single parameter null
because the C# compiler rewrites the method signature from method(params object[] parameters)
to method(object[] parameters)
and also the calls to that method.
dynamic data = serviceInstance.GetType().GetMethod("List", new Type[] { paramsType }).Invoke(serviceInstance, new object[] { null });
这篇关于使用具有“params”参数的反射来调用方法。无参数的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!