C# 类中返回相同对象的泛型方法

C# 类中返回相同对象的泛型方法,c#,asp.net,generics,C#,Asp.net,Generics,我有一个类,我想向该类添加一个泛型方法,该类每次调用时都返回相同的对象。但方法参数可以是不同的对象。无论参数是什么,但方法应始终返回相同的对象类型 创建此方法的目的是,我假设要调用一个API,我需要向该API发送JSon序列化对象。每次我调用API时,它都会在他们的服务中创建一个新客户。API服务只有一个客户类型对象。但在我的应用程序中,我有两种类型的对象(例如:学生、教师),API不关心我是发送学生对象还是教师对象。这两个对象都是API透视图中的客户 因此,每当我调用API时,我都需要创建公共

我有一个类,我想向该类添加一个泛型方法,该类每次调用时都返回相同的对象。但方法参数可以是不同的对象。无论参数是什么,但方法应始终返回相同的对象类型

创建此方法的目的是,我假设要调用一个API,我需要向该API发送JSon序列化对象。每次我调用API时,它都会在他们的服务中创建一个新客户。API服务只有一个客户类型对象。但在我的应用程序中,我有两种类型的对象(例如:学生、教师),API不关心我是发送学生对象还是教师对象。这两个对象都是API透视图中的客户

因此,每当我调用API时,我都需要创建公共客户对象来传递给API。但我的应用程序中有两个对象,我想编写一个方法,该方法同时接受Student和Teacher对象,但返回一个customer对象

这在泛型中可能吗?或者有其他方法使这变得简单高效

请参阅下面的示例代码

 public static Customer CreateCustomer<T>(T data)
 {
    var customer = new Customer()
    {
        CustomerNo = 1,
        CustomerName = "Test",

        CustomerContact = new CustomerContact()
        {
            CustomerContactName = "Test",
            CustomerContactEmail = "test@test.com",
            CustomerContactPhone = "011111111"
        },
        PrimaryAddress = new CustomerAddress()
        {
            Street = "Hill street",
            ZipCode = "16962",
            City = "New york",
            Country = "USA"
        },
        BillingAddress = new CustomerAddress()
        {
            Street = "Hill street",
            ZipCode = "16962",
            City = "New york",
            Country = "USA"
        }
    };
    return customer;
}

public class Teacher 
{
    public long TeacherID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public Subject Subjects { get; set; }

    public string Email{ get; set; }

    public string ContactNO{ get; set; }

    public Address PrimaryAddress { get; set; }

    public Address SecondaryAddress { get; set; }
}

public class Student 
{
    public long StudentID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }

    public string Email{ get; set; }

    public string ContactNO{ get; set; }

    public Address PrimaryAddress { get; set; }

    public Address SecondaryAddress { get; set; }

    public string Grade { get; set; }

    public int Level { get; set; }
}
公共静态客户CreateCustomer(T数据)
{
var customer=新客户()
{
CustomerNo=1,
CustomerName=“测试”,
CustomerContact=新CustomerContact()
{
CustomerContactName=“测试”,
CustomerContactEmail=”test@test.com",
CustomerContactPhone=“0111111”
},
PrimaryAddress=新CustomerAddress()
{
街道=“山街”,
ZipCode=“16962”,
City=“纽约”,
Country=“美国”
},
BillingAddress=新客户地址()
{
街道=“山街”,
ZipCode=“16962”,
City=“纽约”,
Country=“美国”
}
};
退货客户;
}
公共班主任
{
公共长教师ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共主题主题{get;set;}
公共字符串电子邮件{get;set;}
公共字符串ContactNO{get;set;}
公共地址主地址{get;set;}
公共地址第二地址{get;set;}
}
公立班学生
{
公共长学生ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共整数{get;set;}
公共字符串电子邮件{get;set;}
公共字符串ContactNO{get;set;}
公共地址主地址{get;set;}
公共地址第二地址{get;set;}
公共字符串等级{get;set;}
公共整数级别{get;set;}
}

T数据可以是学生或教师。我想替换此数据对象中的硬编码值。有可能吗?

有可能,但您需要提供有关T的更多信息,并更改退货类型:

public T Create<T>(T data) where T : class
{
    return new someType() as T;
}
public T创建(T数据),其中T:class
{
返回新的someType()作为T;
}

如果
教师
学生
都可以成为
客户
,并且这两个类共享相同的数据以成为
客户
,那么最好将这些属性提取到基类或接口中

例如:

public class Person
{
}

public class Student : Person
{
}

public class Teacher : Person
{
}

public static Customer CreateCustomer(Person data)
{

}

您可以从客户类继承教师和学生。 现在,客户必须包含这两个类的公共信息

abstract class Customer {...}
class Teacher : Customer {...}
class Student : Customer {...}
然后你可以把老师和学生当作顾客

