简体   繁体   English

C#使用泛型方法

[英]c# using generic method

I would like to know how I can solve the following problem: 我想知道如何解决以下问题:

I have a generic Method which return response data. 我有一个返回响应数据的通用方法。

public class Request
{
    public T PostRequest<T>(string Ressource, T ObjTOPost) where T : new()
    {
        var request = new RestRequest(Ressource, Method.POST);

        var client = new RestClient("https://api.net/api/v1/");

        request.XmlSerializer = new RestSharp.Serializers.XmlSerializer();
        request.RequestFormat = DataFormat.Xml;

        request.AddXmlBody(ObjTOPost, "http://api.net");
        var response = client.Execute<T>(request);

        return response.Data;
    }
}

When I call the Method like this it says that LoginXML cannot be converted to UserXML. 当我这样调用方法时,它说LoginXML无法转换为UserXML。 I dont get why because LoginXML is a parameter only for serializing my properties for the post request. 我不明白为什么,因为LoginXML是仅用于序列化我的职位属性的参数。

var login = new LoginXML();
        login.username = tbUsername.Text;
        login.password = tbPassword.Text;

Request req = new Request();
UserXML user = req.PostRequest("sessions", login);

Here are my LoginXML and UserXML 这是我的LoginXML和UserXML

 [SerializeAs(Name = "login")]
public class LoginXML
{
    private string user;
    private string pw;
    private string ID;

    [DeserializeAs(Name = "id")]
    public string sessionID { get; set; }
    [SerializeAs(Name = "username")]
    public string username
    {
        get
        {
            return this.user;
        }
        set
        {
            this.user = value.Trim();
        }
    }
    [SerializeAs(Name = "password")]
    public string password
    {
        get { return this.pw; }
        set { this.pw = value.Trim(); }
    }
}

public class UserXML
{
    private string ID;

    [DeserializeAs(Name = "id")]
    public string sessionID {get;set;}
}
public T PostRequest<T>(string Ressource, T ObjTOPost) where T : new()

Your PostRequest return type is T , which is similar to T ObjTOPost . 您的PostRequest返回类型为T ,类似于T ObjTOPost Since you passed in a LoginXML instance into req.PostRequest("sessions", login); 由于您将LoginXML实例传递给LoginXML req.PostRequest("sessions", login); it'd assume your return type will also be LoginXML since they are both T. 假设您的返回类型也都是LoginXML因为它们都是T。

public T PostRequest<T>(string Ressource, T ObjTOPost) where T : new()
{
    // Some code
    return response.Data;
}

Here you are trying to return the Data retrieved from your rest request which is a UserXML type. 在这里,您尝试返回从其余请求中检索到的数据,它是UserXML类型。 However in this case your method return type is LoginXML but you are returning a UserXML object instead, hence the error. 但是,在这种情况下,您的方法返回类型为LoginXML但是您返回的是UserXML对象,因此会出现错误。

One way is to simply return object but it requires the trouble of casting every object and there will be potentially bad casts that causes exceptions. 一种方法是简单地返回对象,但它需要强制转换每个对象,并且存在可能导致异常的不良强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM