Aklımda Kalası Kelimeler

* давайте работать вместе
* Zarf ve Mazruf, Zerafet(xHoyratlık) ile aynı kökten(za-ra-fe) gelir
* Bedesten
* Suç subuta ermiştir - Suç sabit olmuştur
SoapFormatter etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
SoapFormatter etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

1 Temmuz 2013 Pazartesi

Serileştirme, Serialization, C#

Temel olarak serileştirme aşağıdaki kod ile anlaşılabilir ancak birazcık daha detaylı bilgi için biraz daha aşağıdaki kod yardımcı olacaktır.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    [Serializable]
    public class Ornek
    {
        public string Adi { get; set; }

        public void calis()
        {
        }
    }

    class Program
    {
        public static Ornek o = new Ornek() { Adi = "cem" };
        private static void Main(string[] args)
        {
            json();
        }


        public static void json()
        {
            string json = JsonConvert.SerializeObject(o);
            Console.WriteLine(json);
        }

        public static void binary()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = File.Create("c:\\temp\\test.txt");
            Console.WriteLine("Serializing vector");
            formatter.Serialize(stream, o);
            stream.Close();

        }

        public static void xml(){
            XmlSerializer x = new XmlSerializer(o.GetType());
            x.Serialize(Console.Out, o);
        }
    }
}


/* ÇIKTILARIMIZ:
 * XML
 
 <?xml version="1.0" encoding="ibm857"?>
<Ornek xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://w
ww.w3.org/2001/XMLSchema">
  <Adi>cem</Adi>
</Ornek>
 
 * 
 * BINARY
 
      ÿÿÿÿ             JConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null      ConsoleApplication1.Ornek     <Adi>k__BackingField           cem 
 * 
 * JSON
 
 {"Adi":"cem"}
 */

Kodumuz şöyle olacak:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace ConsoleApplication7
{
    [Serializable]
    public class Basit
    {
        public char c = 'a';
        public byte b = (byte)'a';
        private char privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez = (char)65;

        [XmlElement(DataType = "string", ElementName="ADI")] // XSD tanımlamaları
        public string Adi = "isimsiz";

        [NonSerialized] // BinaryFormatter private'a bakmaksızın serileştirdiği için [NonSerialized] ile işaretliyoruzki, serileştirmesin
        private byte NonSerialized_isaretli_BinaryFormatter_Serilestirmez = 1;
    }

    public class Example
    {
        public static void Main()
        {
            var b = BinaryFormatla_seri_deseri_lestir();
            var s = SoapFormatla_seri_deseri_lestir();
            var x = XML_seri_deseri_lestir();
            /*
             * Binary Serialization
             * Serialization can be defined as the process of storing the state of an object to a storage medium. 
             * During this process, 
             *   the public and private fields of the object 
             *   and the name of the class, 
             *   including the assembly containing the class, 
             * are converted to a stream of bytes, which is then written to a data stream. 
             * When the object is subsequently deserialized, an exact clone of the original object is created.
             * When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. 
             * The process can be automated to a large extent, provided you are given sufficient control over the process. 
             */

            /*
             * XML and SOAP Serialization
             * XML serialization converts (serializes) the public fields and properties of an object, 
             * or the parameters and return values of methods, 
             * into an XML stream that conforms to a specific XML Schema definition language (XSD) document. 
             * XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport.
             */
        }

        private static Basit XML_seri_deseri_lestir()
        {
            Type t = Type.GetType("ConsoleApplication7.Basit");
            XmlSerializer ser = new XmlSerializer(t);
            FileStream fs = new FileStream("XmlSer.xml",FileMode.OpenOrCreate);
            ser.Serialize(fs,new Basit());
            fs.Close();

            Basit x = (Basit) ser.Deserialize(new FileStream("XmlSer.xml", FileMode.Open));
            return x;
        }

        private static Basit SoapFormatla_seri_deseri_lestir()
        {
            SoapFormatter soapFmt = new SoapFormatter();
            FileStream fs = new FileStream("basit.xml", FileMode.OpenOrCreate);
            soapFmt.Serialize(fs, new Basit());
            fs.Close();

            FileStream fs1 = new FileStream("basit.xml", FileMode.Open);
            Basit s = (Basit)soapFmt.Deserialize(fs1);
            return s;
        }

        private static Basit BinaryFormatla_seri_deseri_lestir()
        {
            BinaryFormatter binaryFmt = new BinaryFormatter();
            FileStream fs = new FileStream("basit.bin", FileMode.OpenOrCreate);
            binaryFmt.Serialize(fs, new Basit());
            fs.Close();

            FileStream fs1 = new FileStream("basit.bin", FileMode.Open);
            Basit b = (Basit)binaryFmt.Deserialize(fs1);
            return b;
        }
    }
}
ve çıktılardan önce dönen değişkenler şöyle:
Dosyalardaki çıktılar ise: BINARY Formatlı Çıktının HEX Görünümü:
SOAP XML Çıktısı:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Basit id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApplication7/ConsoleApplication7%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<c>a</c>
<b>97</b>
<privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez>A</privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez>
<Adi id="ref-3">isimsiz</Adi>
</a1:Basit>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML Çıktısı:
<?xml version="1.0"?>
<Basit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <c>97</c>
  <b>97</b>
  <ADI>isimsiz</ADI>
</Basit>

12 Ekim 2009 Pazartesi

IFormatter ve Serialization

Referans: MSDN
Örnek: chsarpnedir.com
The SoapFormatter and BinaryFormatter classes implement the IRemotingFormatter interface to support remote procedure calls (RPCs), and the IFormatter interface (inherited by the IRemotingFormatter) to support serialization of a graph of objects. The SoapFormatter class also supports RPCs with ISoapMessage objects, without using the IRemotingFormatter functionality.

During RPCs, the IRemotingFormatter interface allows the specification of two separate object graphs: the graph of objects to serialize, and an additional graph containing an array of header objects that convey information about the remote function call (for example, transaction ID or a method signature).

RPCs that use the BinaryFormatter separate into two distinct parts: method calls, which are sent to the server with the remote object containing the method called, and method responses, which are sent from the server to the client with the status and response information from the called method.

During serialization of a method call the first object of the object graph must support the IMethodCallMessage interface. To deserialize a method call, use the Deserialize method with the HeaderHandler parameter. The remoting infrastructure uses the HeaderHandler delegate to produce an object that supports the ISerializable interface. When the BinaryFormatter invokes the HeaderHandler delegate, it returns the URI of the remote object with the method that is being called. The first object in the graph returned supports the IMethodCallMessage interface.

The serialization procedure for a method response is identical to that of a method call, except the first object of the object graph must support the IMethodReturnMessage interface. To deserialize a method response, use the DeserializeMethodResponse method. To save time, details about the caller object are not sent to the remote object during the method call. These details are instead obtained from the original method call, which is passed to the DeserializeMethodResponse method in the IMethodCallMessage parameter. The first object in the graph returned by the DeserializeMethodResponse method supports the IMethodReturnMessage interface.