Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标识在func中传递的类型_C#_Func - Fatal编程技术网

C# 标识在func中传递的类型

C# 标识在func中传递的类型,c#,func,C#,Func,我有一个这样的界面 public interface IMyService { T ServiceProxy<T>(Func<IService, T> request) where T : Response; } i、 e.各种不同的请求类型被发送到封装在Func中的ServiceProxy方法。所有请求都是相同基类的子类 出于测试目的,我需要创建此接口的假实现。我想根据传递给方法的请求类型做不同的事情 但我无法确定如何识别传入ServiceProxy方法的请求

我有一个这样的界面

public interface IMyService
{
    T ServiceProxy<T>(Func<IService, T> request) where T : Response;
}
i、 e.各种不同的请求类型被发送到封装在Func中的ServiceProxy方法。所有请求都是相同基类的子类

出于测试目的,我需要创建此接口的假实现。我想根据传递给方法的请求类型做不同的事情

但我无法确定如何识别传入ServiceProxy方法的请求类型

e、 g如果是ActivateAccountRequest,我想做一件事,如果是DeleteAccountRequest,我想做另一件事


有什么想法吗?

将界面更改为:

 T ServiceProxy<T>(Expression<Func<IService, T>> request) where T : Response;
T ServiceProxy(表达式请求),其中T:Response;
现在使用此扩展方法获取函数参数:

 public static IEnumerable<object> GetFunctionParameters<TInput, TOutput>(this Expression<Func<TInput, TOutput>> expression)
        {
            var call = expression.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Not a method call");

            foreach (Expression argument in call.Arguments)
            {
                LambdaExpression lambda = Expression.Lambda(argument, expression.Parameters);
                Delegate d = lambda.Compile();
                yield return d.DynamicInvoke(new object[1]);
            }
        }
公共静态IEnumerable GetFunctionParameters(此表达式)
{
var call=expression.Body作为MethodCallExpression;
if(call==null)
抛出新ArgumentException(“不是方法调用”);
foreach(call.Arguments中的表达式参数)
{
LambdaExpression lambda=Expression.lambda(参数,Expression.Parameters);
委托d=lambda.Compile();
收益率返回d.DynamicInvoke(新对象[1]);
}
}

然后只需调用
request.GetFunctionParameters().First().GetType()

如果您想根据参数类型执行不同的操作,请使用重载而不是泛型。
typeof(T)
有效。你为什么要这么做?使用泛型的要点是,您不需要知道具体的类型。如果您这样做,您可能需要使用重载或某种访问者实现(也使用重载)。如果您正在测试,您肯定知道测试的输入是什么,因此任何给定测试的类型是什么。。。你能再解释一下你的测试是什么,你想在测试中做什么,这取决于类型吗?@Chris是对的。您应该适当地删除测试。例如,如果您使用的是Moq,您可以配置该方法,使其在类型为您期望的类型时返回您想要的内容。如果不按照下面的答案传递表达式,您将无法找到它,因为您无法检查委托内调用的代码。抱歉-更改接口不是我的选项
 public static IEnumerable<object> GetFunctionParameters<TInput, TOutput>(this Expression<Func<TInput, TOutput>> expression)
        {
            var call = expression.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Not a method call");

            foreach (Expression argument in call.Arguments)
            {
                LambdaExpression lambda = Expression.Lambda(argument, expression.Parameters);
                Delegate d = lambda.Compile();
                yield return d.DynamicInvoke(new object[1]);
            }
        }