Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerics problem. I'm just not understanding how to make a generic method
    primarykey
    data
    text
    <p>I've got some XML, stored in a table. I know what object it is supposed to Deserialize to, but I have many types of objects.</p> <p>So, I've got this code, and it works, but it is very specific to 1 type of object. I want to make it generic to accept one of many types of object.</p> <pre><code>Type _type = typeof(ReynoldsRCI.Services.rey_SvcApptView) XmlSerializer ser = new XmlSerializer(_type); object sck; using (StringReader reader = new StringReader(_msg.RawText)) { using (XmlTextReader xreader = new XmlTextReader(reader)) { sck = ser.Deserialize(xreader); } } </code></pre> <p>However, what I would LOVE to have is a 1 line method call where I pass it the type of object and the text, and it would deserialize it and return it to the caller for use. But I am completely an idiot when it comes to generics, so the code I have just isn't even compiling.</p> <pre><code>public T DoStuff&lt;T&gt;(T _type, string RawText) { // Doesn't like this line. Error "The best overloaded method match for 'System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type)' has some invalid arguments" XmlSerializer ser = new XmlSerializer(_type); object retObj; using (StringReader reader = new StringReader(RawText)) { using (XmlTextReader xreader = new XmlTextReader(reader)) { retObj = ser.Deserialize(xreader); } } // Doesn't like this line, either "The type or namespace name '_type' could not be found" return (_type)retObj; } </code></pre> <p>I'm obviously missing something basic when it comes to Generics, and I can't piece it together. Can someone help me out with a quick pointer?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload