Microsoft MVP성태의 닷넷 이야기
.NET : 67. System.Drawing.Color 구조체 직렬화 방법 [링크 복사], [링크+제목 복사],
조회: 13875
글쓴 사람
정성태 (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)
269정성태5/4/200611926Visual Studio 2005 Team System Level 200 Course Material
268정성태5/4/200612334Microsoft Component Installer Software Development Kit for Windows (x86) Version 2.0
267정성태5/4/200612317101 Code Samples for Visual Basic 2005
266정성태5/3/200614278Consolas Font Pack for Microsoft Visual Studio 2006 [2]
264정성태5/1/200613744Team System for C++ Developers
262정성태5/1/200623821Registry 등록 없이 COM 개체 사용 [1]파일 다운로드2
261정성태4/30/2006134674GB 메모리 인식 [2]
259정성태4/30/200612207Building ASP.NET 2.0 Web Sites Using Web Standards
260정성태4/30/200611681    답변글 XHTML Validator Modulle
258정성태4/29/200612216WebServiceStudio 2.0
263정성태5/1/200611718    답변글 Calling an Arbitrary Web Service
257정성태4/29/200611673TypeForwardedToAttribute
256정성태4/29/200611890Windows API - Using Condition Variables
252정성태4/26/200611904한글 관련 메세지 정리파일 다운로드1
251정성태4/26/200612553IIS 7.0 에서 소개되는 새로운 기능들 [1]
250정성태4/25/200613187툴 소개: VisualNDepend
249정성태4/25/200619191Integrating a debugger into Reflector
265정성태5/1/200613484    답변글 Deblector: First Version
285정성태5/15/200611492    답변글 New Deblector version ( an Add-In to debug with reflector )
248정성태4/21/200611837VS.NET 2005 Add-In : CSS Properties Window
247정성태4/21/200611712VS.NET 2005 Add-In : Spell Checker for HTML and ASP.NET pages
244정성태4/19/200611611SQL Server 2005 Service Pack 1
241정성태4/19/200611122VSTS Annotations for C++: Beyond Just Documenting Method Behavior [1]파일 다운로드1
240정성태4/18/200611714Accessing System Power and Network Status Using SENS
253정성태4/29/200611947    답변글 [HowToUse]: Accessing System Power and Network Status Using SENS
239정성태4/18/200611549Display a Web Page in a Plain C Win32 Application
... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...