Microsoft MVP성태의 닷넷 이야기
.NET : 67. System.Drawing.Color 구조체 직렬화 방법 [링크 복사], [링크+제목 복사],
조회: 13866
글쓴 사람
정성태 (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)
239정성태4/18/200611549Display a Web Page in a Plain C Win32 Application
238정성태4/17/200611466ASP.NET 2.0 의 Provider 관련 쪽 소스 공개
237정성태4/17/200611156Microsoft Application Verifier
236정성태4/17/200612405IIS6 - IE static resource caching problem
235정성태4/13/200611088"Atlas" Control Toolkit for April CTP Released
234정성태4/13/200611426Access Visual Studio 2005 Team System From Macintosh and UNIX Systems, and from within the Eclipse IDE
233정성태4/13/200611105More Workflow Videos
232정성태4/13/200612098Microsoft Visual C++ 2005 Redistributable Package
231정성태4/13/200611620Windows Presentation Foundation Hands-On-Labs - February 2006 CTP
230정성태4/13/200611458Microsoft Management Model Designer
229정성태4/13/200612454Discovery Wizard for SharePoint
228정성태4/13/200611986TeamPlain Web Access for Team System
227정성태4/13/200611826Scrum for Team System Released
226정성태4/13/200611729Visual Studio Team Foundation Server MSSCCI Provider Version 1.0
224정성태4/13/200611292VC++ - GroupLab Component Library
223정성태4/13/200610419MSBuild Community Tasks Project releases new version
225정성태4/13/20069825    답변글 Visual Studio 2005 Web Application Projects (RC1)
222정성태4/13/200610880Visual Studio 2005 Security Features and Tools
220정성태6/8/200610784Shared Source Common Language Infrastructure 2.0 Release [1]
219정성태8/4/200610624Simple List Extensions Specification
218정성태4/13/2006112343rd party library - EZShellExtensions.Net 1.0 [1]
217정성태4/13/200611017Bug Details: GC fails to load in server mode if config file contains none ASCII characters
215정성태4/13/200611164http://www.400plusdifferences.com/
214정성태4/13/20069642Team Foundation Server, Biztalk 2006
216정성태4/13/200611024    답변글 Team Foundation Server 시험판 공개 다운로드
213정성태4/13/200611108Introduction to the MSN Messenger Activity SDK
... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...