刘磊
2025-06-09 dabbcafc629ef87d11ba55ef8cc1cdc776c047d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.IE.Core;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.Helper
{
    public static class ExportHelper
    {
        public static Type CreateDynamicClass(this PropertyInfo[] propertyInfos)
        {
            string className = "DynamicClass";
            AssemblyName assemblyName = new AssemblyName("WIDESEA_Model.Models");
            AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);//定义具有指定名称和访问权限的动态程序集
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");//在此程序集中定义命名的暂时动态模块
            TypeBuilder typeBuilder = moduleBuilder.DefineType(className, TypeAttributes.Public);//TypeBuilder:在运行时定义并创建类的新实例
 
            Type attributeType1 = typeof(ExcelExporterAttribute);
            MethodInfo [] methodInfos = attributeType1.GetMethods(BindingFlags.Public);
            ConstructorInfo[] constructorInfos2 = attributeType1.GetConstructors();
            ConstructorInfo constructorInfo2 = constructorInfos2[0];
            CustomAttributeBuilder customAttributeBuilder2 = new CustomAttributeBuilder(constructorInfo2, new object[] { });
 
            typeBuilder.SetCustomAttribute(customAttributeBuilder2);
 
            foreach (var property in propertyInfos)
            {
                string propertyName = property.Name;
                Type propertyType = property.PropertyType;
 
                FieldBuilder fieldBuilder = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
                PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
 
                SugarColumn sugarColumn = property.GetCustomAttribute<SugarColumn>();
                if (sugarColumn != null)
                {
                    Type attributeType = typeof(ExporterHeaderAttribute);
                    ConstructorInfo[] constructorInfos = attributeType.GetConstructors();
                    ConstructorInfo constructorInfo = constructorInfos[0];
                    CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(constructorInfo, new object[] { sugarColumn.ColumnDescription, 11f, null, false, true, true, 0, KnownColor.Empty });
                    propertyBuilder.SetCustomAttribute(customAttributeBuilder);
                }
 
                MethodAttributes getSetAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
 
                MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_" + propertyName, getSetAttributes, propertyType, Type.EmptyTypes);
                ILGenerator getIL = getMethodBuilder.GetILGenerator();
                getIL.Emit(OpCodes.Ldarg_0);
                getIL.Emit(OpCodes.Ldfld, fieldBuilder);
                getIL.Emit(OpCodes.Ret);
 
                MethodBuilder setMethodBuilder = typeBuilder.DefineMethod("set_" + propertyName, getSetAttributes, null, new Type[] { propertyType });
                ILGenerator setIL = setMethodBuilder.GetILGenerator();
                setIL.Emit(OpCodes.Ldarg_0);
                setIL.Emit(OpCodes.Ldarg_1);
                setIL.Emit(OpCodes.Stfld, fieldBuilder);
                setIL.Emit(OpCodes.Ret);
 
                propertyBuilder.SetGetMethod(getMethodBuilder);
                propertyBuilder.SetSetMethod(setMethodBuilder);
 
 
            }
 
            Type generatedType = typeBuilder.CreateType();
            return generatedType;
        }
 
        public static void SetProperty(object instance, string propertyName, object value)
        {
            Type type = instance.GetType();
            PropertyInfo propertyInfo = type.GetProperty(propertyName);
            propertyInfo.SetValue(instance, value);
        }
 
        public static object GetProperty(object instance, string propertyName)
        {
            Type type = instance.GetType();
            PropertyInfo propertyInfo = type.GetProperty(propertyName);
            return propertyInfo.GetValue(instance);
        }
 
        public static void SetValue<T>(object instance, T value)
        {
            Type type = instance.GetType();
 
            PropertyInfo[] propertyInfos = typeof(T).GetProperties();
            for (int j = 0; j < propertyInfos.Length; j++)
            {
                PropertyInfo propertyInfo = type.GetProperty(propertyInfos[j].Name);
                object obj = propertyInfos[j].GetValue(value);
                propertyInfo.SetValue(instance, obj);
                ExporterHeaderAttribute exporterHeaderAttribute = propertyInfo.GetCustomAttribute<ExporterHeaderAttribute>();
            }
 
        }
 
    }
}