첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ CustomClass.cs

using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 커스텀 클래스
    /// </summary>
    [TypeConverter(typeof(CustomClassTypeConverter))]
    public class CustomClass
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 문자 배열 - CharacterArray

        /// <summary>
        /// 문자 배열
        /// </summary>
        public char[] CharacterArray { get; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - CustomClass(characterArray)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="characterArray">문자 배열</param>
        public CustomClass(char[] characterArray)
        {
            CharacterArray = characterArray;
        }

        #endregion
    }
}

 

728x90

 

▶ CustomClassTypeConverter.cs

using System;
using System.ComponentModel;
using System.Globalization;

namespace TestProject
{
    /// <summary>
    /// 커스텀 클래스 타입 변환자
    /// </summary>
    public class CustomClassTypeConverter : TypeConverter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 변환 가능 여부 구하기 - CanConvertFrom(context, sourceType)

        /// <summary>
        /// 해당 타입으로 변환 가능 여부 구하기
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }

        #endregion
        #region 변환하기 - ConvertFrom(context, cultureInfo, sourceValue)

        /// <summary>
        /// 변환하기
        /// </summary>
        /// <param name="context">타입 설명자 컨텍스트</param>
        /// <param name="cultureInfo">문화 정보</param>
        /// <param name="sourceValue">소스 값</param>
        /// <returns>변환 값</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo cultureInfo, object sourceValue)
        {
            string sourceString = sourceValue as string;

            if(sourceString != null)
            {
                return new CustomClass(sourceString.ToCharArray());
            }

            return base.ConvertFrom(context, cultureInfo, sourceValue);
        }

        #endregion
        #region 역변환하기 - ConvertTo(context, cultureInfo, sourceValue, targetType)

        /// <summary>
        /// 역변환하기
        /// </summary>
        /// <param name="context">타입 설명자 컨텍스트</param>
        /// <param name="cultureInfo">문화 정보</param>
        /// <param name="sourceValue">소스 값</param>
        /// <param name="targetType">타겟 타입</param>
        /// <returns>역변환 값</returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cultureInfo, object sourceValue, Type targetType)
        {
            CustomClass customClass = sourceValue as CustomClass;

            if(targetType == typeof(string) && customClass != null)
            {
                return string.Join(string.Empty, customClass.CharacterArray);
            }

            return base.ConvertTo(context, cultureInfo, sourceValue, targetType);
        }

        #endregion
    }
}

 

반응형

 

▶ Program.cs

using System;
using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            CustomClass customClass1 = new CustomClass(new [] {'t', 'e', 's', 't'});

            TypeConverter converter = TypeDescriptor.GetConverter(typeof(CustomClass));

            string value = converter.ConvertToString(customClass1); 

            Console.WriteLine(value);

            CustomClass customClass2 = converter.ConvertFrom("what") as CustomClass; 

            Console.WriteLine(string.Join(string.Empty, customClass2.CharacterArray));

        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요