不要在成员签名中嵌套泛型类型

2023-12-10

我已经获得了一个扩展类,它实现了以下具有违反 CA1006:DoNotNestGenericTypesInMemberSignatures 规则的签名的成员。

警告所指的代码包含在下面。

我应该如何重构代码来解决 CA1006 警告?

请记住,我对表达式树不是很熟悉,尽管我对匿名方法、委托和 lambda 有很好的掌握。

任何帮助将不胜感激。

    public static DataServiceQuery<TElement> Expand<TElement, TPropType>(this DataServiceQuery<TElement> source, Expression<Func<TElement, TPropType>> propertySelector) 
    {
        string includeString = BuildString(propertySelector);
        return source.Expand(includeString);
    }

    private static string BuildString(Expression propertySelector)
    {
        switch (propertySelector.NodeType)
        {
            case ExpressionType.Lambda:
                LambdaExpression lambdaExpression = (LambdaExpression)propertySelector;
                return BuildString(lambdaExpression.Body);

            case ExpressionType.Quote:
                UnaryExpression unaryExpression = (UnaryExpression)propertySelector;
                return BuildString(unaryExpression.Operand);

            case ExpressionType.MemberAccess:

                MemberExpression memberExpression = (MemberExpression)propertySelector;
                MemberInfo propertyInfo = memberExpression.Member;

                if (memberExpression.Expression is ParameterExpression)
                {
                    return propertyInfo.Name;
                }
                else
                {
                    // we've got a nested property (e.g. MyType.SomeProperty.SomeNestedProperty)
                    return BuildString(memberExpression.Expression) + "/" + propertyInfo.Name;
                }

            case ExpressionType.Call:
                MethodCallExpression methodCallExpression = (MethodCallExpression)propertySelector;
                if (IsSubInclude(methodCallExpression.Method)) // check that it's a SubInclude call
                {
                    // argument 0 is the expression to which the SubInclude is applied (this could be member access or another SubInclude)
                    // argument 1 is the expression to apply to get the included property
                    // Pass both to BuildString to get the full expression
                    return BuildString(methodCallExpression.Arguments[0]) + "/" +
                           BuildString(methodCallExpression.Arguments[1]);
                }
                // else drop out and throw
                break;
        }
        throw new InvalidOperationException("Expression must be a member expression or an SubInclude call: " + propertySelector.ToString());

    }

    private static readonly MethodInfo[] SubIncludeMethods;
    static MyExtensions()
    {
        Type type = typeof(MyExtensions);
        SubIncludeMethods = type.GetMethods().Where(mi => mi.Name == "SubExpand").ToArray();
    }

    private static bool IsSubInclude(MethodInfo methodInfo)
    {
        if (methodInfo.IsGenericMethod)
        {
            if (!methodInfo.IsGenericMethodDefinition)
            {
                methodInfo = methodInfo.GetGenericMethodDefinition();
            }
        }
        return SubIncludeMethods.Contains(methodInfo);
    }

    public static TPropType SubExpand<TSource, TPropType>(this Collection<TSource> source, Expression<Func<TSource, TPropType>> propertySelector)
        where TSource : class
        where TPropType : class
    {
        throw new InvalidOperationException("This method is only intended for use with DataServiceQueryExtensions.Expand to generate expressions trees"); // no actually using this - just want the expression! 
    }

    public static TPropType SubExpand<TSource, TPropType>(this TSource source, Expression<Func<TSource, TPropType>> propertySelector)
        where TSource : class
        where TPropType : class
    {
        throw new InvalidOperationException("This method is only intended for use with DataServiceQueryExtensions.Expand to generate expressions trees"); // no actually using this - just want the expression! 
    }

该警告是一般警告,旨在帮助您设计更好、更简单的公共界面。在这种情况下,您会收到一条警告:Expression<Func<TElement, TPropType>>你的方法中的参数。但是,对于此方法,简化类型没有意义,您应该使用属性或从规则集中完全删除该规则。


一个愚蠢的例子是这样的方法,您可能应该考虑遵循规则的建议:

public void F(Dictionary<String, List<Tuple<String, Int32>>> dictionary);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不要在成员签名中嵌套泛型类型 的相关文章

