Beispiel #1
0
        private void Initialise()
        {
            var newTypeName     = Guid.NewGuid().ToString();
            var assemblyName    = new AssemblyName(newTypeName);
            var dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            var dynamicModule   = dynamicAssembly.DefineDynamicModule("Main");
            var dynamicType     = dynamicModule.DefineType(newTypeName,
                                                           TypeAttributes.Public |
                                                           TypeAttributes.Class |
                                                           TypeAttributes.AutoClass |
                                                           TypeAttributes.AnsiClass |
                                                           TypeAttributes.BeforeFieldInit |
                                                           TypeAttributes.AutoLayout,
                                                           typeof(T));

            dynamicType.DefineDefaultConstructor(MethodAttributes.Public |
                                                 MethodAttributes.SpecialName |
                                                 MethodAttributes.RTSpecialName);

            foreach (var property in Properties)
            {
                AddProperty(dynamicType, property.Key, property.Value);
            }

            GeneratedType = dynamicType.CreateType();

            foreach (var property in Properties)
            {
                var propertyInfo = GeneratedType.GetProperty(property.Key);

                var setMethod = propertyInfo.GetSetMethod();
                if (setMethod == null)
                {
                    continue;
                }
                _setMethods.Add(property.Key, setMethod);
            }
        }