SerializationExceptionとは? わかりやすく解説

SerializationException クラス

シリアル化中または逆シリアル化中にエラー発生するスローされる例外

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class SerializationException
    Inherits SystemException
Dim instance As SerializationException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class SerializationException : SystemException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class SerializationException : public
 SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SerializationException extends
 SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public class SerializationException extends
 SystemException
解説解説

SerializationException は、値 0x8013150C を保持している HRESULT COR_E_SERIALIZATION を使用します

SerializationException は、Equals メソッド既定実装使用します。この実装では、参照等値かどうか判断できます

SerializationExceptionインスタンス初期プロパティ値の一覧については、SerializationException コンストラクタトピック参照してください

使用例使用例
Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Runtime.InteropServices
Imports System.Security.Permissions

<Assembly: SecurityPermission( _
SecurityAction.RequestMinimum, Execution:=True)> 
' This class includes several Win32 interop definitions.
Friend Class Win32
    Public Shared ReadOnly
 InvalidHandleValue As New IntPtr(-1)
    Public Const FILE_MAP_WRITE As
 Int32 = 2
    Public Const PAGE_READWRITE As
 Int32 = &H4

    <DllImport("Kernel32")> _
    Public Shared Function
 CreateFileMapping(ByVal hFile As IntPtr,
 _
                                             ByVal pAttributes
 As IntPtr, _
                                             ByVal flProtect As
 Int32, _
                                             ByVal dwMaximumSizeHigh
 As Int32, _
                                             ByVal dwMaximumSizeLow
 As Int32, _
                                             ByVal pName As
 String) As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 OpenFileMapping(ByVal dwDesiredAccess As
 Int32, _
                                           ByVal bInheritHandle
 As Boolean, _
                                           ByVal name As
 String) As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 CloseHandle(ByVal handle As IntPtr) As
 Boolean
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 MapViewOfFile(ByVal hFileMappingObject As
 IntPtr, _
                                         ByVal dwDesiredAccess
 As Int32, _
                                         ByVal dwFileOffsetHigh
 As Int32, _
                                         ByVal dwFileOffsetLow
 As Int32, _
                                         ByVal dwNumberOfBytesToMap
 As IntPtr) _
                                         As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 UnmapViewOfFile(ByVal address As IntPtr)
 As Boolean
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 DuplicateHandle(ByVal hSourceProcessHandle As
 IntPtr, _
                                           ByVal hSourceHandle
 As IntPtr, _
                                           ByVal hTargetProcessHandle
 As IntPtr, _
                                           ByRef lpTargetHandle
 As IntPtr, _
                                           ByVal dwDesiredAccess
 As Int32, _
                                           ByVal bInheritHandle
 As Boolean, _
                                           ByVal dwOptions As
 Int32) As Boolean
    End Function

    Public Const DUPLICATE_CLOSE_SOURCE As
 Int32 = &H1
    Public Const DUPLICATE_SAME_ACCESS As
 Int32 = &H2

    <DllImport("Kernel32")> Public
 Shared Function GetCurrentProcess() As
 IntPtr
    End Function
End Class


