C#学习教程:不要在成员签名中嵌套generics类型分享


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

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

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

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

请记住,我对表达式树不是很熟悉,尽管我对匿名方法,委托和lambda非常熟悉。

任何帮助将不胜感激。

public static DataServiceQuery Expand(this DataServiceQuery source, Expression<Func> 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 (eg 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(this Collection source, Expression<Func> 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(this TSource source, Expression<Func> 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>参数的警告。 但是,对于此方法,简化类型没有意义,而是应使用属性禁止警告或从规则集中完全删除规则。


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

上述就是C#学习教程:不要在成员签名中嵌套generics类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 public void F(Dictionary>> dictionary); 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1269743.html

(0)
上一篇 1天前
下一篇 1天前

精彩推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注