如何使C#方法泛型,以便不同的类可以使用它

如何使C#方法泛型,以便不同的类可以使用它,c#,.net,linq,generics,refactoring,C#,.net,Linq,Generics,Refactoring,我怎样才能使这个方法更像一个泛型——可以被不同的类使用 这是我的方法,现在应该由两个类使用,因为现在它只在一个地方调用,输入参数是identidto,现在我应该在另一个地方使用它,在那里我应该作为param class用户传递 以下是方法定义: private IdentificationResponse Identification (IdentDto request) { var customRequestion = new CustomRequest { F

我怎样才能使这个方法更像一个泛型——可以被不同的类使用

这是我的方法,现在应该由两个
类使用,因为现在它只在一个地方调用,输入参数是
identidto
,现在我应该在另一个地方使用它,在那里我应该作为param class
用户传递

以下是方法定义:

private IdentificationResponse Identification (IdentDto request)
{
    var customRequestion = new CustomRequest
    {
        FirstADDRESS = request.Company.Address.FirstAddress,
        SecondADDRESS = request.Company.Address.SecondAddress,
        CompanyTown = request.Company.Address.City,
    };

    if (request.Company.HasUserAdded)
    {
        customRequestion.Name = request.User.Name;
        customRequestion.Surname = request.User.Surname;
    }

    return null; // Not important for this case
}
identto
包含
UserDto
,其中包含
User
,看起来如下:

public class IdentDto
{
    public UserDto User { get; set; }
    public CompanyDto Company { get; set; }
}
public class User 
{
    public string Name { get; set; }

    public string SurName { get; set; }

    public Company Company { get; set; }
}
private IdentificationResponse Identification (IdentDto request)
{
    var customRequestion = new CustomRequest
    {
        FirstADDRESS = request.Company.Address.Address1,
        SecondADDRESS = request.Company.Address.Address2,
        CompanyTown = request.Company.Address.City,
    };

    if (request.Company.HasUserAdded)
    {
        customRequestion.Name = request.Name; // Here I would get issue cuz it's not anymore request.User.Name;
        customRequestion.Surname = request.Surname; // Here I would get only issue cuz it's not anymore request.User.Surname; now it become request.Surname because I would use User class for example..
    }

    return null; // Not important for this case
}
identidto
使用此方法时,此方法可以正常工作,但由于用户本身包含
公司
道具,因此有时会 能够以参数形式接收
User
,用户的
类如下:

public class IdentDto
{
    public UserDto User { get; set; }
    public CompanyDto Company { get; set; }
}
public class User 
{
    public string Name { get; set; }

    public string SurName { get; set; }

    public Company Company { get; set; }
}
private IdentificationResponse Identification (IdentDto request)
{
    var customRequestion = new CustomRequest
    {
        FirstADDRESS = request.Company.Address.Address1,
        SecondADDRESS = request.Company.Address.Address2,
        CompanyTown = request.Company.Address.City,
    };

    if (request.Company.HasUserAdded)
    {
        customRequestion.Name = request.Name; // Here I would get issue cuz it's not anymore request.User.Name;
        customRequestion.Surname = request.Surname; // Here I would get only issue cuz it's not anymore request.User.Surname; now it become request.Surname because I would use User class for example..
    }

    return null; // Not important for this case
}
如果我将参数改为
User
而不是
identto
,那么我可以直接访问
Name
姓氏
,因此代码可能如下所示:

public class IdentDto
{
    public UserDto User { get; set; }
    public CompanyDto Company { get; set; }
}
public class User 
{
    public string Name { get; set; }

    public string SurName { get; set; }

    public Company Company { get; set; }
}
private IdentificationResponse Identification (IdentDto request)
{
    var customRequestion = new CustomRequest
    {
        FirstADDRESS = request.Company.Address.Address1,
        SecondADDRESS = request.Company.Address.Address2,
        CompanyTown = request.Company.Address.City,
    };

    if (request.Company.HasUserAdded)
    {
        customRequestion.Name = request.Name; // Here I would get issue cuz it's not anymore request.User.Name;
        customRequestion.Surname = request.Surname; // Here I would get only issue cuz it's not anymore request.User.Surname; now it become request.Surname because I would use User class for example..
    }

    return null; // Not important for this case
}
所以你们可以看到,若我可以传递用户,而不是直接访问那个参数,但这不是解决方案,因为我不能更改 它喜欢这样,因为其他一些,例如,需要发送
identidto
作为参数

所以我想知道如何重新定义这个方法,使这个方法可以同时处理不同的类

非常感谢


干杯

您可以使用
identidto
User
来实现一个界面,比如:

public interface IMyInterface {

  public Company Company { get; }
  public string Name { get; }
  public string SurName { get; }
}

public class IdentDto : IMyInterface {
    public string Name { get => User.Name; }
    public string SurName { gt => User.SurName; }
}

public class User: IMyInterface { }
然后向方法传递对接口的引用,而不是特定类型的引用。希望有帮助。

简单的方法:

private IdentificationResponse Identification(Company company, string Name, string Surname)
{
 ... Your old logic here ...
}

private IdentificationResponse Identification(IdentDto req) => 
  Identification(req.Company, req.User.Name, req.User.Surname)

private IdentificationResponse Identification(User req) => 
  Identification(req.Company, req.Name, req.SurName)
适配器(通过扩展)方式:

然后,您可以通过
var i=Identification(user.tuserdto())调用您的方法

隐式类型转换方式:

public class User 
{
  public string Name { get; set; }
  public string SurName { get; set; }
  public Company Company { get; set; }
  public static implicit operator IdentDto(User u) =>new IdentDto
        {
          User = new UserDto {Name = u.Name, SurName = u.SurName},
          Company = new CompanyDto { ... }
        };
}

或者最后,您可以重构为公共接口(这是最有组织的方式)。

第二个示例使用与参数相同的“identto”,您是指用户吗?@CyberProgs是的,您是对的,我的misstakeHave User和userdt实现IUser接口{Name,姓氏}。然后让Company和companyd实现ICompany接口{Address1,Address2,City}。然后让你的方法在IUser和I公司进行测试。如果你想为你的identidto和User类提供覆盖,而这些类只是回调到非覆盖中。或者如果这是一次性的,那么只需要2个覆盖(identidto和User),为你的方法提供它需要的6件事情。你可以接受type
object
并使用开关模式匹配,但我不推荐这样做。界面最好。