' This class wraps memory that can be simultaneously 
' shared by multiple AppDomains and Processes.
<Serializable()> Public NotInheritable
 Class SharedMemory
    Implements ISerializable
    Implements IDisposable

    ' The handle and string that identify 
    ' the Windows file-mapping object.
    Private m_hFileMap As IntPtr = IntPtr.Zero
    Private m_name As String

    ' The address of the memory-mapped file-mapping object.
    Private m_address As IntPtr
    <SecurityPermissionAttribute(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.UnmanagedCode)> _
    Public Function GetByte(ByVal
 offset As Int32) As Byte
        Dim b(0) As Byte
        Marshal.Copy(New IntPtr(m_address.ToInt64() + offset),
 b, 0, 1)
        Return b(0)
    End Function

    <SecurityPermissionAttribute(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.UnmanagedCode)> _
    Public Sub SetByte(ByVal
 offset As Int32, ByVal value As
 Byte)
        Dim b(0) As Byte
        b(0) = value
        Marshal.Copy(b, 0, New IntPtr(m_address.ToInt64() + offset),
 1)
    End Sub


    ' The constructors.
    Public Sub New(ByVal
 size As Int32)
        Me.New(size, Nothing)
    End Sub

    Public Sub New(ByVal
 size As Int32, ByVal name As
 String)
        m_hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue, _
           IntPtr.Zero, Win32.PAGE_READWRITE, 0, size, name)
        If (m_hFileMap.Equals(IntPtr.Zero)) Then
 _
           Throw New Exception("Could
 not create memory-mapped file.")
        m_name = name
        m_address = Win32.MapViewOfFile(m_hFileMap, _
           Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero)
    End Sub


    ' The cleanup methods.
    Public Sub Dispose() Implements
 IDisposable.Dispose
        GC.SuppressFinalize(Me)
        Dispose(True)
    End Sub

    Private Sub Dispose(ByVal
 disposing As Boolean)
        Win32.UnmapViewOfFile(m_address)
        Win32.CloseHandle(m_hFileMap)
        m_address = IntPtr.Zero
        m_hFileMap = IntPtr.Zero
    End Sub

    Protected Overrides Sub
 Finalize()
        Dispose(False)
    End Sub


    ' Private helper methods.
    Private Shared Function
 AllFlagsSet(ByVal flags As Int32, _
                                        ByVal flagsToTest As
 Int32) As Boolean
        Return (flags And flagsToTest) = flagsToTest
    End Function

    Private Shared Function
 AnyFlagsSet(ByVal flags As Int32, _
                                        ByVal flagsToTest As
 Int32) As Boolean
        Return (flags And flagsToTest) <>
 0
    End Function


    ' The security attribute demands that code that calls  
    ' this method have permission to perform serialization.
    <SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter:=True)>
 _
    Sub GetObjectData(ByVal info As
 SerializationInfo, _
                      ByVal context As StreamingContext)
 _
                      Implements ISerializable.GetObjectData
        ' The context's State member indicates where the object will
 be deserialized.

        ' A SharedMemory object cannot be serialized 
        ' to any of the following destinations.
        Const InvalidDestinations As StreamingContextStates
 = _
           StreamingContextStates.CrossMachine Or _
           StreamingContextStates.File Or _
           StreamingContextStates.Other Or _
           StreamingContextStates.Persistence Or _
           StreamingContextStates.Remoting
        If AnyFlagsSet(CType(context.State, Int32), _
                       CType(InvalidDestinations, Int32)) Then
            Throw New SerializationException("The
 SharedMemory object " & _
               "cannot be serialized to any of the following streaming
 contexts: " _
               & InvalidDestinations)
        End If

        Const DeserializableByHandle As StreamingContextStates
 = _
                 StreamingContextStates.Clone Or _
                 StreamingContextStates.CrossAppDomain

        If AnyFlagsSet(CType(context.State, Int32), _
              CType(DeserializableByHandle, Int32)) Then
            info.AddValue("hFileMap", m_hFileMap)
        End If

        Const DeserializableByName As StreamingContextStates
 = _
                 StreamingContextStates.CrossProcess   ' The same computer.
        If AnyFlagsSet(CType(context.State, Int32), CType(DeserializableByName,
 _
                       Int32)) Then
            If m_name = Nothing Then
                Throw New SerializationException("The
 SharedMemory object " & _
                   "cannot be serialized CrossProcess because
 it was not constructed " & _
                   "with a String name.")
            End If
            info.AddValue("name", m_name)
        End If
    End Sub

    Private Sub New(ByVal
 info As SerializationInfo, ByVal context
 As StreamingContext)
        ' The context's State member indicates where the object was
 serialized from.

        Const InvalidSources As StreamingContextStates
 = _
                 StreamingContextStates.CrossMachine Or _
                 StreamingContextStates.File Or _
                 StreamingContextStates.Other Or _
                 StreamingContextStates.Persistence Or _
                 StreamingContextStates.Remoting

        If AnyFlagsSet(CType(context.State, Int32), CType(InvalidSources,
 Int32)) Then
            Throw New SerializationException("The
 SharedMemory object " & _
               "cannot be deserialized from any of the following
 stream contexts: " & _
               InvalidSources)
        End If

        Const SerializedByHandle As StreamingContextStates
 = _
                 StreamingContextStates.Clone Or _
                 StreamingContextStates.CrossAppDomain ' The same process.
        If AnyFlagsSet(CType(context.State, Int32), _
              CType(SerializedByHandle, Int32)) Then
            Try
                Win32.DuplicateHandle(Win32.GetCurrentProcess(), _
                   CType(info.GetValue("hFileMap",
 GetType(IntPtr)), IntPtr), _
                      Win32.GetCurrentProcess(), m_hFileMap, 0, False,
 _
                      Win32.DUPLICATE_SAME_ACCESS)
            Catch e As SerializationException
                Throw New SerializationException("A
 SharedMemory was not " & _
                   "serialized using any of the following streaming
 contexts: " & _
                   SerializedByHandle)
            End Try
        End If

        Const SerializedByName As StreamingContextStates
 = _
                 StreamingContextStates.CrossProcess   ' The same computer.
        If AnyFlagsSet(CType(context.State, Int32), _
                       CType(SerializedByName, Int32)) Then
            Try
                m_name = info.GetString("name")
            Catch e As SerializationException
                Throw New SerializationException("A
 SharedMemory object " & _
                   "was not serialized using any of the following
 streaming contexts: " & _
                   SerializedByName)
            End Try
            m_hFileMap = Win32.OpenFileMapping(Win32.FILE_MAP_WRITE, False,
 m_name)
        End If
        If Not m_hFileMap.Equals(IntPtr.Zero)
 Then
            m_address = Win32.MapViewOfFile(m_hFileMap, _
               Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero)
        Else
            Throw New SerializationException("A
 SharedMemory object " & _
               "could not be deserialized.")
        End If
    End Sub
End Class

