public static Type?GetType(object hashKey, Type sourceType, ITypeBuilder typeBuilder)
        {
            if (hashKey == null)
            {
                throw new ArgumentNullException(nameof(hashKey));
            }
            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }
            if (typeBuilder == null)
            {
                throw new ArgumentNullException(nameof(typeBuilder));
            }

            try
            {
                lock (_builtTypes)
                {
                    Type?type;

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

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

                    type = typeBuilder.Build(assemblyBuilder);

                    if (type != null)
                    {
                        builderTable.Add(hashKey, type);
#if !NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETCOREAPP2_2
                        SaveAssembly(assemblyBuilder, type);
#endif
                    }

                    return(type);
                }
            }
            catch (TypeBuilderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // Convert an Exception to TypeBuilderException.
                //
                throw new TypeBuilderException($"Could not build the '{sourceType.FullName}' type.", ex);
            }
        }
示例#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
            {
                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);
            }
        }
示例#3
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());
                    }

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

                    type = typeBuilder.Build(sourceType, assemblyBuilder);

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

                    return(type);
                }
            }
            catch (TypeBuilderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new TypeBuilderException(string.Format(
                                                   "Could not build the '{0}' type: {1}", sourceType.FullName, ex.Message), ex);
            }
        }
示例#4
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());
					}

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

					type = typeBuilder.Build(sourceType, assemblyBuilder);

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

					return type;
				}
			}
			catch (TypeBuilderException)
			{
				throw;
			}
			catch (Exception ex)
			{
				throw new TypeBuilderException(string.Format(
					"Could not build the '{0}' type: {1}", sourceType.FullName, ex.Message), ex);
			}
		}
示例#5
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);
            }
        }