Microsoft MVP성태의 닷넷 이야기
.NET : 67. System.Drawing.Color 구조체 직렬화 방법 [링크 복사], [링크+제목 복사],
조회: 13876
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 


System.Drawing.Color 구조체 직렬화 방법


얼마 전, Color 구조체를 포함한 특정 클래스의 값들이 당연히 직렬화 될 거라고 믿고 진행하는 도중 뜻하지 않게 만났던 문제였습니다. 해당 직렬화를 담당했던 래퍼 메서드가 제가 만든 것이어서, 처음에는 래퍼 메서드에 오류가 있다는 식으로 보고가 들어온 것입니다.

그럴 수밖에 없었던 것이, 아래의 정의를 보시면 아시겠지만 Color는 표면상으로 볼때, 직렬화에 전혀 문제가 없어보이기 때문입니다.

[SerializableAttribute]
[TypeConverterAttribute(typeof(ColorConverter))]
public struct Color

검색을 해보니, 일단은 직렬화가 안되는 것이 당연했습니다.

Topic:  XmlSerializer does not serialize Color
; http://www.forum.miraplacid.com/topic.pl?topic_id=183

Color의 RGB 정의가 각각 다음과 같이 read-only 속성이었기 때문입니다.

public byte R { get; }
public byte G { get; }
public byte B { get; }

오호... 왜? 읽기 전용 속성으로 된 것일까요? 잠시 고민해 보았지만, 그렇게 만든 의도에 대해서는 전혀 이해가 되질 않았습니다. 뭐, 아무렴 어떻습니까! 지금 중요한 것은 해당 속성들이 Read-only라는 것이니. 그렇다면, Color 속성에 대해서는 별도 속성 또는 문자열 등의 방법을 통해서 직렬화해야 해야 할 것 같습니다.

혹시나 싶어서, 이를 위한 좀 더 매끄러운 해결책이 없을까 싶어 좀 더 검색을 해보았는데, 그나마 가장 적절한 해법을 제시해 주는 다음의 토픽을 발견할 수 있었습니다.

System.Drawing.Color serialization 
; http://devintelligence.com/blogs/netadventures/archive/2005/11/28/1124.aspx

간단하니, 아래에 코드를 실어봅니다.

using System;
using System.Drawing;
using System.Xml.Serialization;

[Serializable]
public class Person
{
  private Color _HairColor = Color.Empty;
    
  public Person()
  {
  }
    
  [XmlIgnoreAttribute()]
  public Color HairColor
  {
    get
    {
      return _HairColor;
    }
    set
    {
      _HairColor = value;
    }
  }
    
  [XmlElement("HairColor")]
  public string HairColorHtml
  {
    get { return ColorTranslator.ToHtml(_HairColor); }
    set { _HairColor = ColorTranslator.FromHtml( value ); }
  }    
}

썩 마음에 들진 않지만, 그런대로 해결이 될 것 같습니다. 그런데, 다시 한번 궁금해집니다. 왜 R,G,B 속성을 Read-only로만 해두었을까요?



[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/10/2021]


비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
160정성태4/13/200611643MS 인증서버 : 인증서 유효 기간 기본값 변경
157정성태4/13/200611125Converting a managed PDB into a XML file
158정성태4/13/200610473    답변글 [추가]: A SymWriter sample is coming...
156정성태4/13/200611474Method Call Interception (MCI) in C++
155정성태4/13/200610981Web Project Conversion Wizard
154정성태12/21/200511718Interprocess Communication Using the Running Object Table (ROT)파일 다운로드1
153정성태4/13/200611304Assembly loading failure is expensive [1]
152정성태4/13/200611726ASCX 컨트롤을 커스텀 컨트롤로 사용하는 방법.
151정성태4/13/200610647TechNet 세미나 목록
150정성태4/13/200610744SCDL Tool [2]
149정성태4/13/200610800Disk (based) Output Cache
147정성태4/13/200610686CLR Profiler for the .NET Framework 2.0
146정성태4/13/200611644Scriptomatic Version 2.0 by the Microsoft Scripting Guys
145정성태4/13/200611143Windows Server 2003 R2 [2]
144정성태4/13/200611608WTL(Windows Template Library) 7.5
143정성태4/13/200611867.NET 1.1 에서 WebService Client 의 KeepAlive = false; 설정
142정성태4/13/200611055Virtual Server 2005 R2 (x86, x64) EE 180-day Evaluation [2]
141정성태4/13/200611694.NET 표준개발 가이드와 Microsoft.Framework 공개
140정성태4/13/200610156PAINT.NET
139정성태11/30/20051117812월 세미나 행사
137정성태11/17/200510605WSE 3.0 정식 버전
162정성태4/13/200611254    답변글 [추가]: Web Service Security Guide
136정성태11/17/200510892Creating an Outlook My.Blogs Managed Code Add-in
138정성태11/24/200510326    답변글 Ceating an Outlook Business Contact Assistant Add-in with Visual Studio Tools for Office
135정성태11/16/200510675What's port 445 used for in Windows 2000/XP?파일 다운로드1
134정성태11/15/200510665Microsoft SQL Server Management Studio Express - Community Technical Preview (CTP) November 2005 [1]
... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...