Wrote a very ugly function Public void GetDataFromFile (int Type, out List The List, the out T Object) { The switch (Type) { Case 1: Object=(T) FormatterInformation. Deserialize (FS); List=null; break; Case 2: List=FormatterInformation. Deserialize (FS) as List; The Object=default; break; Default: List=null; The Object=default; break; }
Is deserialized object, an object or a group of list are How to use a generic function out? Serialization of what it would be, can be overloaded, Public void SaveDataToFile & lt; T> (List ListInput) { FormatterInformation. Serialize (FS, ListInput); } Public void SaveDataToFile Object (T) { FormatterInformation. Serialize (FS, Object); }
CodePudding user response:
Don't know your specific methods, and see if you send the code to the following should be ok, T can List<> Don't have to specifically define List< collection or object; T> Public void GetDataFromFile (out T Object) { Object=(T) FormatterInformation. Deserialize (FS); } The Collection GetDataFromFile (out List Obj) Object GetDataFromFile (out XXX obj)
CodePudding user response:
Same serialized also need only one method, do not need to reload Public void SaveDataToFile object (object) { FormatterInformation. Serialize (FS, Object); } Or Public void SaveDataToFile Object (T) { FormatterInformation. Serialize (FS, Object); } You can meet the requirements, do not need SaveDataToFile & lt; T> (List ListInput)