Class App
    <STAThread()> Shared Sub Main()
        Serialize()
        Console.WriteLine()
        Deserialize()
    End Sub

    Shared Sub Serialize()
        ' Create a hashtable of values that will eventually be serialized.
        Dim sm As New SharedMemory(1024,
 "MyMemory")
        Dim x As Int32
        For x = 0 To 99
            sm.SetByte(x, x)
        Next

        Dim b(9) As Byte
        For x = 0 To b.Length - 1
            b(x) = sm.GetByte(x)
        Next
        Console.WriteLine(BitConverter.ToString(b))

        ' To serialize the hashtable (and its key/value pairs), you
 must first 
        ' open a stream for writing. Use a file stream here.
        Dim fs As New FileStream("DataFile.dat",
 FileMode.Create)

        ' Construct a BinaryFormatter telling it where 
        ' the objects will be serialized to.
        Dim formatter As New
 BinaryFormatter(Nothing, _
           New StreamingContext(StreamingContextStates.CrossAppDomain))
        Try
            formatter.Serialize(fs, sm)
        Catch e As SerializationException
            Console.WriteLine("Failed to serialize. Reason: "
 + e.Message)
            Throw
        Finally
            fs.Close()
        End Try
    End Sub

    Shared Sub Deserialize()
        ' Declare the hashtable reference.
        Dim sm As SharedMemory = Nothing

        ' Open the file containing the data that you want to deserialize.
        Dim fs As New FileStream("DataFile.dat",
 FileMode.Open)
        Try
            Dim Formatter As New
 BinaryFormatter(Nothing, _
               New StreamingContext(StreamingContextStates.CrossAppDomain))

            ' Deserialize the SharedMemory object from the file and
 
            ' assign the reference to the local variable.
            sm = DirectCast(Formatter.Deserialize(fs), SharedMemory)
        Catch e As SerializationException
            Console.WriteLine("Failed to deserialize. Reason:
 " & e.Message)
        Finally
            fs.Close()
        End Try

        ' To prove that the SharedMemory object deserialized correctly,
 
        ' display some of its bytes to the console.
        Dim b(9) As Byte
        Dim x As Int32
        For x = 0 To b.Length - 1
            b(x) = sm.GetByte(x)
        Next
        Console.WriteLine(BitConverter.ToString(b))
    End Sub
End Class
// Note: You must compile this file using the C# /unsafe switch.
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Security.Permissions;

[assembly: SecurityPermission(
SecurityAction.RequestMinimum, Execution = true)]
// This class includes several Win32 interop definitions.
internal class Win32
{
    public static readonly IntPtr InvalidHandleValue
 = new IntPtr(-1);
    public const UInt32 FILE_MAP_WRITE = 2;
    public const UInt32 PAGE_READWRITE = 0x04;

    [DllImport("Kernel32")]
    public static extern IntPtr CreateFileMapping(IntPtr
 hFile,
        IntPtr pAttributes, UInt32 flProtect,
        UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName);

    [DllImport("Kernel32")]
    public static extern IntPtr OpenFileMapping(UInt32
 dwDesiredAccess,
        Boolean bInheritHandle, String name);

    [DllImport("Kernel32")]
    public static extern Boolean CloseHandle(IntPtr
 handle);

    [DllImport("Kernel32")]
    public static extern IntPtr MapViewOfFile(IntPtr
 hFileMappingObject,
        UInt32 dwDesiredAccess,
        UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow,
        IntPtr dwNumberOfBytesToMap);

    [DllImport("Kernel32")]
    public static extern Boolean UnmapViewOfFile(IntPtr
 address);

    [DllImport("Kernel32")]
    public static extern Boolean DuplicateHandle(IntPtr
 hSourceProcessHandle,
        IntPtr hSourceHandle,
        IntPtr hTargetProcessHandle, ref IntPtr lpTargetHandle,
        UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwOptions);
    public const UInt32 DUPLICATE_CLOSE_SOURCE
 = 0x00000001;
    public const UInt32 DUPLICATE_SAME_ACCESS
 = 0x00000002;

    [DllImport("Kernel32")]
    public static extern IntPtr GetCurrentProcess();
}


// This class wraps memory that can be simultaneously 
// shared by multiple AppDomains and Processes.
[Serializable]
public sealed class SharedMemory : ISerializable,
 IDisposable
{
    // The handle and string that identify 
    // the Windows file-mapping object.
    private IntPtr m_hFileMap = IntPtr.Zero;
    private String m_name;

    // The address of the memory-mapped file-mapping object.
    private IntPtr m_address;

    public unsafe Byte* Address
    {
        get { return (Byte*)m_address; }
    }

    // The constructors.
    public SharedMemory(Int32 size) : this(size,
 null) { }

    public SharedMemory(Int32 size, String name)
    {
        m_hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue,
            IntPtr.Zero, Win32.PAGE_READWRITE,
            0, unchecked((UInt32)size), name);
        if (m_hFileMap == IntPtr.Zero)
            throw new Exception("Could not create memory-mapped
 file.");
        m_name = name;
        m_address = Win32.MapViewOfFile(m_hFileMap, Win32.FILE_MAP_WRITE,
            0, 0, IntPtr.Zero);
    }

    // The cleanup methods.
    public void Dispose()
    {
        GC.SuppressFinalize(this);
        Dispose(true);
    }

    private void Dispose(Boolean disposing)
    {
        Win32.UnmapViewOfFile(m_address);
        Win32.CloseHandle(m_hFileMap);
        m_address = IntPtr.Zero;
        m_hFileMap = IntPtr.Zero;
    }

    ~SharedMemory()
    {
        Dispose(false);
    }

    // Private helper methods.
    private static Boolean AllFlagsSet(Int32
 flags, Int32 flagsToTest)
    {
        return (flags & flagsToTest) == flagsToTest;
    }

    private static Boolean AnyFlagsSet(Int32
 flags, Int32 flagsToTest)
    {
        return (flags & flagsToTest) != 0;
    }


    // The security attribute demands that code that calls  
    // this method have permission to perform serialization.
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =
 true)]
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext
 context)
    {
        // The context's State member indicates
        // where the object will be deserialized.

        // A SharedMemory object cannot be serialized 
        // to any of the following destinations.
        const StreamingContextStates InvalidDestinations =
                  StreamingContextStates.CrossMachine |
                  StreamingContextStates.File |
                  StreamingContextStates.Other |
                  StreamingContextStates.Persistence |
                  StreamingContextStates.Remoting;
        if (AnyFlagsSet((Int32)context.State, (Int32)InvalidDestinations))
            throw new SerializationException("The SharedMemory
 object " +
                "cannot be serialized to any of the following streaming contexts:
 " +
                InvalidDestinations);

        const StreamingContextStates DeserializableByHandle =
                  StreamingContextStates.Clone |
            // The same process.
                  StreamingContextStates.CrossAppDomain;
        if (AnyFlagsSet((Int32)context.State, (Int32)DeserializableByHandle))
            info.AddValue("hFileMap", m_hFileMap);

        const StreamingContextStates DeserializableByName =
            // The same computer.
                  StreamingContextStates.CrossProcess;
        if (AnyFlagsSet((Int32)context.State, (Int32)DeserializableByName))
        {
            if (m_name == null)
                throw new SerializationException("The SharedMemory
 object " +
                    "cannot be serialized CrossProcess because it was not constructed
 " +
                    "with a String name.");
            info.AddValue("name", m_name);
        }
    }


    // The security attribute demands that code that calls  
    // this method have permission to perform serialization.
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =
 true)]
    private SharedMemory(SerializationInfo info, StreamingContext
 context)
    {
        // The context's State member indicates 
        // where the object was serialized from.

        const StreamingContextStates InvalidSources =
                  StreamingContextStates.CrossMachine |
                  StreamingContextStates.File |
                  StreamingContextStates.Other |
                  StreamingContextStates.Persistence |
                  StreamingContextStates.Remoting;
        if (AnyFlagsSet((Int32)context.State, (Int32)InvalidSources))
            throw new SerializationException("The SharedMemory
 object " +
                "cannot be deserialized from any of the following stream contexts:
 " +
                InvalidSources);

        const StreamingContextStates SerializedByHandle =
                  StreamingContextStates.Clone |
            // The same process.
                  StreamingContextStates.CrossAppDomain;
        if (AnyFlagsSet((Int32)context.State, (Int32)SerializedByHandle))
        {
            try
            {
                Win32.DuplicateHandle(Win32.GetCurrentProcess(),
                    (IntPtr)info.GetValue("hFileMap", typeof(IntPtr)),
                    Win32.GetCurrentProcess(), ref m_hFileMap, 0, false
,
                    Win32.DUPLICATE_SAME_ACCESS);
            }
            catch (SerializationException)
            {
                throw new SerializationException("A SharedMemory
 was not serialized " +
                    "using any of the following streaming
 contexts: " +
                    SerializedByHandle);
            }
        }

        const StreamingContextStates SerializedByName =
            // The same computer.
                  StreamingContextStates.CrossProcess;
        if (AnyFlagsSet((Int32)context.State, (Int32)SerializedByName))
        {
            try
            {
                m_name = info.GetString("name");
            }
            catch (SerializationException)
            {
                throw new SerializationException("A SharedMemory
 object was not " +
                    "serialized using any of the following
 streaming contexts: " +
                    SerializedByName);
            }
            m_hFileMap = Win32.OpenFileMapping(Win32.FILE_MAP_WRITE, false,
 m_name);
        }
        if (m_hFileMap != IntPtr.Zero)
        {
            m_address = Win32.MapViewOfFile(m_hFileMap, Win32.FILE_MAP_WRITE,
                0, 0, IntPtr.Zero);
        }
        else
        {
            throw new SerializationException("A SharedMemory
 object could not " +
                "be deserialized.");
        }
    }
}


