■ DataTable 객체에서 동적 클래스 컬렉션 구하기 예제

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

DataTable dataTable = new DataTable();

 

dataTable.Columns.Add("ID"  , typeof(string));

dataTable.Columns.Add("Name", typeof(string));

 

dataTable.Rows.Add("0001", "김철수");

dataTable.Rows.Add("0002", "이순희");

dataTable.Rows.Add("0003", "홍길동");

 

ObservableCollection<object> collection = GetCollection(dataTable);

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

 

■ DataTable 객체에서 동적 클래스 컬렉션 구하기

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

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.ComponentModel;

using System.Data;

using System.Reflection;

using System.Reflection.Emit;

 

#region 동적 속성 추가하기 - AddDynamicProperty(typeBuilder, fieldName, propertyName, propertyType)

 

/// <summary>

/// 동적 속성 추가하기

/// </summary>

/// <param name="typeBuilder">타입 빌더</param>

/// <param name="fieldName">필드명</param>

/// <param name="propertyName">속성명</param>

/// <param name="propertyType">속성 타입</param>

public void AddDynamicProperty(TypeBuilder typeBuilder, string fieldName, string propertyName, Type propertyType)

{

    FieldBuilder fieldBuilder = typeBuilder.DefineField(fieldName, propertyType, FieldAttributes.Private);

 

    PropertyBuilder propertyBuilder = typeBuilder.DefineProperty

    (

        propertyName,

        System.Reflection.PropertyAttributes.HasDefault,

        propertyType,

        null

    );

 

    MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

 

    MethodBuilder getMethodBuilder = typeBuilder.DefineMethod

    (

        string.Format("get_{0}", propertyName),

        methodAttributes,

        propertyType,

        Type.EmptyTypes

    );

 

    ILGenerator getILGenerator = getMethodBuilder.GetILGenerator();

 

    getILGenerator.Emit(OpCodes.Ldarg_0            );

    getILGenerator.Emit(OpCodes.Ldfld, fieldBuilder);

    getILGenerator.Emit(OpCodes.Ret                );

 

    MethodBuilder setMethodBuilder = typeBuilder.DefineMethod

    (

        string.Format("set_{0}", propertyName),

        methodAttributes,

        null,

        new Type[] { propertyType }

    );

 

    ILGenerator setILGenerator = setMethodBuilder.GetILGenerator();

 

    setILGenerator.Emit(OpCodes.Ldarg_0            );

    setILGenerator.Emit(OpCodes.Ldarg_1            );

    setILGenerator.Emit(OpCodes.Stfld, fieldBuilder);

    setILGenerator.Emit(OpCodes.Ret                );

 

    propertyBuilder.SetGetMethod(getMethodBuilder);

    propertyBuilder.SetSetMethod(setMethodBuilder);

}

 

#endregion

 

#region 동적 클래스 타입 구하기 - GetDynamicClassType(dataTable)

 

/// <summary>

/// 동적 클래스 타입 구하기

/// </summary>

/// <param name="dataTable">데이터 테이블</param>

/// <returns>동적 클래스 타입</returns>

public Type GetDynamicClassType(DataTable dataTable)

{

    AssemblyName assemblyName = new AssemblyName("DynamicAssembly");

 

    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly

    (

        assemblyName,

        System.Reflection.Emit.AssemblyBuilderAccess.Run

    );

 

    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");

 

    TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicClass", TypeAttributes.Public);

 

    foreach(DataColumn dataColumn in dataTable.Columns)

    {

        AddDynamicProperty(typeBuilder, "_" + dataColumn.ColumnName, dataColumn.ColumnName, dataColumn.DataType);

    }

 

    Type dynamicClassType = typeBuilder.CreateType();

 

    return dynamicClassType;

}

 

#endregion

 

#region 컬렉션 구하기 - GetCollection(dataTable)

 

/// <summary>

/// 컬렉션 구하기

/// </summary>

/// <param name="dataTable">데이터 테이블</param>

/// <returns>컬렉션</returns>

public ObservableCollection<object> GetCollection(DataTable dataTable)

{

    Type dynamicClassType = GetDynamicClassType(dataTable);

 

    ObservableCollection<object> collection = new ObservableCollection<object>();

 

    foreach(DataRow dataRow in dataTable.Rows)

    {

        object instance = Activator.CreateInstance(dynamicClassType);

 

        foreach(DataColumn dataColumn in dataTable.Columns)

        {

            object value = dataRow[dataColumn];

 

            if(value != DBNull.Value)

            {

                dynamicClassType.InvokeMember(dataColumn.ColumnName, BindingFlags.SetProperty, null, instance, new object[] { value });

            }

        }

 

        collection.Add(instance);

    }

 

    return collection;

}

 

#endregion

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

Posted by 사용자 icodebroker

댓글을 달아 주세요