c# - Visual Studio 2015 扩展方法调用作为方法组

标签 c# visual-studio-2015 extension-methods method-group

我有一个类似的扩展方法

public static void RemoveDetail<TMaster, TChild>(this TMaster master, TChild child)
        where TMaster : class, IMaster<TChild>
        where TChild : class, IDetail<TMaster>;

我有两个类(class)

public class Principal : IMaster<Permission>
{
        public virtual IEnumerable<Permission> Permissions { get; }
}

public class Permission : IDetail<Principal>

我从方法 public static void Foreach<T>(this IEnumerable<T> source, Action<T> action); 接受的操作调用 RemoveDetail :

aPrincipal.Permissions.Foreach(x => aPrincipal.RemoveDetail(x));

ReSharper 建议我用方法组替换此调用

aPrincipal.Permissions.Foreach(aPrincipal.RemoveDetail);

这在 VS2013 和以前的版本中运行良好,但在 VS2015 中编译失败

'Principal' does not contain a definition for 'RemoveDetail' and no extension method 'RemoveDetail' accepting a first argument of type 'Principal' could be found (are you missing a using directive or an assembly reference?)

有人有什么建议吗? 我是否必须更新所有用法并制作 ReSharper 以避免这种替换?

最佳答案

我能够重现你的问题

public interface IDetail<T>
{

}

public interface IMaster<T>
{

}

public class MyClass
{
    public void method()
    {
        Principal aPrincipal = new Principal();
        aPrincipal.Permissions.Foreach(x => aPrincipal.RemoveDetail(x)); // No suggestion from resharper
    }
}

public class Permission : IDetail<Principal>
{

}

public class Principal : IMaster<Permission>
{
    public virtual IEnumerable<Permission> Permissions { get; }
}

public static class Class
{
    public static void RemoveDetail<TMaster, TChild>(this TMaster master, TChild child)
        where TMaster : class, IMaster<TChild>
        where TChild : class, IDetail<TMaster>
    {

    }

    public static void Foreach<T>(this IEnumerable<T> source, Action<T> action)
    {

    }
}

resharper 没有提出任何建议。所以可能你必须将你的 Resharper 更新到更新的版本。可能是一个错误,现在已修复。

如果这与您的做法不一样。那么你必须输入更多信息。

当我尝试将其转换为方法组时,编译器也会给出同样的错误。所以这个问题可能仍然存在:Why Compiler cant do this?

编辑:

要解决此问题,您必须调用 RemoveDetail来自 Principal 内部的方法。所以让你的 Principal 类像这样。

public class Principal : IMaster<Permission>
{
    public virtual IEnumerable<Permission> Permissions { get; }

    public void RemoveDetail(Permission p)
    {
        Class.RemoveDetail(this, p);
    }
}

我认为编译器内部存在某种无法识别的模棱两可(可能是错误)RemoveDetail方法。编译器如何尝试在 Principal 中找到它.所以你可以通过创建 RemoveDetail 来修复它里面Principal并调用静态 RemoveDetail从那里开始。

编辑 2:

问题是泛型类型约束。

where TMaster : class, IMaster<TChild>

这使得编译器查看实现了 IMaster<TChild> 的类那就是Principal .如果您删除此 where 子句,它将解决问题。否则编译器期望它应该是 Principal .

如果您使用 lambda,则可以消除这种歧义。

aPrincipal.RemoveDetail // compiler expects property/field/method in Principal
                        // but compiler forgets method group and static generic method!
x => aPrincipal.RemoveDetail(x) // this is no more like property or field
                                //but a method that is considered static generic method!

所以这是一个 C#6 错误

关于c# - Visual Studio 2015 扩展方法调用作为方法组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32350426/

相关文章:

c# - 为什么 NetworkStream.ReadAsync 没有超时?

c# - Visual Studio 生成后事件返回错误 MSB3073

javascript - TypeScript "Compile on save"功能在 Visual Studio 2015 中不起作用

asp.net-mvc - 如何在ASP.NET MVC View 中使用扩展方法?

c# - 当当前任务阻塞时,如何在 .NET 中安排另一项任务?

c# - 使用 CaSTLe Windsor 进行依赖注入(inject)的多态性

javascript - 如何设置 VS2015 在我搞乱 JavaScript 时抛出错误?

子进程(通过 CreateProcess)在带有重定向 stdout 和 stdin 的 getch() 上停止

c# - Silverlight中没有List的Find扩展方法?

c# - 通用列表的通用扩展方法