随机推荐

  • Google 地图 - Android 应用程序未加载 - 空对象引用

    我目前正在尝试开发一个与谷歌地图集成的Android应用程序 目前我很难找到错误 因为代码来自谷歌网站本身 只不过它是一个SupportMapFragment 如果您知道 SupportMapFragment 的实际教程也会很棒 实际 因为
  • mysqli_begin_transaction()和mysqli_autocommit有什么区别

    Does mysqli begin transaction 实现相同的功能mysqli autocommit mysqli autocommit 永久设置自动提交模式 这意味着如果你将其设置为0 然后提交事务 模式将保持为0 mysqli
  • 如何在android中读取特定联系人的收件箱短信?

    我正在尝试读取来自内容提供商的短信 我有以下代码 Uri uri Uri parse SMS URI INBOX String whereClause address String whereArgs address String proj
  • JavaFX EXCEPTION_ACCESS_VIOLATION

    我对 JavaFX 桌面应用程序有疑问 特别是 3D 渲染功能 每次我尝试构建和启动 JavaFX 应用程序时 JVM 都会崩溃 并且会收到类似于以下错误的错误 A fatal error has been detected by the
  • 找不到要加载的 DSO:libhermes-executor-release.so

    log Time Tag Message 15 56 52 131 SoLoader couldn t find DSO to load libjscexecutor so 15 56 54 275 SoLoader couldn t fi
  • 如何在powershell中向异步作业添加回调函数并获取返回数据

    我一直在互联网上搜索并组合了许多不同的代码片段 但我只是没有成功地为我的异步作业创建回调 我的想法是 我想运行多个作业 并且使用回调方法 我将能够解析不同作业的输出 以更改主脚本中的某些状态和输出 目前我有这个 虽然事件被调用 但我无法获得
  • 找不到 JSP 文件可能是 spring 中的一个错误吗

    我在我的应用程序中遵循了 Spring Boot 的官方文件和包结构 但我仍然收到白标签页面错误 大多数答案和建议都不能解决这个问题 这可能是 Spring Boot 中的一个错误吗 下面是我放置代码的方式以及文件和文件夹的排列结构 应用程
  • 自定义视图,不需要 xml 中的layout_width

    Android SDK有TableLayout容器
  • 仅在使用 iTextSharp 进行生产时出现“文档未打开”错误

    我在 iTextSharp 中收到 文档未打开 错误 但仅限于生产环境 该代码在我的开发机器和暂存中运行良好 我在阶段服务器上的 Temp 文件夹中设置了相同的权限 public static byte ConvertHtmlToPdf s
  • 使用 Java 在多个文本文件中查找并替换一个单词?

    如何使用 Java 在多个文本文件中查找并替换单词 这是我为单个人做的方法String public class ReplaceAll public static void main String args String str We wa
  • Android 导航架构组件 - 获取当前可见片段

    在尝试导航组件之前 我曾经手动执行片段事务并使用片段标签来获取当前片段 val fragment MyFragment supportFragmentManager findFragmentByTag tag MyFragment 现在在我
  • 使用 NHibernate 测试连接参数

    我们有一个程序 用户可以在其中指定他们的数据库连接参数 通常的嫌疑人包括主机 端口 用户名 密码和表名 我们使用 NHibernate 连接到数据库 我们想要做的是能够使用 NHibernate 构建配置 然后在继续其他操作之前测试连接参数
  • Dagger2 和依赖组件中的限定符

    我有一个应用程序组件和一个依赖组件 应用程序组件声明显式依赖项 依赖组件可以注入这些依赖项 但是 当我有一个必须使用 Qualifier 消除歧义的依赖项时 依赖组件无法注入该依赖项 这是应用程序组件 Component modules A
  • java 在子类构造函数中初始化基类字段

    这是一个关于java子类的非常基本的问题 我还是不明白 假设我有一个包含三个字段且仅包含默认构造函数的超类 public class Superclass public int a public int b public int c 我想添
  • get_map 发生了什么?

    我今天开始使用 get map 向 googlemaps 发出大约 230 个地图请求 然后出现错误 丢失了原始错误 我的第一个假设是我使用它超出了 api 的限制 但我尝试使用开放街道地图 api 作为源 我收到的错误消息仍然看起来像 g
  • 使用 Vbscript 将记录批量插入到 Access 中

    我真的对这个感到抓狂 我有一个 vbscript 我正在尝试将几十万条记录插入到 Access 数据库中 显然 如果我一次只执行一个操作 速度会很慢 所以我想我可以通过某种事务批量插入它们 所以我尝试写这个 set rs CreateObj
  • 使用 jOOQ 查找即将到来的生日

    我正在尝试将查找即将到来的生日的现有查询转换为使用 jOOQ 我原来的查询 使用 MySQL 并且有点简化 是 SELECT COUNT FROM people WHERE DATE ADD people dob INTERVAL YEAR
  • 如何在python中编写没有任何分隔符的文本文件?

    我希望在 Python 2 7 中编写一个包含 6 行的简单文本文件 我正在使用这段代码 import csv export open images2 test tfw wb writer csv writer export delimit
  • Django 1 到 Django 2 on_delete 错误

    我相信这个工作流程是为以前的 Django 版本创建的 现在 当我尝试升级它时 添加 on delete 时出现错误 这是我所做的 但它仍然不起作用 我想知道我做错了什么 ORIGINAL class Task AbstractEntity
  • 不要在成员签名中嵌套泛型类型

    我已经获得了一个扩展类 它实现了以下具有违反 CA1006 DoNotNestGenericTypesInMemberSignatures 规则的签名的成员 警告所指的代码包含在下面 我应该如何重构代码来解决 CA1006 警告 请记住 我