Implement ISerializable
- 2 minutes to read
In This Article
Implements the ISerializable interface in the current class.
#Availability
From the context menus or via shortcuts:
- when the edit cursor or caret is on a declaration of a class containing fields or properties, provided that the class does not implement ISerializable.
#Example
public class │MyClass
{
public string StringValue { get; set; }
public string IntValue { get; set; }
}
Public Class │TestClass
Public Property StringValue() As String
Public Property IntValue() As String
End Class
Result:
public class MyClass : ISerializable
{
protected MyClass(SerializationInfo info, StreamingContext context)
{
if(info == null)
return;
StringValue = info.GetString("StringValue");
IntValue = info.GetString("IntValue");
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if(info == null)
return;
info.AddValue("StringValue", StringValue);
info.AddValue("IntValue", IntValue);
}
public string StringValue { get; set; }
public string IntValue { get; set; }
}
Public Class TestClass
Implements ISerializable
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
If info Is Nothing Then
Return
End If
StringValue = info.GetString("StringValue")
IntValue = info.GetString("IntValue")
End Sub
Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializable.GetObjectData
If info Is Nothing Then
Return
End If
info.AddValue("StringValue", StringValue)
info.AddValue("IntValue", IntValue)
End Sub
Public Property StringValue() As String
Public Property IntValue() As String
End Class