替代嵌套类型 Expression<Func<T>>

Alternative to nested type of type Expressionlt;Funclt;Tgt;gt;(替代嵌套类型 Expressionlt;Funclt;Tgt;gt;)
本文介绍了替代嵌套类型 Expression<Func<T>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在调用服务时使用了一个函数.在调用服务之前,它会创建一个日志条目:

I have a function used when calling a service. Before it call the service, it will create a log entry:

protected TResult CallService<TService, TResult>(TService service,
    Expression<Func<TService, TResult>> functionSelector)
{
    Logger.LogServiceCall(service, functionSelector);
    return functionSelector.Compile()(service);
}

Visual Studio 2010 代码分析器在以下消息中通知我不应使用嵌套类型:

The Visual Studio 2010 Code Analyzer informs me that I shouldn't use Nested Type in the following message:

CA1006:Microsoft.Design:考虑一个设计在哪里'ServiceManager.CallService(TService,表达式<Func<TService, TResult>>)'不嵌套泛型类型'Expression'.

CA1006 : Microsoft.Design : Consider a design where 'ServiceManager.CallService<TService, Result>(TService, Expression<Func<TService, TResult>>)' doesn't nest generic type 'Expression<Func<TService, TResult>>'.

虽然我可以简单地为该条目创建一个禁止规则,但是否存在可以阻止显示此类警告的替代方法?

While I could simply create a suppression rule for this entry, is there is an alternative that exist that would prevent displaying such warning?

推荐答案

在这种情况下我会抑制它,原因是调用者不必处理嵌套泛型,他只是传递一个 lambda 表达式,它易于使用.

I would suppress it in this case, with the reason that the caller doesn't have to cope with nested generics, he is just passing a lambda expression, which is easy to use.

CA 不会为 lambda 表达式设置例外.有时最好是抑制它然后编写奇怪的代码.

CA does not make exceptions for lambda expressions. Sometimes It is better to suppress it then to write weird code.

这篇关于替代嵌套类型 Expression&lt;Func&lt;T&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to do a full outer join in Linq?(如何在 Linq 中进行完整的外部联接?)
How do I group data in an ASP.NET MVC View?(如何在 ASP.NET MVC 视图中对数据进行分组?)
how to update the multiple rows at a time using linq to sql?(如何使用 linq to sql 一次更新多行?)
how to recognize similar words with difference in spelling(如何识别拼写不同的相似词)
C# Linq to SQL: How to express quot;CONVERT([...] AS INT)quot;?(C# Linq to SQL:如何表达“CONVERT([...] AS INT)?)
Why do quot;linq to sqlquot; queries starts with the FROM keyword unlike regular SQL queries?(为什么要“linq to sql与常规 SQL 查询不同,查询以 FROM 关键字开头?)