gpt4 book ai didi

c# - 显式接口(interface)声明中的编译错误 'IUser.UserName' 不是接口(interface)的成员 - 为什么我得到这个?

转载 作者:太空狗 更新时间:2023-10-30 00:24:35 25 4
gpt4 key购买 nike

我正在使用 Asp.Net Identity 框架,我有一个如下所示的用户类:

public class User : IUser
{
public string Id {get; private set;}
public string Email {get; set;}

string IUser.UserName { get { return Email;} set { Email = value;}}
}

我刚刚升级到 Asp.Net Identity Framework 的第 2 版,我开始收到编译错误“'IUser.UserName' in explicit interface declaration is not a member of interface ”。之前一切都很好。

发生了什么事?

最佳答案

有两件事促成了这一点:

  1. 您正在显式实现 IUser 接口(interface)的 UserName 属性
  2. 在 Asp.Net Identity Framework 版本 2 中,IUser 接口(interface)的定义发生了变化。 UserName 属性现在在通用 IUser 接口(interface)上定义,非通用 IUser 接口(interface)从该接口(interface)继承。

当您显式实现接口(interface)时,C# 希望您使用派生最少的接口(interface)的名称来限定成员名称,而不是可以从它继承的接口(interface)的名称。

要修复您的代码,您需要这样做:

public class User : IUser
{
string IUser<string>.UserName { get { return Email;} set { Email = value;}}
}

奖励示例

这是一个生成相同错误消息的完整示例:

public interface Base
{
string MyProperty { get; set; }
}

public interface Inherited : Base
{

}

public class Implementor : Inherited
{
string Inherited.MyProperty { get; set; }
}

关于c# - 显式接口(interface)声明中的编译错误 'IUser.UserName' 不是接口(interface)的成员 - 为什么我得到这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23629668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com