一个微不足道的课程:
public class TestClass : System.ServiceModel.Security.SecurityCredentialsManager { }
当使用csc针对MS CompactFramework v3.5进行编译时,将导致dll无法通过peverify,从而导致"类型加载失败"。
从SecurityCredentialsManager类继承这似乎是一个问题,它是MS提供的框架的一部分,但我似乎无法确定为什么这个类有问题。
反射显示此类的签名为:
public abstract class SecurityCredentialsManager
{
// Methods
protected SecurityCredentialsManager() { }
internal static SecurityTokenManager CreateSecurityTokenManager(SecurityCredentialsManager credMgr) { }
}
或在IL中:
.class public abstract auto ansi beforefieldinit SecurityCredentialsManager
extends [mscorlib]System.Object
{
.method family hidebysig specialname rtspecialname instance void .ctor() cil managed { }
.method assembly hidebysig static class System.IdentityModel.Selectors.SecurityTokenManager CreateSecurityTokenManager(class System.ServiceModel.Security.SecurityCredentialsManager credMgr) cil managed { }
}
继承此System.ServiceModel程序集中的其他类不会导致错误,因此它不是程序集解析问题。
我已尝试在VS 2005和VS 2017命令窗口中运行此脚本,但它似乎不依赖于VS版本。
这是工具链调用的成绩单,证明了错误:
H:\git\Test>TYPE Test.cs
namespace Test
{
public class TestClass : System.ServiceModel.Security.SecurityCredentialsManager { }
}
H:\git\Test>Csc.exe /noconfig /nostdlib+ /platform:AnyCpu /reference:"C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\mscorlib.dll" /reference:"C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.ServiceModel.dll" /target:library Test.cs
Microsoft (R) Visual C# Compiler version 2.7.0.62715 (db02128e)
Copyright (C) Microsoft Corporation. All rights reserved.
H:\git\Test>peverify.exe Test.dll
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
[token 0x02000002] Type load failed.
1 Error(s) Verifying Test.dll
H:\git\Test>ildasm.exe /TEXT /ALL /CAVERBAL Test.dll
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----- DOS Header:
// Magic: 0x5a4d
// Bytes on last page: 0x0090
// Pages in file: 0x0003
// Relocations: 0x0000
// Size of header (paragraphs):0x0004
// Min extra paragraphs: 0x0000
// Max extra paragraphs: 0xffff
// Initial (relative) SS: 0x0000
// Initial SP: 0x00b8
// Checksum: 0x0000
// Initial IP: 0x0000
// Initial (relative) CS: 0x0000
// File addr. of reloc table: 0x0040
// Overlay number: 0x0000
// OEM identifier: 0x0000
// OEM info: 0x0000
// File addr. of COFF header: 0x0080
// ----- COFF/PE Headers:
// Signature: 0x00004550
// ----- COFF Header:
// Machine: 0x014c
// Number of sections: 0x0003
// Time-date stamp: 0x5b182555
// Ptr to symbol table: 0x00000000
// Number of symbols: 0x00000000
// Size of optional header: 0x00e0
// Characteristics: 0x2022
// ----- PE Optional Header (32 bit):
// Magic: 0x010b
// Major linker version: 0x30
// Minor linker version: 0x00
// Size of code: 0x00000400
// Size of init.data: 0x00000600
// Size of uninit.data: 0x00000000
// Addr. of entry point: 0x000022d2
// Base of code: 0x00002000
// Base of data: 0x00004000
// Image base: 0x10000000
// Section alignment: 0x00002000
// File alignment: 0x00000200
// Major OS version: 0x0004
// Minor OS version: 0x0000
// Major image version: 0x0000
// Minor image version: 0x0000
// Major subsystem version: 0x0004
// Minor subsystem version: 0x0000
// Size of image: 0x00008000
// Size of headers: 0x00000200
// Checksum: 0x00000000
// Subsystem: 0x0003
// DLL characteristics: 0x8540
// Size of stack reserve: 0x00100000
// Size of stack commit: 0x00001000
// Size of heap reserve: 0x00100000
// Size of heap commit: 0x00001000
// Loader flags: 0x00000000
// Directories: 0x00000010
// 0x00000000 [0x00000000] address [size] of Export Directory:
// 0x00002280 [0x0000004f] address [size] of Import Directory:
// 0x00004000 [0x00000298] address [size] of Resource Directory:
// 0x00000000 [0x00000000] address [size] of Exception Directory:
// 0x00000000 [0x00000000] address [size] of Security Directory:
// 0x00006000 [0x0000000c] address [size] of Base Relocation Table:
// 0x00000000 [0x00000000] address [size] of Debug Directory:
// 0x00000000 [0x00000000] address [size] of Architecture Specific:
// 0x00000000 [0x00000000] address [size] of Global Pointer:
// 0x00000000 [0x00000000] address [size] of TLS Directory:
// 0x00000000 [0x00000000] address [size] of Load Config Directory:
// 0x00000000 [0x00000000] address [size] of Bound Import Directory:
// 0x00002000 [0x00000008] address [size] of Import Address Table:
// 0x00000000 [0x00000000] address [size] of Delay Load IAT:
// 0x00002008 [0x00000048] address [size] of CLR Header:
// Image sections:
// .text
// 0x000002d8 Virtual Size
// 0x00002000 Virtual Address
// 0x00000400 Size of Raw Data
// 0x00000200 Pointer to Raw Data
// 0x00000000 Pointer to Relocations
// 0x00000000 Pointer to Linenumbers
// 0x0000 Number of Relocations
// 0x0000 Number of Linenumbers
// 0x60000020 Characteristics
// CNT_CODE
// MEM_EXECUTE
// MEM_READ
// .rsrc
// 0x00000298 Virtual Size
// 0x00004000 Virtual Address
// 0x00000400 Size of Raw Data
// 0x00000600 Pointer to Raw Data
// 0x00000000 Pointer to Relocations
// 0x00000000 Pointer to Linenumbers
// 0x0000 Number of Relocations
// 0x0000 Number of Linenumbers
// 0x40000040 Characteristics
// CNT_INITIALIZED_DATA
// MEM_READ
// .reloc
// 0x0000000c Virtual Size
// 0x00006000 Virtual Address
// 0x00000200 Size of Raw Data
// 0x00000a00 Pointer to Raw Data
// 0x00000000 Pointer to Relocations
// 0x00000000 Pointer to Linenumbers
// 0x0000 Number of Relocations
// 0x0000 Number of Linenumbers
// 0x42000040 Characteristics
// CNT_INITIALIZED_DATA
// MEM_DISCARDABLE
// MEM_READ
// Base Relocation Table
// 0x00002000 Page RVA
// 0x0000000c Block Size
// 0x00000002 Number of Entries
// Entry 1: Type 0x3 Offset 0x000002d4
// Entry 2: Type 0x0 Offset 0x00000000
// Import Address Table
// DLL : mscoree.dll
// 0x00002000 Import Address Table
// 0x000022c2 Import Name Table
// 0 Time Date Stamp
// 0 Index of First Forwarder Reference
//
// 0x0000 _CorDllMain
// Delay Load Import Address Table
// No data.
// Entry point code:
//FF 25 00 20 00 10
// ----- CLR Header:
// Header size: 0x00000048
// Major runtime version: 0x0002
// Minor runtime version: 0x0005
// 0x0000205c [0x00000224] address [size] of Metadata Directory:
// Flags: 0x00000001
// Entry point token: 0x00000000
// 0x00000000 [0x00000000] address [size] of Resources Directory:
// 0x00000000 [0x00000000] address [size] of Strong Name Signature:
// 0x00000000 [0x00000000] address [size] of CodeManager Table:
// 0x00000000 [0x00000000] address [size] of VTableFixups Directory:
// 0x00000000 [0x00000000] address [size] of Export Address Table:
// 0x00000000 [0x00000000] address [size] of Precompile Header:
// Metadata Header
// Storage Signature:
// 0x424a5342 Signature
// 0x0001 Major Version
// 0x0001 Minor Version
// 0x00000000 Extra Data Offset
// 0x0000000c Version String Length
// 'v2.0.50727' Version String
// Storage Header:
// 0x00 Flags
// 0x0005 Number of Streams
// Stream 1:
// 0x0000006c Offset
// 0x000000d0 Size
// '#~' Name
// Stream 2:
// 0x0000013c Offset
// 0x000000b4 Size
// '#Strings' Name
// Stream 3:
// 0x000001f0 Offset
// 0x00000004 Size
// '#US' Name
// Stream 4:
// 0x000001f4 Offset
// 0x00000010 Size
// '#GUID' Name
// Stream 5:
// 0x00000204 Offset
// 0x00000020 Size
// '#Blob' Name
// Metadata Stream Header:
// 0x00000000 Reserved
// 0x02 Major
// 0x00 Minor
// 0x00 Heaps
// 0x01 Rid
// 0x0000000900001447 MaskValid
// 0x000016003301fa00 Sorted
// Code Manager Table:
// default
// Export Address Table Jumps:
// No data.
// File size : 3072
// PE header size : 512 (496 used) (16.67%)
// PE additional info : 835 (27.18%)
// Num.of PE sections : 3
// CLR header size : 72 ( 2.34%)
// CLR meta-data size : 548 (17.84%)
// CLR additional info : 0 ( 0.00%)
// CLR method headers : 1 ( 0.03%)
// Managed code : 8 ( 0.26%)
// Data : 1536 (50.00%)
// Unaccounted : -440 (-14.32%)
// Num.of PE sections : 3
// .text - 1024
// .rsrc - 1024
// .reloc - 512
// CLR meta-data size : 548
// Module - 1 (10 bytes)
// TypeDef - 2 (28 bytes) 0 interfaces, 0 explicit layout
// TypeRef - 3 (18 bytes)
// MethodDef - 1 (14 bytes) 0 abstract, 0 native, 1 bodies
// MemberRef - 2 (12 bytes)
// CustomAttribute- 1 (6 bytes)
// Assembly - 1 (22 bytes)
// AssemblyRef - 2 (40 bytes)
// Strings - 179 bytes
// Blobs - 32 bytes
// UserStrings - 4 bytes
// Guids - 16 bytes
// Uncategorized - 167 bytes
// CLR method headers : 1
// Num.of method bodies - 1
// Num.of fat headers - 0
// Num.of tiny headers - 1
// Managed code : 8
// Ave method size - 8
// Classes defined in this module:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Class TestClass (public) (auto) (ansi)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Metadata version: v2.0.50727
.assembly extern /*23000001*/ mscorlib
{
.publickeytoken = (96 9D B8 05 3D 33 22 AC ) // ....=3".
.ver 3:5:0:0
}
.assembly extern /*23000002*/ retargetable System.ServiceModel
{
.publickeytoken = (96 9D B8 05 3D 33 22 AC ) // ....=3".
.ver 3:5:0:0
}
.assembly /*20000001*/ Test
{
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom /*0C000001:0A000001*/ instance void [mscorlib/*23000001*/]System.Diagnostics.DebuggableAttribute/*01000001*/::.ctor(valuetype [mscorlib/*23000001*/]System.Diagnostics.DebuggableAttribute/*01000001*//DebuggingModes/*01000002*/) /* 0A000001 */
// = {int32(263)}
// // = ( 01 00 07 01 00 00 00 00 )
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module Test.dll
// MVID: {4D00E592-C83B-48AC-9DD0-AEF1C5C0B337}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00460000
// =============== CLASS MEMBERS DECLARATION ===================
.class /*02000002*/ public auto ansi beforefieldinit Test.TestClass
extends [System.ServiceModel/*23000002*/]System.ServiceModel.Security.SecurityCredentialsManager/*01000003*/
{
.method /*06000001*/ public hidebysig specialname rtspecialname
instance void .ctor() cil managed
// SIG: 20 00 01
{
// Method begins at RVA 0x2050
// Code size 8 (0x8)
.maxstack 8
IL_0000: /* 02 | */ ldarg.0
IL_0001: /* 28 | (0A)000002 */ call instance void [System.ServiceModel/*23000002*/]System.ServiceModel.Security.SecurityCredentialsManager/*01000003*/::.ctor() /* 0A000002 */
IL_0006: /* 00 | */ nop
IL_0007: /* 2A | */ ret
} // end of method TestClass::.ctor
} // end of class Test.TestClass
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************