コンテキストの状態と追加情報を指定して、StreamingContext クラスの新しいインスタンスを初期化します。
名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
state As StreamingContextStates, _
additional As Object _
)
'使用
Dim state As StreamingContextStates
Dim additional As Object
Dim instance As New StreamingContext(state, additional)
public StreamingContext (
StreamingContextStates state,
Object additional
)
public:
StreamingContext (
StreamingContextStates state,
Object^ additional
)
public StreamingContext (
StreamingContextStates state,
Object additional
)
public function StreamingContext (
state : StreamingContextStates,
additional : Object
)
適用できません。
パラメータ
- state
この StreamingContext の転送元のコンテキストまたは転送先のコンテキストを指定する StreamingContextStates 値のビットごとの組み合わせ。
- additional
StreamingContext に関連付ける追加情報。この情報は、ISerializable を実装する任意のオブジェクトまたは任意のシリアル化サロゲートで使用できます。ほとんどのユーザーは、このパラメータを設定する必要はありません。
使用例
state パラメータと additional パラメータを使用して、StreamingContext クラスのインスタンスを作成する例を次に示します。
Imports System
Imports System.Text
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Security.Permissions
Class Program
Shared Sub Main(ByVal args() As String)
Try
SerializeAndDeserialize()
Catch exc As System.Exception
Console.WriteLine(exc.Message)
Finally
Console.WriteLine("Press <Enter> to exit....")
Console.ReadLine()
End Try
End Sub
Shared Sub SerializeAndDeserialize()
Dim myObject As Object = DateTime.Now
' Create a StreamingContext that includes a
' a DateTime.
Dim sc As New StreamingContext(StreamingContextStates.CrossProcess, myObject)
Dim bf As New BinaryFormatter(Nothing, sc)
Dim ms As New MemoryStream(New Byte(2047) {})
bf.Serialize(ms, New [MyClass]())
ms.Seek(0, SeekOrigin.Begin)
Dim f As [MyClass] = CType(bf.Deserialize(ms), [MyClass])
Console.WriteLine(vbTab + " MinValue: {0} " + vbLf + vbTab + " MaxValue: {1}", f.MinValue, f.MaxValue)
Console.WriteLine("StreamingContext.State: {0}", sc.State)
Dim myDateTime As DateTime = CType(sc.Context, DateTime)
Console.WriteLine("StreamingContext.Context: {0}", myDateTime.ToLongTimeString())
End Sub
End Class
<Serializable(), SecurityPermission(SecurityAction.Demand, SerializationFormatter := True)> _
Class [MyClass]
Implements ISerializable
Private minValue_value As Integer
Private maxValue_value As Integer
Public Sub New()
minValue_value = Integer.MinValue
maxValue_value = Integer.MaxValue
End Sub
Public Property MinValue() As Integer
Get
Return minValue_value
End Get
Set
minValue_value = value
End Set
End Property
Public Property MaxValue() As Integer
Get
Return maxValue_value
End Get
Set
maxValue_value = value
End Set
End Property
Sub GetObjectData(ByVal si As SerializationInfo, ByVal context As StreamingContext) Implements ISerializable.GetObjectData
si.AddValue("minValue", minValue_value)
si.AddValue("maxValue", maxValue_value)
End Sub
Protected Sub New(ByVal si As SerializationInfo, ByVal context As StreamingContext)
minValue_value = Fix(si.GetValue("minValue", GetType(Integer)))
maxValue_value = Fix(si.GetValue("maxValue", GetType(Integer)))
End Sub
End Class
using System;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Security.Permissions;
namespace StreamingContextExample
{
class Program
{
static void Main(string[] args)
{
try
{
SerializeAndDeserialize();
}
catch (System.Exception exc)
{
Console.WriteLine(exc.Message);
}
finally
{
Console.WriteLine("Press <Enter> to exit....");
Console.ReadLine();
}
}
static void SerializeAndDeserialize()
{
object myObject = DateTime.Now;
// Create a StreamingContext that includes a
// a DateTime.
StreamingContext sc = new StreamingContext(
StreamingContextStates.CrossProcess, myObject);
BinaryFormatter bf = new BinaryFormatter(null, sc);
MemoryStream ms = new MemoryStream(new byte[2048]);
bf.Serialize(ms, new MyClass());
ms.Seek(0, SeekOrigin.Begin);
MyClass f = (MyClass)bf.Deserialize(ms);
Console.WriteLine("\t MinValue: {0} \n\t MaxValue: {1}",
f.MinValue , f.MaxValue);
Console.WriteLine("StreamingContext.State: {0}",
sc.State);
DateTime myDateTime = (DateTime)sc.Context;
Console.WriteLine("StreamingContext.Context: {0}",
myDateTime.ToLongTimeString());
}
}
[Serializable]
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
class MyClass : ISerializable
{
private int minValue_value;
private int maxValue_value;
public MyClass()
{
minValue_value = int.MinValue;
maxValue_value = int.MaxValue;
}
public int MinValue
{
get { return minValue_value; }
set { minValue_value = value;}
}
public int MaxValue
{
get { return maxValue_value; }
set { maxValue_value = value; }
}
void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
{
si.AddValue("minValue", minValue_value);
si.AddValue("maxValue", maxValue_value);
}
protected MyClass(SerializationInfo si, StreamingContext context)
{
minValue_value = (int)si.GetValue("minValue", typeof(int));
maxValue_value = (int)si.GetValue("maxValue", typeof(int));
}
}
}
プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
参照
関連項目
StreamingContext 構造体
StreamingContext メンバ
System.Runtime.Serialization 名前空間
FlagsAttribute
Equals