Home > Net >  C # generic function asked again
C # generic function asked again

Time:03-29

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)

CodePudding user response:

The
refer to the original poster tucker heather's reply:
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);
}


Direct inheritance LocalizedTypeGetter
 
Public class LocalizedTypeGetterEx: LocalizedTypeGetter
{
Public override Type BindToType (string assemblyName, string typeName)
{
If (typeName. The Contains (" List ")==true & amp; & TypeName. The Contains (typeof (Student). Name)==true)//if it is a generic collections
{
Return typeof (List}

Return to base. BindToType (assemblyName typeName);
}
}

//or using the XmlSerializer, will copy the object serialization to flow, and then a new object is obtained by deserialization,
///& lt; Summary>
///class
///& lt;/summary>
Public class SerializLog
{
//1. Use the complete reference object serialization way deep copy is the best way to
//2. The Clone method below, I need to use the generic object as a parameter, there need to be behind the Clone bring & lt; T> Statement, otherwise the compiler error

Public static T Clone RealObject) (T/T to be serialized objects
{
Using (Stream Stream=new MemoryStream ())//initializes a Stream objects
{
XmlSerializer serializer=new XmlSerializer (typeof (T));//to be serialized objects serialized to XML document (the Formatter)
Serializer. Serialize (stream, realObject);//after the sequence of the object is written to the stream
Stream. Seek (0, SeekOrigin. Begin);
Return (T) serializer. Deserialize (stream);//get new object deserialization
}
}
}


  •  Tags:  
  • C#
  • Related