Compilation error 'IUser.UserName' in explicit interface declaration is not a member of interface - why am I gettings this?
Asked Answered
O

2

8

I'm using the Asp.Net Identity framework, and I have a User class which looks like this:

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

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

I've just upgraded to version 2 of Asp.Net Identity Framework, and I've started getting the compilation error "'IUser.UserName' in explicit interface declaration is not a member of interface". Everything was fine before.

What is happening?

Outstrip answered 13/5, 2014 at 11:13 Comment(0)
O
12

Two things contributed to this:

  1. You're implementing the UserName property of the IUser interface explicitly
  2. In version 2 of the Asp.Net Identity Framework, the definition of the IUser interface changed. The UserName property is now defined on a generic IUser interface, from which the non-generic IUser interface inherits.

When you implement interfaces explicitly, C# expects you to qualify the member name with the name of the least-derived interface, not the name of an interface which may inherit from it.

To fix your code, you need to do this:

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

Bonus Example

Here's a complete example which generates the same error message:

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

public interface Inherited : Base
{

}

public class Implementor : Inherited
{
    string Inherited.MyProperty { get; set; }
}
Outstrip answered 13/5, 2014 at 11:13 Comment(1)
I'm having the same problem and tried this but i have no luck.Any reference/link to this convention?Aether
D
0

To anyone coming here from Google, you might get this error because you had one of your type definitions wrong.

interface IItem {
    bool Quantity { get; }
}
class Item : IItem {
    int IItem.Quantity { get { return ..
Dulcine answered 28/1, 2023 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.