class App
{
    [STAThread]
    static void Main(string[]
 args)
    {
        Serialize();
        Console.WriteLine();
        Deserialize();
    }

    unsafe static void Serialize()
    {
        // Create a hashtable of values that will eventually be serialized.
        SharedMemory sm = new SharedMemory(1024, "JeffMemory");
        for (Int32 x = 0; x < 100; x++)
            *(sm.Address + x) = (Byte)x;

        Byte[] b = new Byte[10];
        for (Int32 x = 0; x < b.Length; x++) b[x] = *(sm.Address
 + x);
        Console.WriteLine(BitConverter.ToString(b));

        // To serialize the SharedMemory object, 
        // you must first open a stream for writing. 
        // Use a file stream here.
        FileStream fs = new FileStream("DataFile.dat",
 FileMode.Create);

        // Construct a BinaryFormatter and tell it where 
        // the objects will be serialized to.
        BinaryFormatter formatter = new BinaryFormatter(null
,
            new StreamingContext(StreamingContextStates.CrossAppDomain));
        try
        {
            formatter.Serialize(fs, sm);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }
    }


    unsafe static void Deserialize()
    {
        // Declare a SharedMemory reference.
        SharedMemory sm = null;

        // Open the file containing the data that you want to deserialize.
        FileStream fs = new FileStream("DataFile.dat",
 FileMode.Open);
        try
        {
            BinaryFormatter formatter = new BinaryFormatter(null
,
                new StreamingContext(StreamingContextStates.CrossAppDomain));

            // Deserialize the SharedMemory object from the file and
 
            // assign the reference to the local variable.
            sm = (SharedMemory)formatter.Deserialize(fs);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }

        // To prove that the SharedMemory object deserialized correctly,
 
        // display some of its bytes to the console.
        Byte[] b = new Byte[10];
        for (Int32 x = 0; x < b.Length; x++) b[x] = *(sm.Address
 + x);
        Console.WriteLine(BitConverter.ToString(b));
    }
}
継承階層継承階層
System.Object
   System.Exception
     System.SystemException
      System.Runtime.Serialization.SerializationException
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationException メンバ
System.Runtime.Serialization 名前空間
Exception
その他の技術情報
例外の処理とスロー

SerializationException コンストラクタ ()

SerializationException クラス新しインスタンス既定プロパティ使用して初期化します。

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Dim instance As New SerializationException
public SerializationException ()
public:
SerializationException ()
public SerializationException ()
public function SerializationException ()
解説解説

SerializationException は SystemException クラスから継承します現在のコンストラクタ使用して初期化する SerializationExceptionインスタンス初期プロパティ値を次の表に示します

プロパティ

初期値

InnerException

null 参照 (Visual Basic では Nothing)

Message

ローカライズされた、SerializationExceptionエラー メッセージ

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationException クラス
SerializationException メンバ
System.Runtime.Serialization 名前空間

SerializationException コンストラクタ (String)

指定したメッセージ使用して、SerializationException クラス新しインスタンス初期化します。

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Dim message As String

Dim instance As New SerializationException(message)
public SerializationException (
    string message
)
public:
SerializationException (
    String^ message
)
public SerializationException (
    String message
)
public function SerializationException (
    message : String
)

パラメータ

message

例外発生した原因示します

解説解説

SerializationException は SystemException クラスから継承します現在のコンストラクタ使用して初期化する SerializationExceptionインスタンス初期プロパティ値を次の表に示します

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationException クラス
SerializationException メンバ
System.Runtime.Serialization 名前空間

SerializationException コンストラクタ (String, Exception)

指定したエラー メッセージと、この例外原因である内部例外への参照使用して、SerializationException クラス新しインスタンス初期化します。

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    message As String, _
    innerException As Exception _
)
Dim message As String
Dim innerException As Exception

Dim instance As New SerializationException(message,
 innerException)
public SerializationException (
    string message,
    Exception innerException
)
public:
SerializationException (
    String^ message, 
    Exception^ innerException
)
public SerializationException (
    String message, 
    Exception innerException
)
public function SerializationException (
    message : String, 
    innerException : Exception
)

パラメータ

message

例外原因説明するエラー メッセージ

innerException

現在の例外の原因である例外innerException パラメータnull 参照 (Visual Basic では Nothing) でない場合は、内部例外処理する catch ブロック現在の例外が発生します

解説解説

前の例外直接結果としてスローされる例外については、InnerException プロパティに、前の例外への参照格納されます。InnerException プロパティは、コンストラクタ渡された値と同じ値を返しますInnerException プロパティによって内部例外値がコンストラクタ渡されなかった場合は、null 参照 (Visual Basic では Nothing) を返します

SerializationExceptionインスタンス初期プロパティ値を次の表に示します

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationException クラス
SerializationException メンバ
System.Runtime.Serialization 名前空間
Exception
その他の技術情報
例外の処理とスロー

SerializationException コンストラクタ (SerializationInfo, StreamingContext)

シリアル化したデータから、SerializationException クラス新しインスタンス初期化します。

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Protected Sub New ( _
    info As SerializationInfo, _
    context As StreamingContext _
)
Dim info As SerializationInfo
Dim context As StreamingContext

Dim instance As New SerializationException(info,
 context)
protected SerializationException (
    SerializationInfo info,
    StreamingContext context
)
protected:
SerializationException (
    SerializationInfo^ info, 
    StreamingContext context
)
protected SerializationException (
    SerializationInfo info, 
    StreamingContext context
)
protected function SerializationException (
    info : SerializationInfo, 
    context : StreamingContext
)

パラメータ

info

名前/値形式シリアル化オブジェクト データ保持しているシリアル化情報オブジェクト

context

例外発生元または発生先に関すコンテキスト情報

例外例外
例外種類条件

ArgumentNullException

info パラメータnull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例
Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Runtime.InteropServices
Imports System.Security.Permissions

<Assembly: SecurityPermission( _
SecurityAction.RequestMinimum, Execution:=True)> 
' This class includes several Win32 interop definitions.
Friend Class Win32
    Public Shared ReadOnly
 InvalidHandleValue As New IntPtr(-1)
    Public Const FILE_MAP_WRITE As
 Int32 = 2
    Public Const PAGE_READWRITE As
 Int32 = &H4

    <DllImport("Kernel32")> _
    Public Shared Function
 CreateFileMapping(ByVal hFile As IntPtr,
 _
                                             ByVal pAttributes
 As IntPtr, _
                                             ByVal flProtect As
 Int32, _
                                             ByVal dwMaximumSizeHigh
 As Int32, _
                                             ByVal dwMaximumSizeLow
 As Int32, _
                                             ByVal pName As
 String) As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 OpenFileMapping(ByVal dwDesiredAccess As
 Int32, _
                                           ByVal bInheritHandle
 As Boolean, _
                                           ByVal name As
 String) As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 CloseHandle(ByVal handle As IntPtr) As
 Boolean
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 MapViewOfFile(ByVal hFileMappingObject As
 IntPtr, _
                                         ByVal dwDesiredAccess
 As Int32, _
                                         ByVal dwFileOffsetHigh
 As Int32, _
                                         ByVal dwFileOffsetLow
 As Int32, _
                                         ByVal dwNumberOfBytesToMap
 As IntPtr) _
                                         As IntPtr
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 UnmapViewOfFile(ByVal address As IntPtr)
 As Boolean
    End Function

    <DllImport("Kernel32")> _
    Public Shared Function
 DuplicateHandle(ByVal hSourceProcessHandle As
 IntPtr, _
                                           ByVal hSourceHandle
 As IntPtr, _
                                           ByVal hTargetProcessHandle
 As IntPtr, _
                                           ByRef lpTargetHandle
 As IntPtr, _
                                           ByVal dwDesiredAccess
 As Int32, _
                                           ByVal bInheritHandle
 As Boolean, _
                                           ByVal dwOptions As
 Int32) As Boolean
    End Function

    Public Const DUPLICATE_CLOSE_SOURCE As
 Int32 = &H1
    Public Const DUPLICATE_SAME_ACCESS As
 Int32 = &H2

    <DllImport("Kernel32")> Public
 Shared Function GetCurrentProcess() As
 IntPtr
    End Function
End Class


' This class wraps memory that can be simultaneously 
' shared by multiple AppDomains and Processes.
<Serializable()> Public NotInheritable
 Class SharedMemory
    Implements ISerializable
    Implements IDisposable

    ' The handle and string that identify 
    ' the Windows file-mapping object.
    Private m_hFileMap As IntPtr = IntPtr.Zero
    Private m_name As String

    ' The address of the memory-mapped file-mapping object.
    Private m_address As IntPtr
    <SecurityPermissionAttribute(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.UnmanagedCode)> _
    Public Function GetByte(ByVal
 offset As Int32) As Byte
        Dim b(0) As Byte
        Marshal.Copy(New IntPtr(m_address.ToInt64() + offset),
 b, 0, 1)
        Return b(0)
    End Function

    <SecurityPermissionAttribute(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.UnmanagedCode)> _
    Public Sub SetByte(ByVal
 offset As Int32, ByVal value As
 Byte)
        Dim b(0) As Byte
        b(0) = value
        Marshal.Copy(b, 0, New IntPtr(m_address.ToInt64() + offset),
 1)
    End Sub


    ' The constructors.
    Public Sub New(ByVal
 size As Int32)
        Me.New(size, Nothing)
    End Sub

    Public Sub New(ByVal
 size As Int32, ByVal name As
 String)
        m_hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue, _
           IntPtr.Zero, Win32.PAGE_READWRITE, 0, size, name)
        If (m_hFileMap.Equals(IntPtr.Zero)) Then
 _
           Throw New Exception("Could
 not create memory-mapped file.")
        m_name = name
        m_address = Win32.MapViewOfFile(m_hFileMap, _
           Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero)
    End Sub


    ' The cleanup methods.
    Public Sub Dispose() Implements
 IDisposable.Dispose
        GC.SuppressFinalize(Me)
        Dispose(True)
    End Sub

    Private Sub Dispose(ByVal
 disposing As Boolean)
        Win32.UnmapViewOfFile(m_address)
        Win32.CloseHandle(m_hFileMap)
        m_address = IntPtr.Zero
        m_hFileMap = IntPtr.Zero
    End Sub

    Protected Overrides Sub
 Finalize()
        Dispose(False)
    End Sub


    ' Private helper methods.
    Private Shared Function
 AllFlagsSet(ByVal flags As Int32, _
                                        ByVal flagsToTest As
 Int32) As Boolean
        Return (flags And flagsToTest) = flagsToTest
    End Function

    Private Shared Function
 AnyFlagsSet(ByVal flags As Int32, _
                                        ByVal flagsToTest As
 Int32) As Boolean
        Return (flags And flagsToTest) <>
 0
    End Function


    ' The security attribute demands that code that calls  
    ' this method have permission to perform serialization.
    <SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter:=True)>
 _
    Sub GetObjectData(ByVal info As
 SerializationInfo, _
                      ByVal context As StreamingContext)
 _
                      Implements ISerializable.GetObjectData
        ' The context's State member indicates where the object will
 be deserialized.

        ' A SharedMemory object cannot be serialized 
        ' to any of the following destinations.
        Const InvalidDestinations As StreamingContextStates
 = _
           StreamingContextStates.CrossMachine Or _
           StreamingContextStates.File Or _
           StreamingContextStates.Other Or _
           StreamingContextStates.Persistence Or _
           StreamingContextStates.Remoting
        If AnyFlagsSet(CType(context.State, Int32), _
                       CType(InvalidDestinations, Int32)) Then
            Throw New SerializationException("The
 SharedMemory object " & _
               "cannot be serialized to any of the following streaming
 contexts: " _
               & InvalidDestinations)
        End If

        Const DeserializableByHandle As StreamingContextStates
 = _
                 StreamingContextStates.Clone Or _
                 StreamingContextStates.CrossAppDomain

        If AnyFlagsSet(CType(context.State, Int32), _
              CType(DeserializableByHandle, Int32)) Then
            info.AddValue("hFileMap", m_hFileMap)
        End If

        Const DeserializableByName As StreamingContextStates
 = _
                 StreamingContextStates.CrossProcess   ' The same computer.
        If AnyFlagsSet(CType(context.State, Int32), CType(DeserializableByName,
 _
                       Int32)) Then
            If m_name = Nothing Then
                Throw New SerializationException("The
 SharedMemory object " & _
                   "cannot be serialized CrossProcess because
 it was not constructed " & _
                   "with a String name.")
            End If
            info.AddValue("name", m_name)
        End If
    End Sub

    Private Sub New(ByVal
 info As SerializationInfo, ByVal context
 As StreamingContext)
        ' The context's State member indicates where the object was
 serialized from.

        Const InvalidSources As StreamingContextStates
 = _
                 StreamingContextStates.CrossMachine Or _
                 StreamingContextStates.File Or _
                 StreamingContextStates.Other Or _
                 StreamingContextStates.Persistence Or _
                 StreamingContextStates.Remoting

        If AnyFlagsSet(CType(context.State, Int32), CType(InvalidSources,
 Int32)) Then
            Throw New SerializationException("The
 SharedMemory object " & _
               "cannot be deserialized from any of the following
 stream contexts: " & _
               InvalidSources)
        End If

        Const SerializedByHandle As StreamingContextStates
 = _
                 StreamingContextStates.Clone Or _
                 StreamingContextStates.CrossAppDomain ' The same process.
        If AnyFlagsSet(CType(context.State, Int32), _
              CType(SerializedByHandle, Int32)) Then
            Try
                Win32.DuplicateHandle(Win32.GetCurrentProcess(), _
                   CType(info.GetValue("hFileMap",
 GetType(IntPtr)), IntPtr), _
                      Win32.GetCurrentProcess(), m_hFileMap, 0, False,
 _
                      Win32.DUPLICATE_SAME_ACCESS)
            Catch e As SerializationException
                Throw New SerializationException("A
 SharedMemory was not " & _
                   "serialized using any of the following streaming
 contexts: " & _
                   SerializedByHandle)
            End Try
        End If

        Const SerializedByName As StreamingContextStates
 = _
                 StreamingContextStates.CrossProcess   ' The same computer.
        If AnyFlagsSet(CType(context.State, Int32), _
                       CType(SerializedByName, Int32)) Then
            Try
                m_name = info.GetString("name")
            Catch e As SerializationException
                Throw New SerializationException("A
 SharedMemory object " & _
                   "was not serialized using any of the following
 streaming contexts: " & _
                   SerializedByName)
            End Try
            m_hFileMap = Win32.OpenFileMapping(Win32.FILE_MAP_WRITE, False,
 m_name)
        End If
        If Not m_hFileMap.Equals(IntPtr.Zero)
 Then
            m_address = Win32.MapViewOfFile(m_hFileMap, _
               Win32.FILE_MAP_WRITE, 0, 0, IntPtr.Zero)
        Else
            Throw New SerializationException("A
 SharedMemory object " & _
               "could not be deserialized.")
        End If
    End Sub
End Class

Class App
    <STAThread()> Shared Sub Main()
        Serialize()
        Console.WriteLine()
        Deserialize()
    End Sub

    Shared Sub Serialize()
        ' Create a hashtable of values that will eventually be serialized.
        Dim sm As New SharedMemory(1024,
 "MyMemory")
        Dim x As Int32
        For x = 0 To 99
            sm.SetByte(x, x)
        Next

        Dim b(9) As Byte
        For x = 0 To b.Length - 1
            b(x) = sm.GetByte(x)
        Next
        Console.WriteLine(BitConverter.ToString(b))

        ' To serialize the hashtable (and its key/value pairs), you
 must first 
        ' open a stream for writing. Use a file stream here.
        Dim fs As New FileStream("DataFile.dat",
 FileMode.Create)

        ' Construct a BinaryFormatter telling it where 
        ' the objects will be serialized to.
        Dim formatter As New
 BinaryFormatter(Nothing, _
           New StreamingContext(StreamingContextStates.CrossAppDomain))
        Try
            formatter.Serialize(fs, sm)
        Catch e As SerializationException
            Console.WriteLine("Failed to serialize. Reason: "
 + e.Message)
            Throw
        Finally
            fs.Close()
        End Try
    End Sub

    Shared Sub Deserialize()
        ' Declare the hashtable reference.
        Dim sm As SharedMemory = Nothing

        ' Open the file containing the data that you want to deserialize.
        Dim fs As New FileStream("DataFile.dat",
 FileMode.Open)
        Try
            Dim Formatter As New
 BinaryFormatter(Nothing, _
               New StreamingContext(StreamingContextStates.CrossAppDomain))

            ' Deserialize the SharedMemory object from the file and
 
            ' assign the reference to the local variable.
            sm = DirectCast(Formatter.Deserialize(fs), SharedMemory)
        Catch e As SerializationException
            Console.WriteLine("Failed to deserialize. Reason:
 " & e.Message)
        Finally
            fs.Close()
        End Try

        ' To prove that the SharedMemory object deserialized correctly,
 
        ' display some of its bytes to the console.
        Dim b(9) As Byte
        Dim x As Int32
        For x = 0 To b.Length - 1
            b(x) = sm.GetByte(x)
        Next
        Console.WriteLine(BitConverter.ToString(b))
    End Sub
End Class
// Note: You must compile this file using the C# /unsafe switch.
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Security.Permissions;

[assembly: SecurityPermission(
SecurityAction.RequestMinimum, Execution = true)]
// This class includes several Win32 interop definitions.
internal class Win32
{
    public static readonly IntPtr InvalidHandleValue
 = new IntPtr(-1);
    public const UInt32 FILE_MAP_WRITE = 2;
    public const UInt32 PAGE_READWRITE = 0x04;

    [DllImport("Kernel32")]
    public static extern IntPtr CreateFileMapping(IntPtr
 hFile,
        IntPtr pAttributes, UInt32 flProtect,
        UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName);

    [DllImport("Kernel32")]
    public static extern IntPtr OpenFileMapping(UInt32
 dwDesiredAccess,
        Boolean bInheritHandle, String name);

    [DllImport("Kernel32")]
    public static extern Boolean CloseHandle(IntPtr
 handle);

    [DllImport("Kernel32")]
    public static extern IntPtr MapViewOfFile(IntPtr
 hFileMappingObject,
        UInt32 dwDesiredAccess,
        UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow,
        IntPtr dwNumberOfBytesToMap);

    [DllImport("Kernel32")]
    public static extern Boolean UnmapViewOfFile(IntPtr
 address);

    [DllImport("Kernel32")]
    public static extern Boolean DuplicateHandle(IntPtr
 hSourceProcessHandle,
        IntPtr hSourceHandle,
        IntPtr hTargetProcessHandle, ref IntPtr lpTargetHandle,
        UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwOptions);
    public const UInt32 DUPLICATE_CLOSE_SOURCE
 = 0x00000001;
    public const UInt32 DUPLICATE_SAME_ACCESS
 = 0x00000002;

    [DllImport("Kernel32")]
    public static extern IntPtr GetCurrentProcess();
}


// This class wraps memory that can be simultaneously 
// shared by multiple AppDomains and Processes.
[Serializable]
public sealed class SharedMemory : ISerializable,
 IDisposable
{
    // The handle and string that identify 
    // the Windows file-mapping object.
    private IntPtr m_hFileMap = IntPtr.Zero;
    private String m_name;

    // The address of the memory-mapped file-mapping object.
    private IntPtr m_address;

    public unsafe Byte* Address
    {
        get { return (Byte*)m_address; }
    }

    // The constructors.
    public SharedMemory(Int32 size) : this(size,
 null) { }

    public SharedMemory(Int32 size, String name)
    {
        m_hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue,
            IntPtr.Zero, Win32.PAGE_READWRITE,
            0, unchecked((UInt32)size), name);
        if (m_hFileMap == IntPtr.Zero)
            throw new Exception("Could not create memory-mapped
 file.");
        m_name = name;
        m_address = Win32.MapViewOfFile(m_hFileMap, Win32.FILE_MAP_WRITE,
            0, 0, IntPtr.Zero);
    }

    // The cleanup methods.
    public void Dispose()
    {
        GC.SuppressFinalize(this);
        Dispose(true);
    }

    private void Dispose(Boolean disposing)
    {
        Win32.UnmapViewOfFile(m_address);
        Win32.CloseHandle(m_hFileMap);
        m_address = IntPtr.Zero;
        m_hFileMap = IntPtr.Zero;
    }

    ~SharedMemory()
    {
        Dispose(false);
    }

    // Private helper methods.
    private static Boolean AllFlagsSet(Int32
 flags, Int32 flagsToTest)
    {
        return (flags & flagsToTest) == flagsToTest;
    }

    private static Boolean AnyFlagsSet(Int32
 flags, Int32 flagsToTest)
    {
        return (flags & flagsToTest) != 0;
    }


    // The security attribute demands that code that calls  
    // this method have permission to perform serialization.
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =
 true)]
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext
 context)
    {
        // The context's State member indicates
        // where the object will be deserialized.

        // A SharedMemory object cannot be serialized 
        // to any of the following destinations.
        const StreamingContextStates InvalidDestinations =
                  StreamingContextStates.CrossMachine |
                  StreamingContextStates.File |
                  StreamingContextStates.Other |
                  StreamingContextStates.Persistence |
                  StreamingContextStates.Remoting;
        if (AnyFlagsSet((Int32)context.State, (Int32)InvalidDestinations))
            throw new SerializationException("The SharedMemory
 object " +
                "cannot be serialized to any of the following streaming contexts:
 " +
                InvalidDestinations);

        const StreamingContextStates DeserializableByHandle =
                  StreamingContextStates.Clone |
            // The same process.
                  StreamingContextStates.CrossAppDomain;
        if (AnyFlagsSet((Int32)context.State, (Int32)DeserializableByHandle))
            info.AddValue("hFileMap", m_hFileMap);

        const StreamingContextStates DeserializableByName =
            // The same computer.
                  StreamingContextStates.CrossProcess;
        if (AnyFlagsSet((Int32)context.State, (Int32)DeserializableByName))
        {
            if (m_name == null)
                throw new SerializationException("The SharedMemory
 object " +
                    "cannot be serialized CrossProcess because it was not constructed
 " +
                    "with a String name.");
            info.AddValue("name", m_name);
        }
    }


    // The security attribute demands that code that calls  
    // this method have permission to perform serialization.
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =
 true)]
    private SharedMemory(SerializationInfo info, StreamingContext
 context)
    {
        // The context's State member indicates 
        // where the object was serialized from.

        const StreamingContextStates InvalidSources =
                  StreamingContextStates.CrossMachine |
                  StreamingContextStates.File |
                  StreamingContextStates.Other |
                  StreamingContextStates.Persistence |
                  StreamingContextStates.Remoting;
        if (AnyFlagsSet((Int32)context.State, (Int32)InvalidSources))
            throw new SerializationException("The SharedMemory
 object " +
                "cannot be deserialized from any of the following stream contexts:
 " +
                InvalidSources);

        const StreamingContextStates SerializedByHandle =
                  StreamingContextStates.Clone |
            // The same process.
                  StreamingContextStates.CrossAppDomain;
        if (AnyFlagsSet((Int32)context.State, (Int32)SerializedByHandle))
        {
            try
            {
                Win32.DuplicateHandle(Win32.GetCurrentProcess(),
                    (IntPtr)info.GetValue("hFileMap", typeof(IntPtr)),
                    Win32.GetCurrentProcess(), ref m_hFileMap, 0, false
,
                    Win32.DUPLICATE_SAME_ACCESS);
            }
            catch (SerializationException)
            {
                throw new SerializationException("A SharedMemory
 was not serialized " +
                    "using any of the following streaming
 contexts: " +
                    SerializedByHandle);
            }
        }

        const StreamingContextStates SerializedByName =
            // The same computer.
                  StreamingContextStates.CrossProcess;
        if (AnyFlagsSet((Int32)context.State, (Int32)SerializedByName))
        {
            try
            {
                m_name = info.GetString("name");
            }
            catch (SerializationException)
            {
                throw new SerializationException("A SharedMemory
 object was not " +
                    "serialized using any of the following
 streaming contexts: " +
                    SerializedByName);
            }
            m_hFileMap = Win32.OpenFileMapping(Win32.FILE_MAP_WRITE, false,
 m_name);
        }
        if (m_hFileMap != IntPtr.Zero)
        {
            m_address = Win32.MapViewOfFile(m_hFileMap, Win32.FILE_MAP_WRITE,
                0, 0, IntPtr.Zero);
        }
        else
        {
            throw new SerializationException("A SharedMemory
 object could not " +
                "be deserialized.");
        }
    }
}


