Exemplo n.º 1
0
        /// <summary>
        /// Creates a Custom Model assembly with given name and extended type definitions and saves it to assemblyName.dll
        /// </summary>
        /// <param name="assemblyName">The name of the assembly (will be saved as assemblyName.dll)</param>
        /// <param name="extendedTables">The enumeration of extended tables to be implemented</param>
        public static void CreateCustomModelAssembly(string assemblyName, IEnumerable<ExtendedTable> extendedTables)
        {
            string assemblyFileName = assemblyName + ".dll";
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyFileName, assemblyFileName);

            var derivedTypes = new List<Type>();

            var tableMappings = new TableMappings();
            foreach (var extendedTable in extendedTables)
            {
                var tableMapping = new TableMapping(extendedTable.TableName);
                foreach (var extendedType in extendedTable.Types)
                {
                    var derivedType = CreateDerivedType(moduleBuilder, extendedTable, extendedType);

                    derivedTypes.Add(derivedType);

                    tableMapping.Add(new TypeMapping(extendedType.BaseType, derivedType));
                }
                tableMappings.Add(tableMapping);
            }
            SerializeMappings(assemblyName, tableMappings);
            assemblyBuilder.Save(assemblyFileName);
        }
 static TableMappingManager()
 {
     TableMappings = DeserializeMappings(Properties.Settings.Default.CustomModelAssembly);
     BaseToTableMapping = new ConcurrentDictionary<Type, TableMapping>(
                from tableMapping in TableMappings.Mappings
                from typeMapping in tableMapping.Mappings
                select new KeyValuePair<Type, TableMapping>(Type.GetType(typeMapping.BaseType + ", " + typeMapping.BaseAssembly), tableMapping)
         );
     BaseToExtended = new ConcurrentDictionary<Type, Type>(
                from tableMapping in TableMappings.Mappings
                from typeMapping in tableMapping.Mappings
                select new KeyValuePair<Type, Type>(Type.GetType(typeMapping.BaseType + ", " + typeMapping.BaseAssembly), Type.GetType(typeMapping.ExtendedType + ", " + typeMapping.ExtendedAssembly))
         );
 }
 static TableMappingManager()
 {
     TableMappings      = DeserializeMappings(Properties.Settings.Default.CustomModelAssembly);
     BaseToTableMapping = new ConcurrentDictionary <Type, TableMapping>(
         from tableMapping in TableMappings.Mappings
         from typeMapping in tableMapping.Mappings
         select new KeyValuePair <Type, TableMapping>(Type.GetType(typeMapping.BaseType + ", " + typeMapping.BaseAssembly), tableMapping)
         );
     BaseToExtended = new ConcurrentDictionary <Type, Type>(
         from tableMapping in TableMappings.Mappings
         from typeMapping in tableMapping.Mappings
         select new KeyValuePair <Type, Type>(Type.GetType(typeMapping.BaseType + ", " + typeMapping.BaseAssembly), Type.GetType(typeMapping.ExtendedType + ", " + typeMapping.ExtendedAssembly))
         );
 }
Exemplo n.º 4
0
 private static void SerializeMappings(string assemblyName, TableMappings mapper)
 {
     using (var xmlWriter = XmlWriter.Create(assemblyName + ".xml"))
     {
         var serializer = new DataContractSerializer(mapper.GetType());
         serializer.WriteObject(xmlWriter, mapper);
     }
 }