当前位置: 首页>>代码示例>>C#>>正文


C# StreamingContext.State属性代码示例

本文整理汇总了C#中System.Runtime.Serialization.StreamingContext.State属性的典型用法代码示例。如果您正苦于以下问题:C# StreamingContext.State属性的具体用法?C# StreamingContext.State怎么用?C# StreamingContext.State使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Runtime.Serialization.StreamingContext的用法示例。


在下文中一共展示了StreamingContext.State属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SecurityPermission

// 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",CharSet=CharSet.Unicode)]
    public static extern IntPtr CreateFileMapping(IntPtr hFile,
        IntPtr pAttributes, UInt32 flProtect,
        UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName);

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

    [DllImport("Kernel32",CharSet=CharSet.Unicode)]
    public static extern Boolean CloseHandle(IntPtr handle);

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

    [DllImport("Kernel32",CharSet=CharSet.Unicode)]
    public static extern Boolean UnmapViewOfFile(IntPtr address);

    [DllImport("Kernel32",CharSet=CharSet.Unicode)]
    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",CharSet=CharSet.Unicode)]
    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));
    }
}
开发者ID:.NET开发者,项目名称:System.Runtime.Serialization,代码行数:304,代码来源:StreamingContext.State


注:本文中的System.Runtime.Serialization.StreamingContext.State属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。