■ XmlSerializer 클래스 : 객체 구하기

----------------------------------------------------------------------------------------------------

using System;

using System.IO;

using System.Xml.Serialization;

 

#region 객체 구하기 - GetObject(sourceType, filePath)

 

/// <summary>

/// 객체 구하기

/// </summary>

/// <param name="sourceType">소스 타입</param>

/// <param name="filePath">파일 경로</param>

/// <returns>인스턴스</returns>

public object GetObject(Type sourceType, string filePath)

{

    StreamReader streamReader;

 

    XmlSerializer xmlSerializer = new XmlSerializer(sourceType);

 

    object targetObject;

 

    try

    {

        streamReader = new StreamReader(filePath);

 

        targetObject = xmlSerializer.Deserialize(streamReader);

 

        streamReader.Close();

    }

    catch

    {

        targetObject = sourceType.GetConstructor(System.Type.EmptyTypes).Invoke(null);

    }

 

    return targetObject;

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG