简体   繁体   English

如何在C#中传递和使用通用对象?

[英]How to pass and use a generic object in c#?

I have multiple methods that each return an Object. 我有多个方法,每个方法都返回一个对象。

public objA myCall(string[] args)
{           
    webAPI myAPI = new webAPI();
    returnData = myAPI.callApi("http://localhsot/api", args , "POST");

    XmlSerializer serializer = new XmlSerializer(typeof(myObj));

    using (TextReader reader = new StringReader(returnData))
    {
        objA result = (objA)serializer.Deserialize(reader);

        return result;
    }
}

And

public objB myCall(string[] args)
{           
  webAPI myAPI = new webAPI();
  returnData = myAPI.callApi("http://localhsot/api", args , "POST");

  XmlSerializer serializer = new XmlSerializer(typeof(myObj));

  using (TextReader reader = new StringReader(returnData))
  {
      objB result = (objB)serializer.Deserialize(reader);

      return result;
  }
}

What I would like to do is consolidate these into one method using generics. 我想做的就是使用泛型将它们合并为一种方法。 This way I can pass in the object that I would like returned. 这样,我可以传递我想要返回的对象。 I've never used generics before and need a little help. 我以前从未使用过泛型,因此需要一些帮助。 This is what I have tried: 这是我尝试过的:

public T myCall<T>(ref T myObj, string[] args)
{           
  webAPI myAPI = new webAPI();
  returnData = myAPI.callApi("http://localhsot/api", args , "POST");

  XmlSerializer serializer = new XmlSerializer(typeof(myObj));

  using (TextReader reader = new StringReader(returnData))
  {
      myObj result = (myObj)serializer.Deserialize(reader);

      return result;
  }
}

But when I put this into Visual Studio, I get get an error saying that "myObj" is a variable but is used like a type. 但是,当我将其放入Visual Studio时,我得到一个错误,说“ myObj”是一个变量,但像类型一样使用。 If you have had experience with this and are willing to help, I would appreciate it. 如果您有此经验并愿意提供帮助,我将不胜感激。

You are almost there 你快到了

public T myCall<T>(string[] args)
{           
  webAPI myAPI = new webAPI();
  returnData = myAPI.callApi("http://localhsot/api", args , "POST");

  XmlSerializer serializer = new XmlSerializer(typeof(T));

  using (TextReader reader = new StringReader(returnData))
  {
      T result = (T)serializer.Deserialize(reader);

      return result;
  }
}

And then you call it by passing the type as the generic constraint. 然后通过将类型作为通用约束传递来调用它。

var result = myCall<objA>(someArguments);

On a side note (and also my opinion) objA is not a good name for a type. objA (也是我的观点), objA不是类型的好名字。

You should remove from parameters myObj and change it in the body of method to T like this 您应该从参数myObj删除并将其在方法主体中更改为T如下所示

public T myCall<T>(string[] args)
{           
  webAPI myAPI = new webAPI();
  returnData = myAPI.callApi("http://localhsot/api", args , "POST");

  XmlSerializer serializer = new XmlSerializer(typeof(T));

  using (TextReader reader = new StringReader(returnData))
  {
      T result = (T)serializer.Deserialize(reader);

      return result;
  }
}

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

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