c# - VB.NET, make a function with return type generic? -
currently have written function deserialize xml seen below. how change don't have replace type every time want serialize object type ? current object type ctoolconfig. how make function generic ?
public shared function deserializefromxml(byref strfilenameandpath string) xmlhandler.xmlserialization.ctoolconfig dim deserializer new system.xml.serialization.xmlserializer(gettype(ctoolconfig)) dim srencodingreader io.streamreader = new io.streamreader(strfilenameandpath, system.text.encoding.utf8) dim thisfacility ctoolconfig thisfacility = directcast(deserializer.deserialize(srencodingreader), ctoolconfig) srencodingreader.close() srencodingreader.dispose() return thisfacility end function public shared function deserializefromxml1(byref strfilenameandpath string) system.collections.generic.list(of xmlhandler.xmlserialization.ctoolconfig) dim deserializer new system.xml.serialization.xmlserializer(gettype(system.collections.generic.list(of ctoolconfig))) dim srencodingreader io.streamreader = new io.streamreader(strfilenameandpath, system.text.encoding.utf8) dim facilitylist system.collections.generic.list(of ctoolconfig) facilitylist = directcast(deserializer.deserialize(srencodingreader), system.collections.generic.list(of ctoolconfig)) srencodingreader.close() srencodingreader.dispose() return facilitylist end function
is mean?
public shared function deserializefromxml(of t)(byref strfilenameandpath string) t dim deserializer new system.xml.serialization.xmlserializer(gettype(t)) dim srencodingreader io.streamreader = new io.streamreader(strfilenameandpath, system.text.encoding.utf8) dim thisfacility t thisfacility = directcast(deserializer.deserialize(srencodingreader), t) srencodingreader.close() srencodingreader.dispose() return thisfacility end function public shared function deserializefromxml1(of t)(byref strfilenameandpath string) system.collections.generic.list(of t) dim deserializer new system.xml.serialization.xmlserializer(gettype(system.collections.generic.list(of t))) dim srencodingreader io.streamreader = new io.streamreader(strfilenameandpath, system.text.encoding.utf8) dim facilitylist system.collections.generic.list(of t) facilitylist = directcast(deserializer.deserialize(srencodingreader), system.collections.generic.list(of t)) srencodingreader.close() srencodingreader.dispose() return facilitylist end function
note can put constraints on t, such as:
public shared function deserializefromxml(of t class)
and put multiple constraints, such as:
public shared function deserializefromxml(of t {class, new, idisposable})
Comments
Post a Comment