BCL接口和beforefieldinit标志

时间:2011-07-18 11:21:51

标签: c# interface

我写了一个程序,它扫描BCL类型并检查beforefieldinit标志。所以有些接口没有beforefieldinit标志(ICollection,IEnumerable),有些接口没有。 有人知道为什么吗?为什么?

1 个答案:

答案 0 :(得分:3)

你说:

  

因此一些接口具有beforefieldinit标志(ICollection,IEnumerable)

反射器和ildasm都没有显示这个......

例如(通过ildasm):

.class interface public abstract auto ansi System.Collections.IEnumerable
{
  .custom instance void System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 01 00 00 ) 
  .custom instance void System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 34 39 36 42 30 41 42 45 2D 43 44 45 45   // ..$496B0ABE-CDEE
                                                                                        2D 31 31 64 33 2D 38 38 45 38 2D 30 30 39 30 32   // -11d3-88E8-00902
                                                                                        37 35 34 43 34 33 41 00 00 )                      // 754C43A..
} // end of class System.Collections.IEnumerable

.class interface public abstract auto ansi System.Collections.ICollection
       implements System.Collections.IEnumerable
{
  .custom instance void System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 01 00 00 ) 
} // end of class System.Collections.ICollection

.class interface public abstract auto ansi System.Collections.Generic.IEnumerable`1<+ T>
       implements System.Collections.IEnumerable
{
  .custom instance void System.Runtime.CompilerServices.TypeDependencyAttribute::.ctor(string) = ( 01 00 14 53 79 73 74 65 6D 2E 53 5A 41 72 72 61   // ...System.SZArra
                                                                                                   79 48 65 6C 70 65 72 00 00 )                      // yHelper..
} // end of class System.Collections.Generic.IEnumerable`1

.class interface public abstract auto ansi System.Collections.Generic.ICollection`1<T>
       implements class System.Collections.Generic.IEnumerable`1<!T>,
                  System.Collections.IEnumerable
{
  .custom instance void System.Runtime.CompilerServices.TypeDependencyAttribute::.ctor(string) = ( 01 00 14 53 79 73 74 65 6D 2E 53 5A 41 72 72 61   // ...System.SZArra
                                                                                                   79 48 65 6C 70 65 72 00 00 )                      // yHelper..
} // end of class System.Collections.Generic.ICollection`1

具有此标志的Comparer形成鲜明对比:

.class public auto ansi serializable sealed beforefieldinit System.Collections.Comparer
       extends System.Object
       implements System.Collections.IComparer,
                  System.Runtime.Serialization.ISerializable
{
  .custom instance void System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 01 00 00 ) 
} // end of class System.Collections.Comparer

怀疑这只是你程序中的假阳性。