Customer c1 = new Student();
Customer c2 = new Teacher();

Api.Call(c1);
Api.Call(c2);
Api.Call应该如下所示:

// in Api class

public void Call(Customer c)
{
  //call the Api
}

您可以使用if接口或基类,该基类应该具有Customer类所具有的所有属性。可以在派生类中放置其他属性。然后您的方法将需要基类或接口


既然您已经提到您将调用API并发布此JSON,那么您可能还想看看NewtonSoft的
JsonPropertyAttribute
(假设您正在使用它)。这样,您甚至不需要创建
Customer
对象。您可以简单地将类修饰成所需的JSON格式进行序列化。HTH

首先,编写类似

public class CustomerService<T> where T : class
    {    
        public static Customer CreateCustomer(T data)
        {
            Customer customer = new Customer();

            if (typeof(T) == typeof(Student))  //We just check here is data comes from api is of type Student
            {
                Student student = (Student)(object)data; //then cast this data to Student

                customer = new Customer()
                {
                    CustomerNo = student.StudentID, // Convert.ToInt32(student.StudentID),
                    CustomerName = student.FirstName,

                    //Assign all your remaining customer properties with desired values
                    CustomerContact = new CustomerContact()
                    {
                        CustomerContactName = "Test",
                        CustomerContactEmail = "test@test.com",
                        CustomerContactPhone = "011111111"
                    },
                    PrimaryAddress = new CustomerAddress()
                    {
                        Street = "Hill street",
                        ZipCode = "16962",
                        City = "New york",
                        Country = "USA"
                    },
                    BillingAddress = new CustomerAddress()
                    {
                        Street = "Hill street",
                        ZipCode = "16962",
                        City = "New york",
                        Country = "USA"
                    }
                };
            }

            if (typeof(T) == typeof(Teacher))  //We just check here is data comes from api is of type Teacher
            {
                Teacher teacher = (Teacher)(object)data; //then cast this data to Teacher 

                customer = new Customer()
                {
                    CustomerNo =  teacher.TeacherID,  // Convert.ToInt32(teacher.TeacherID),
                    CustomerName = teacher.FirstName,

                    //Assign all your remaining customer properties with desired values
                    CustomerContact = new CustomerContact()
                    {
                        CustomerContactName = "Test",
                        CustomerContactEmail = "test@test.com",
                        CustomerContactPhone = "011111111"
                    },
                    PrimaryAddress = new CustomerAddress()
                    {
                        Street = "Hill street",
                        ZipCode = "16962",
                        City = "New york",
                        Country = "USA"
                    },
                    BillingAddress = new CustomerAddress()
                    {
                        Street = "Hill street",
                        ZipCode = "16962",
                        City = "New york",
                        Country = "USA"
                    }
                };
            }


            return customer;
        }


    }
公共类CustomerService,其中T:class
{    
公共静态客户CreateCustomer(T数据)
{
客户=新客户();
if(typeof(T)=typeof(Student))//我们只需在这里检查数据是否来自api,是否为Student类型
{
学生=(学生)(对象)数据;//然后将此数据强制转换为学生
客户=新客户()
{
CustomerNo=student.StudentID,//Convert.ToInt32(student.StudentID),
CustomerName=student.FirstName,
//为所有剩余的客户属性指定所需的值
CustomerContact=新CustomerContact()
{
CustomerContactName=“测试”,
CustomerContactEmail=”test@test.com",
CustomerContactPhone=“0111111”
},
PrimaryAddress=新CustomerAddress()
{
街道=“山街”,
ZipCode=“16962”,
City=“纽约”,
Country=“美国”
},
BillingAddress=新客户地址()
{
街道=“山街”,
ZipCode=“16962”,
City=“纽约”,
Country=“美国”
}
};
}
if(typeof(T)=typeof(Teacher))//我们只需检查这里的数据来自api,类型为Teacher
{
教师=(教师)(对象)数据;//然后将此数据强制转换为教师
 [HttpPost]
 //public IHttpActionResult GetCustomer([HttpPost]Teacher teacher)
 public IHttpActionResult GetCustomer()
    {
        Teacher teacher = new Teacher { TeacherID = 12, FirstName = "Vijay" };  //this teacher data comes from front end or from caller of this api
        Customer customer1 = CustomerService<Teacher>.CreateCustomer(teacher);            

        return Ok(customer1);
    }
 [HttpPost]
 //public IHttpActionResult GetCustomer([HttpPost]Student student)
 public IHttpActionResult GetCustomer()
    {           
        Student student = new Student { StudentID = 11, FirstName = "Kunal" };  //this student data comes from front end or from caller of this api
        Customer customer2 = CustomerService<Student>.CreateCustomer(student);

        return Ok(customer2);
    }