runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Remove generic type limitation around collectible assemblies

Open stephentoub opened this issue 11 years ago • 3 comments

See the discussion at https://github.com/dotnet/corefx/commit/b460c2701270b4c8ff8e6aeebd8a4071b283ceda#commitcomment-14671045.

Example:

using System;
using System.Reflection;
using System.Reflection.Emit;

class MyType<T>
{
    [ThreadStatic]
    private static int t_value;
}

class Repro
{
    static void Main()
    {
        var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("dynamic_assembly"), AssemblyBuilderAccess.RunAndCollect);
        var module = assembly.DefineDynamicModule("dynamic");
        var typeBuilder = module.DefineType("Dummy");
        typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
        var dummType = typeBuilder.CreateType();

        var constructedType = typeof(MyType<>).MakeGenericType(dummType);
    }
}

fails with:

Unhandled Exception: System.TypeLoadException: Collectible type 'MyType`1[Dummy]' may not have Thread or Context static members.
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at Repro.Main() in Program.cs:line 22

This means that a developer writing a general-purpose library with generic types can't use [ThreadStatic] because it would break a consumer of the library, and/or that a developer writing a collectible assembly needs to know the implementation details of generic types being used. Neither of those is very good.

Related to https://github.com/dotnet/corefx/issues/4726.

stephentoub avatar Nov 30 '15 17:11 stephentoub

@janvorli I'm assigning this to you since you're working on collectible assemblies currently

fadimounir avatar Apr 04 '19 00:04 fadimounir

This issue no longer exists. I've removed this limitation in the past while working on unloadability.

janvorli avatar Apr 04 '19 12:04 janvorli

Note we still have this code


        // Workaround for https://github.com/dotnet/runtime/issues/4731.
        // When that's fixed, a [ThreadStatic] Stack should be added back to AllocFreeConcurrentStack<T>.

        [ThreadStatic]
        internal static Dictionary<Type, object>? t_stacks;

danmoseley avatar Apr 08 '21 16:04 danmoseley