0

I am facing a weird exception: Google cannot find anything about it (which is quite rare).

The code:

new Query<TElement>(/* ... arguments ...*/);

Thew an exception

System.TypeLoadException

Collectible type 'L.Caching.Query`1[<>transientClass1]' may not have Thread or Context static members.

I finally solved that issue, but this worth telling a bit google about it! (I will answer this question)

Let me put things in their context:

  • I am calling the "new" that threw the exception from a runtime compiled lambda.
  • The class taking place of TElement, e.g. <>transientClass1, is a dynamically generated class (using Reflection.Emit) - quite inofensive: it is the equivalent of what generates the compiler when writing an anonymous type new{a=thing, b=otherThing}
  • This "anonmyous" class works well in many other parts of my application (dont think about wrong IL emission)
  • Query<T> is just another generic class.

What could cause that exception ?

4

1 回答 1

0

答案部分在“用于动态类型生成的可收集程序集”的最后一节中

线程静态数据不支持线程静态变量。

但这部分是不够的,因为该类<>transientClass1没有声明任何线程静态数据。

但是Query<T>确实如此。因此,因为<>transientClass1是可收藏的组件,所以Query< <>transientClass1>必须符合相同的要求(因为它也是可收藏的)。

这就是为什么Query<T>可能也可以成功使用 elsewere <>transientClass1,但是将两者混合会引发此异常。

为了解决这个问题,我只需将我的线程静态字段移到另一个类中(并在 中安全使用Query<T>)。

于 2013-11-04T20:07:55.763 回答