2

I have an interface

public interface  IIdentity<T>
{
    T GetUser();
}

I have a base class that implements the Interface as an abstract method

public abstract class BaseUser<T> : IIdentity<T>
{
    public string UserId { get; set; }
    public string AuthType { get; set; }
    public List<Claim> Claims { get; set; }
    public abstract T GetUser();
}

In the class that inherits the base class

 public class JwtUser : BaseUser
 {
     public string Sub { get; set; }
 }

I get an error using the generic type BaseUser requires 1 argument, what do i do here, basically I'd like my user to inherit shared properties from the base class which it does (i think) and to implement the generic method from the base class as I'm going to have different types of users (JWT/Windows etc) I need to abstract away the getUsers method, hope that makes sense ?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
fuzzybear
  • 2,325
  • 3
  • 23
  • 45

2 Answers2

3

You have to ways to implement this, both require to set the generic in BaseUser.

You could expose that generic:

public class JwtUser<T> : BaseUser<T>
{
    public string Sub { get; set; }
}

Or, just set the generic:

public class JwtUser : BaseUser<JwtUser>
{
    public string Sub { get; set; }
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
1

it should be like , for Ref : Generic Classes (C# Programming Guide)

public class JwtUser<User> : BaseUser<User>
 {
     public string Sub { get; set; }
 }

or

public class JwtUser<T> : BaseUser<T>
 {
     public string Sub { get; set; }
 }

and when create instace

var jwtUser =new JwtUser<User> ();

or

  class JwtUser: BaseUser<JwtUser> { }

In all way at the end you must need to specify value for T template as its generic.

For example if you take List<T> for using it you must need to intialize with proper type like if interger list then List<int> intlist = new List<int>();

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263