Exemple #1
0
        public static Type GetType(object hashKey, Type sourceType, ITypeBuilder typeBuilder)
        {
            if (hashKey == null)
            {
                throw new ArgumentNullException("hashKey");
            }
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }
            if (typeBuilder == null)
            {
                throw new ArgumentNullException("typeBuilder");
            }

            try
            {
                lock (_builtTypes)
                {
                    Type type;
                    IDictionary <object, Type> builderTable;

                    if (_builtTypes.TryGetValue(typeBuilder.GetType(), out builderTable))
                    {
                        if (builderTable.TryGetValue(hashKey, out type))
                        {
                            return(type);
                        }
                    }
                    else
                    {
                        _builtTypes.Add(typeBuilder.GetType(), builderTable = new Dictionary <object, Type>());
                    }

                    if (LoadTypes)
                    {
                        var originalAssembly = sourceType.Assembly;

                        Assembly extensionAssembly;

                        if (!_assemblies.TryGetValue(originalAssembly, out extensionAssembly))
                        {
                            extensionAssembly = LoadExtensionAssembly(originalAssembly);
                            _assemblies.Add(originalAssembly, extensionAssembly);
                        }

                        if (extensionAssembly != null)
                        {
                            type = extensionAssembly.GetType(typeBuilder.GetTypeName());

                            if (type != null)
                            {
                                builderTable.Add(hashKey, type);
                                return(type);
                            }
                        }
                    }

                    var assemblyBuilder = GetAssemblyBuilder(sourceType, typeBuilder.AssemblyNameSuffix);

                    type = typeBuilder.Build(assemblyBuilder);

                    if (type != null)
                    {
                        builderTable.Add(hashKey, type);
                        SaveAssembly(assemblyBuilder, type);
                    }

                    return(type);
                }
            }
            catch (TypeBuilderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // Convert an Exception to TypeBuilderException.
                //
                throw new TypeBuilderException(string.Format(Resources.TypeFactory_BuildFailed, sourceType.FullName), ex);
            }
        }
Exemple #2
0
        public static Type GetType(object hashKey, Type sourceType, ITypeBuilder typeBuilder)
        {
            if (hashKey == null)
            {
                throw new ArgumentNullException("hashKey");
            }
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }
            if (typeBuilder == null)
            {
                throw new ArgumentNullException("typeBuilder");
            }

            try
            {
                Hashtable builderTable = (Hashtable)_builtTypes[typeBuilder.GetType()];
                Type      type;

                if (builderTable != null)
                {
                    type = (Type)builderTable[hashKey];

                    if (type != null)
                    {
                        return(type);
                    }
                }

                lock (_builtTypes.SyncRoot)
                {
                    builderTable = (Hashtable)_builtTypes[typeBuilder.GetType()];

                    if (builderTable != null)
                    {
                        type = (Type)builderTable[hashKey];

                        if (type != null)
                        {
                            return(type);
                        }
                    }
                    else
                    {
                        _builtTypes.Add(typeBuilder.GetType(), builderTable = new Hashtable());
                    }

                    if (_loadTypes)
                    {
                        Assembly originalAssembly = sourceType.Assembly;
                        Assembly extensionAssembly;

                        if (_assemblies.Contains(originalAssembly))
                        {
                            extensionAssembly = (Assembly)_assemblies[originalAssembly];
                        }
                        else
                        {
                            extensionAssembly = LoadExtensionAssembly(originalAssembly);
                            _assemblies.Add(originalAssembly, extensionAssembly);
                        }

                        if (extensionAssembly != null)
                        {
                            type = extensionAssembly.GetType(typeBuilder.GetTypeName());

                            if (type != null)
                            {
                                builderTable.Add(hashKey, type);
                                return(type);
                            }
                        }
                    }

                    AssemblyBuilderHelper assemblyBuilder = GetAssemblyBuilder(sourceType, typeBuilder.AssemblyNameSuffix);

                    type = typeBuilder.Build(assemblyBuilder);

                    if (type != null)
                    {
                        builderTable.Add(hashKey, type);
                        SaveAssembly(assemblyBuilder, type);
                    }

                    return(type);
                }
            }
            catch (TypeBuilderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // Convert an Exception to TypeBuilderException.
                //
                throw new TypeBuilderException(
                          string.Format(Resources.TypeFactory_BuildFailed, sourceType.FullName), ex);
            }
        }