class App
{
    [STAThread]
    static void Main(string[]
 args)
    {
        Serialize();
        Console.WriteLine();
        Deserialize();
    }

    unsafe static void Serialize()
    {
        // Create a hashtable of values that will eventually be serialized.
        SharedMemory sm = new SharedMemory(1024, "JeffMemory");
        for (Int32 x = 0; x < 100; x++)
            *(sm.Address + x) = (Byte)x;

        Byte[] b = new Byte[10];
        for (Int32 x = 0; x < b.Length; x++) b[x] = *(sm.Address
 + x);
        Console.WriteLine(BitConverter.ToString(b));

        // To serialize the SharedMemory object, 
        // you must first open a stream for writing. 
        // Use a file stream here.
        FileStream fs = new FileStream("DataFile.dat",
 FileMode.Create);

        // Construct a BinaryFormatter and tell it where 
        // the objects will be serialized to.
        BinaryFormatter formatter = new BinaryFormatter(null
,
            new StreamingContext(StreamingContextStates.CrossAppDomain));
        try
        {
            formatter.Serialize(fs, sm);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }
    }


    unsafe static void Deserialize()
    {
        // Declare a SharedMemory reference.
        SharedMemory sm = null;

        // Open the file containing the data that you want to deserialize.
        FileStream fs = new FileStream("DataFile.dat",
 FileMode.Open);
        try
        {
            BinaryFormatter formatter = new BinaryFormatter(null
,
                new StreamingContext(StreamingContextStates.CrossAppDomain));

            // Deserialize the SharedMemory object from the file and
 
            // assign the reference to the local variable.
            sm = (SharedMemory)formatter.Deserialize(fs);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }

        // To prove that the SharedMemory object deserialized correctly,
 
        // display some of its bytes to the console.
        Byte[] b = new Byte[10];
        for (Int32 x = 0; x < b.Length; x++) b[x] = *(sm.Address
 + x);
        Console.WriteLine(BitConverter.ToString(b));
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationException クラス
SerializationException メンバ
System.Runtime.Serialization 名前空間

SerializationException コンストラクタ

SerializationException クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
SerializationException () SerializationException クラス新しインスタンス既定プロパティ使用して初期化します。
SerializationException (String) 指定したメッセージ使用してSerializationException クラス新しインスタンス初期化します。
SerializationException (SerializationInfo, StreamingContext) シリアル化したデータから、SerializationException クラス新しインスタンス初期化します。
SerializationException (String, Exception) 指定したエラー メッセージと、この例外原因である内部例外への参照使用してSerializationException クラス新しインスタンス初期化します。
参照参照

関連項目

SerializationException クラス
SerializationException メンバ
System.Runtime.Serialization 名前空間

SerializationException プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ HResult  特定の例外割り当てられているコード化数値である HRESULT を取得または設定します。 ( Exception から継承されます。)
参照参照

関連項目

SerializationException クラス
System.Runtime.Serialization 名前空間
Exception

その他の技術情報

例外の処理とスロー

SerializationException メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SerializationException クラス
System.Runtime.Serialization 名前空間
Exception

その他の技術情報

例外の処理とスロー

SerializationException メンバ

シリアル化中または逆シリアル化中にエラー発生するスローされる例外

SerializationException データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
プロテクト コンストラクタプロテクト コンストラクタ
  名前 説明
プロテクト メソッド SerializationException オーバーロードされますSerializationException クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ HResult  特定の例外割り当てられているコード化数値である HRESULT を取得または設定します。(Exception から継承されます。)
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SerializationException クラス
System.Runtime.Serialization 名前空間
Exception

その他の技術情報

例外の処理とスロー



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SerializationException」の関連用語

SerializationExceptionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SerializationExceptionのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS