java - How to Make Object.class Type Generic -


i create utility method takes 2 objects parameters , marshalls xml them. below code works if use actual object type parameter, how make generic? below wont compile since can't resolve object type. ideas?

 public static string getxml(object from){     stringwriter xml = new stringwriter();     try {       jaxbcontext.newinstance(from.class).createmarshaller().marshal(from, xml);     } catch (exception e) {       e.printstacktrace();     }     return xml.tostring();   } 

you can class instance of object so

jaxbcontext.newinstance(from.getclass()) //... 

this explained in ojbect#getclass() javadoc.

returns runtime class of object.

note there aren't generics directly involved in code snippet.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -