Home > Net >  C # generics to inherit the parent class subclasses override
C # generics to inherit the parent class subclasses override

Time:02-09

Hi,

Thinking the following code:

This is BaseService.
the content of the cs
 public virtual async Task Add (TViewModel viewModel, TEntity entity) 


This is sysUserInfoService.
the content of the cs
 public class SysUserInfoServices: BaseServices , ISysUserInfoServices 


Including UserViewModel for
 public class UserViewModel: IUserViewModel 
{
Public UserViewModel ()
{

}
}


And has the following UserCreateDto
 public class UserCreateDto: IUserViewModel 



I set rewrite BaseService. The Add method of cs, but I failed...
 public override async Task Add (UserCreateDto UserCreateDto, sysUserInfo entity) 
{

return 0;
}


Tip can't find the suitable method to override a...
Could you tell me how to handle this? Thank you.

CodePudding user response:

The static void Main (string [] args)
{
D D=new D ();
Da Fan (new B1 ());
Da Fan (new B2 ());
Da Fan (new C ());
}

The class A
{
Public virtual int AFan (T T)
{
return 0;
}
}
B1: class C
{

}
The class B2: C
{

}
Class C
{

}
Class D: A
{
Public override int AFan (C of C)
{
return 1;
}

Public override int AFan (B1 B1)//not to find the right way to rewrite
{
Return 2;
}

Public override int AFan (B2 B2)//not to find the right way to rewrite
{
Return 3;
}
}
You can put the BaseServices In UserViewModel instead IUserViewModel
Public override async Task Add (UserCreateDto UserCreateDto, sysUserInfo entity)
Rewrite UserCreateDto IUserViewModel instead,

CodePudding user response:

Generics are BaseService incoming?

 public interface IUserViewModel 
{}
Public class UserViewModel: IUserViewModel
{}
Public class UserCreateDto: IUserViewModel
{}
Public interface ISysUserInfoServices
{}
Public class SysUserInfo
{}

Public class BaseService
{
Public virtual async Task Add (TViewModel viewModel, TEntity entity)
{
return 0;
}

}

Public class SysUserInfoServices: BaseService , ISysUserInfoServices
{
Public override Task Add (UserViewModel viewModel, SysUserInfo entity)
{
Return the base. The Add (viewModel, entity);
}

}

CodePudding user response:

Moderator: hello,

BaseService incoming is...
 public class SysUserInfoServices: BaseServices , ISysUserInfoServices 


refer to the second floor is nu month god reply:
BaseService incoming generics are?

 public interface IUserViewModel 
{}
Public class UserViewModel: IUserViewModel
{}
Public class UserCreateDto: IUserViewModel
{}
Public interface ISysUserInfoServices
{}
Public class SysUserInfo
{}

Public class BaseService
{
Public virtual async Task Add (TViewModel viewModel, TEntity entity)
{
return 0;
}

}

Public class SysUserInfoServices: BaseService , ISysUserInfoServices
{
Public override Task Add (UserViewModel viewModel, SysUserInfo entity)
{
Return the base. The Add (viewModel, entity);
}

}

CodePudding user response:

Nope, make a mistake,


reference 1st floor Yinsri response:
static void Main (string [] args)
{
D D=new D ();
Da Fan (new B1 ());
Da Fan (new B2 ());
Da Fan (new C ());
}

The class A
{
Public virtual int AFan (T T)
{
return 0;
}
}
B1: class C
{

}
The class B2: C
{

}
Class C
{

}
Class D: A
{
Public override int AFan (C of C)
{
return 1;
}

Public override int AFan (B1 B1)//not to find the right way to rewrite
{
Return 2;
}

Public override int AFan (B2 B2)//not to find the right way to rewrite
{
Return 3;
}
}
You can put the BaseServices In UserViewModel instead IUserViewModel
Public override async Task Add (UserCreateDto UserCreateDto, sysUserInfo entity)
Rewrite UserCreateDto IUserViewModel instead,

CodePudding user response:

I'm sorry, here is a little slip of pen...
Actually UserCreateDto is actually UserViewModel inherited from the entity class,
 public class UserCreateDto: UserViewModel 



refer to the second floor is nu month god reply:
BaseService incoming generics are?

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related