본문 바로가기

Code

(117)
Generic Dictionary Xml Serialization C# Example 1 Generic Dictionary는 Xml.Serialization.XmlSerializer을 사용해서 serialize할 수 없다. 그래서 Generic Dictionary을 XML 파일로 serialize할 수 있는 GenericDictionaryXmlSerializer 클래스를 만들어 보았다. 이 클래스는 아래 링크에 있는 코드를 응용해서 만든 것이다. http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx 위의 링크에 있는 코드는 Generic 클래스를 사용하지 않았는데, 필자의 코드는 Generic 코드를 사용한 점이 다르다. using System; using System.Collections.Generic; using System.Xml;..
DateTime 이 포함된 객체의 Serialization Xml.Serialization 을 이용해서 DateTime 멤버를 포함하고 있는 객체의 Serialization을 시도해보았다. 결과는 만족스러웠다. 아무런 문제 없이 Serialization이 잘 이루어졌다. 아래는 예제 코드이다. 아래에서 Chrono 클래스는 string field 와 DateTime field를 가지고 있다. using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; namespace SerializationTest { class Program { static void Main(string[] args) { Chrono c1 = new Chrono(); Chrono..
List<string> 객체의 XML Serialization 사용 예제 List 객체도 XML Serialization이 가능하다. 아래는 그 예제이다. 별로 어렵지 않은 예제이니 잘 살펴보기 바란다. using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; namespace SerializationTest { class Program { static void Main(string[] args) { List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); XmlSerializer myXmlSerializer = new XmlSerializer(typeof(List)..
XML Serialization 사용법과 예제 Xml.Serialization 을 사용하면 XML 형식으로 Serialization 할 수 있다. 아래 예제를 보자. 아래 예제는 string 객체를 Serialization 한 예제이다. XmlSerializer 와 XmlWriter, XmlReader 를 사용해서 Serialize & Deserialize 한다. using System; using System.Xml; using System.Xml.Serialization; namespace SerializationTest { class Program { static void Main(string[] args) { string myName = "John Dow"; using (XmlWriter writer = XmlWriter.Create("ser..
Path 클래스를 사용해서 원하는 파일의 위치를 지정하는 방법 현재 실행 중인 어플리케이션과 같은 폴더에 있는 파일이나, 하위 폴더에 있는 파일의 위치를 지정하고자 한다면, Path 클래스를 사용하면 된다. 아래는 간단한 예제이다. Application.ExecutablePath 은 현재 실행 중인 어플리케이션의 위치를 반환한다. Path.GetDirectoryName 은 path에서 폴더 값만 반환한다. Path.Combine 은 두개의 path를 결합한다. using System; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }..
Windows Form Application의 Path를 구하는 방법 Windows Form Application의 path를 구하는 방법은 간단하다. Application.ExecutablePath 를 사용하면 된다. 아래는 사용 예제이다. using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = Application.ExecutablePath; } } }
실행중인 application의 path를 구하는 방법 아래는 실행중인 어플리케이션의 현재 위치를 구하는 간단한 예제이다. 이 예제는 System.Reflection 을 사용했다. Assembly.GetExecutingAssembly() 은 현재의 Assembly를 반환한다. Location Property는 Assembly의 위치를 반환한다. using System; using System.Reflection; namespace Test { class Program { static void Main(string[] args) { string myPath = Assembly.GetExecutingAssembly().Location; Console.WriteLine(myPath); Console.Read(); } } }
BinaryFormatter.Serialize 사용법 BinaryFormatter.Serialize 와 BinaryFormatter.Deserialize 를 사용한 예제입니다. 아래 예제를 보면 [Serializable] Attribute를 사용한 Name 클래스만 Serialize & Deserialize 한 것을 알 수 있습니다. 이 예제는 사실 BinaryFormatter.Serialize & BinaryFormatter.Deserialize 의 버그 여부를 조사하기 위해서 만들었습니다. Serialize 후 일부 데이터를 변경하고, 다시 Deserialize 했을 때, 혹시 버그가 있나 검사해봤는데, 정상이었습니다. BinaryFormatter.Serialize & BinaryFormatter.Deserialize 는 generic static me..