加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 返回同一对象的类中的泛型方法

发布时间:2020-12-16 01:27:18 所属栏目:百科 来源:网络整理
导读:我有一个类,我想为该类添加一个泛型方法,每次调用时都返回一个相同的对象.但方法参数可以是不同的对象.无论参数是什么,但方法应始终返回相同的对象类型. 创建此方法的目的是我想调用一个API,我需要将JSon序列化对象发送给它.每次我调用API时,它都会在他们的
我有一个类,我想为该类添加一个泛型方法,每次调用时都返回一个相同的对象.但方法参数可以是不同的对象.无论参数是什么,但方法应始终返回相同的对象类型.

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

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

这是泛型的可能吗?或者任何其他方式使这简单而有效?

请参阅下面的示例代码.

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 = "022222111"
        },PrimaryAddress = new CustomerAddress()
        {
            Street = "Hill street",ZipCode = "16962",City = "New york",Country = "USA"
        },BillingAddress = new CustomerAddress()
        {
            Street = "Hill street",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; }
}

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

解决方法

首先编写看起来像你的泛型类

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",CustomerContactPhone = "022222111"
                    },PrimaryAddress = new CustomerAddress()
                    {
                        Street = "Hill street",Country = "USA"
                    },BillingAddress = new CustomerAddress()
                    {
                        Street = "Hill street",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,Country = "USA"
                    }
                };
            }


            return customer;
        }


    }

您可以根据需要在上述方法中使用if-if或if-else-if.

从你的api控制器动作调用你的CreateCustomer泛型方法

学生数据或教师数据来自您的前端

[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);
    }

结果

老师作为客户:

enter image description here

学生作为客户:

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读