2 years ago
#62673
oyeesh
C# How to return an instance of subclass based on the type
I have the following structure
public interface IUser
{
Guid Id { get; set; }
string Name { get; set; }
TUserType GetTypeUser<TUserType>()
where TUserType : class, IUser;
}
public class UserBase : IUser
{
public Guid Id { get; set; }
public string Name { get; set; }
public GetTypeUser<TUserType>()
{
// implement something so that based on the given type return the correct subclass
}
}
public class FullTimeUser : UserBase, IFullTimeUser
{
public String FullTimeSpecific { get; set;}
}
public class PartTimeUser : UserBase, IPartTimeUser
{
public String PartTimeSpecific { get; set;}
}
How do I implement GetTypeUser
on the base class so that I would be able to pass in the type of subclass I need so that I get an instance of the correct subclass?
c#
types
subclass
0 Answers
Your Answer