@ -27,6 +27,7 @@ using System.Threading;
@@ -27,6 +27,7 @@ using System.Threading;
using ICSharpCode.Decompiler.Metadata ;
using ICSharpCode.Decompiler.IL ;
using ICSharpCode.Decompiler.DebugInfo ;
namespace ICSharpCode.Decompiler.Disassembler
{
@ -55,8 +56,15 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -55,8 +56,15 @@ namespace ICSharpCode.Decompiler.Disassembler
set = > methodBodyDisassembler . ShowMetadataTokens = value ;
}
public IDebugInfoProvider DebugInfo {
get = > methodBodyDisassembler . DebugInfo ;
set = > methodBodyDisassembler . DebugInfo = value ;
}
public bool ExpandMemberDefinitions { get ; set ; } = false ;
public IAssemblyResolver AssemblyResolver { get ; set ; }
public ReflectionDisassembler ( ITextOutput output , CancellationToken cancellationToken )
: this ( output , new MethodBodyDisassembler ( output , cancellationToken ) , cancellationToken )
{
@ -125,25 +133,27 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -125,25 +133,27 @@ namespace ICSharpCode.Decompiler.Disassembler
public void DisassembleMethod ( PEFile module , MethodDefinitionHandle handle )
{
var metadata = module . Metadata ;
var genericContext = new GenericContext ( handle , module ) ;
// write method header
output . WriteDefinition ( ".method " , new Metadata . MethodDefinition ( module , handle ) ) ;
DisassembleMethodHeaderInternal ( module , handle , metadata , genericContext ) ;
DisassembleMethodBlock ( module , handle , metadata , genericContext ) ;
output . WriteReference ( module , handle , ".method" , isDefinition : true ) ;
output . Write ( " " ) ;
DisassembleMethodHeaderInternal ( module , handle , genericContext ) ;
DisassembleMethodBlock ( module , handle , genericContext ) ;
}
public void DisassembleMethodHeader ( PEFile module , MethodDefinitionHandle handle )
{
var metadata = module . Metadata ;
var genericContext = new GenericContext ( handle , module ) ;
// write method header
output . WriteDefinition ( ".method " , new Metadata . MethodDefinition ( module , handle ) ) ;
DisassembleMethodHeaderInternal ( module , handle , metadata , genericContext ) ;
output . WriteReference ( module , handle , ".method" , isDefinition : true ) ;
output . Write ( " " ) ;
DisassembleMethodHeaderInternal ( module , handle , genericContext ) ;
}
void DisassembleMethodHeaderInternal ( PEFile module , MethodDefinitionHandle handle , MetadataReader metadata , GenericContext genericContext )
void DisassembleMethodHeaderInternal ( PEFile module , MethodDefinitionHandle handle , GenericContext genericContext )
{
var metadata = module . Metadata ;
WriteMetadataToken ( handle , spaceAfter : true ) ;
var methodDefinition = metadata . GetMethodDefinition ( handle ) ;
// .method public hidebysig specialname
@ -272,8 +282,9 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -272,8 +282,9 @@ namespace ICSharpCode.Decompiler.Disassembler
}
}
void DisassembleMethodBlock ( PEFile module , MethodDefinitionHandle handle , MetadataReader metadata , GenericContext genericContext )
void DisassembleMethodBlock ( PEFile module , MethodDefinitionHandle handle , GenericContext genericContext )
{
var metadata = module . Metadata ;
var methodDefinition = metadata . GetMethodDefinition ( handle ) ;
OpenBlock ( defaultCollapsed : isInType ) ;
@ -359,59 +370,61 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -359,59 +370,61 @@ namespace ICSharpCode.Decompiler.Disassembler
output . Write ( secdecl . Action . ToString ( ) ) ;
break ;
}
output . WriteLine ( " = {" ) ;
output . Indent ( ) ;
var blob = metadata . GetBlobReader ( secdecl . PermissionSet ) ;
if ( ( char ) blob . ReadByte ( ) ! = '.' ) {
blob . Reset ( ) ;
WriteXmlSecurityDeclaration ( blob . ReadUTF8 ( blob . RemainingBytes ) ) ;
if ( AssemblyResolver = = null ) {
output . Write ( " = " ) ;
WriteBlob ( secdecl . PermissionSet , metadata ) ;
} else {
string currentAssemblyName = null ;
string currentFullAssemblyName = null ;
if ( metadata . IsAssembly ) {
currentAssemblyName = metadata . GetString ( metadata . GetAssemblyDefinition ( ) . Name ) ;
currentFullAssemblyName = metadata . GetFullAssemblyName ( ) ;
}
int count = blob . ReadCompressedInteger ( ) ;
for ( int i = 0 ; i < count ; i + + ) {
var typeName = blob . ReadSerializedString ( ) ;
string [ ] nameParts = typeName . Split ( new [ ] { ", " } , StringSplitOptions . None ) ;
if ( nameParts . Length < 2 | | nameParts [ 1 ] = = currentAssemblyName ) {
output . Write ( "class " ) ;
output . Write ( DisassemblerHelpers . Escape ( typeName + ", " + currentFullAssemblyName ) ) ;
} else {
string [ ] typeNameParts = typeName . Split ( new [ ] { ", " } , StringSplitOptions . None ) ;
if ( typeNameParts . Length < 2 )
throw new NotImplementedException ( ) ;
output . Write ( '[' ) ;
output . Write ( typeNameParts [ 1 ] ) ;
output . Write ( ']' ) ;
output . WriteReference ( typeNameParts [ 0 ] , null ) ; // TODO : hyperlink!
output . WriteLine ( " = {" ) ;
output . Indent ( ) ;
var blob = metadata . GetBlobReader ( secdecl . PermissionSet ) ;
if ( ( char ) blob . ReadByte ( ) ! = '.' ) {
blob . Reset ( ) ;
WriteXmlSecurityDeclaration ( blob . ReadUTF8 ( blob . RemainingBytes ) ) ;
} else {
string currentAssemblyName = null ;
string currentFullAssemblyName = null ;
if ( metadata . IsAssembly ) {
currentAssemblyName = metadata . GetString ( metadata . GetAssemblyDefinition ( ) . Name ) ;
currentFullAssemblyName = metadata . GetFullAssemblyName ( ) ;
}
output . Write ( " = {" ) ;
blob . ReadCompressedInteger ( ) ; // ?
// The specification seems to be incorrect here, so I'm using the logic from Cecil instead.
int argCount = blob . ReadCompressedInteger ( ) ;
if ( argCount > 0 ) {
output . WriteLine ( ) ;
output . Indent ( ) ;
for ( int j = 0 ; j < argCount ; j + + ) {
WriteSecurityDeclarationArgument ( module , ref blob ) ;
int count = blob . ReadCompressedInteger ( ) ;
for ( int i = 0 ; i < count ; i + + ) {
var typeName = blob . ReadSerializedString ( ) ;
string [ ] nameParts = typeName . Split ( new [ ] { ", " } , StringSplitOptions . None ) ;
if ( nameParts . Length < 2 | | nameParts [ 1 ] = = currentAssemblyName ) {
output . Write ( "class " ) ;
output . Write ( DisassemblerHelpers . Escape ( typeName + ", " + currentFullAssemblyName ) ) ;
} else {
output . Write ( '[' ) ;
output . Write ( nameParts [ 1 ] ) ;
output . Write ( ']' ) ;
output . Write ( nameParts [ 0 ] ) ;
}
output . Write ( " = {" ) ;
blob . ReadCompressedInteger ( ) ; // ?
// The specification seems to be incorrect here, so I'm using the logic from Cecil instead.
int argCount = blob . ReadCompressedInteger ( ) ;
if ( argCount > 0 ) {
output . WriteLine ( ) ;
output . Indent ( ) ;
for ( int j = 0 ; j < argCount ; j + + ) {
WriteSecurityDeclarationArgument ( module , ref blob ) ;
output . WriteLine ( ) ;
}
output . Unindent ( ) ;
}
output . Write ( '}' ) ;
output . Unindent ( ) ;
if ( i + 1 < count )
output . Write ( ',' ) ;
output . WriteLine ( ) ;
}
output . Write ( '}' ) ;
if ( i + 1 < count )
output . Write ( ',' ) ;
output . WriteLine ( ) ;
}
output . Unindent ( ) ;
output . WriteLine ( "}" ) ;
}
output . Unindent ( ) ;
output . WriteLine ( "}" ) ;
}
}
@ -481,7 +494,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -481,7 +494,7 @@ namespace ICSharpCode.Decompiler.Disassembler
}
}
PrimitiveSerializationTypeCode ResolveEnumUnderlyingType ( string typeName , PEFile module , out Metadata . TypeDefinition typeDefinition )
PrimitiveSerializationTypeCode ResolveEnumUnderlyingType ( string typeName , PEFile module , out ( PEFile Module , EntityHandle Handle ) typeDefinition )
{
typeDefinition = default ;
@ -522,21 +535,21 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -522,21 +535,21 @@ namespace ICSharpCode.Decompiler.Disassembler
PEFile containingModule = null ;
// if we deal with an assembly-qualified name, resolve the assembly
if ( nameParts . Length = = 2 )
containingModule = module . AssemblyResolver . Resolve ( AssemblyNameReference . Parse ( nameParts [ 1 ] ) ) ;
containingModule = AssemblyResolver . Resolve ( AssemblyNameReference . Parse ( nameParts [ 1 ] ) ) ;
if ( containingModule ! = null ) {
// try to find the type in the assembly
var handle = FindType ( containingModule , typeNameParts ) ;
var metadata = containingModule . Metadata ;
if ( handle . IsNil | | ! handle . IsEnum ( metadata , out var typeCode ) )
throw new NotSupportedException ( ) ;
typeDefinition = new Metadata . TypeDefinition ( containingModule , handle ) ;
typeDefinition = ( containingModule , handle ) ;
return ( PrimitiveSerializationTypeCode ) typeCode ;
} else {
// just fully-qualified name, try current assembly
var handle = FindType ( module , typeNameParts ) ;
if ( handle . IsNil ) {
// otherwise try mscorlib
var mscorlib = module . AssemblyResolver . Resolve ( AssemblyNameReference . Parse ( "mscorlib" ) ) ;
var mscorlib = AssemblyResolver . Resolve ( AssemblyNameReference . Parse ( "mscorlib" ) ) ;
handle = FindType ( mscorlib , typeNameParts ) ;
if ( handle . IsNil )
throw new NotImplementedException ( ) ;
@ -545,7 +558,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -545,7 +558,7 @@ namespace ICSharpCode.Decompiler.Disassembler
var metadata = module . Metadata ;
if ( handle . IsNil | | ! handle . IsEnum ( metadata , out var typeCode ) )
throw new NotSupportedException ( ) ;
typeDefinition = new Metadata . TypeDefinition ( module , handle ) ;
typeDefinition = ( module , handle ) ;
return ( PrimitiveSerializationTypeCode ) typeCode ;
}
}
@ -625,14 +638,14 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -625,14 +638,14 @@ namespace ICSharpCode.Decompiler.Disassembler
}
var typeInfo = ReadArgumentType ( ref blob ) ;
var typeCode = typeInfo . TypeCode ;
var typeDefinition = default ( Metadata . TypeDefinition ) ;
var typeDefinition = default ( ( PEFile Module , EntityHandle Handle ) ) ;
if ( typeInfo . Kind = = TypeKind . Enum ) {
typeCode = ResolveEnumUnderlyingType ( typeInfo . TypeName , module , out typeDefinition ) ;
}
var name = blob . ReadSerializedString ( ) ;
object value = ReadArgumentValue ( ref blob , typeCode , typeInfo . Kind , typeInfo . IsArray , typeInfo . TypeName ) ;
WriteTypeInfo ( typeInfo . TypeCode , typeInfo . Kind , typeInfo . IsArray , typeInfo . TypeName , typeDefinition , modu le) ;
WriteTypeInfo ( module , typeInfo . TypeCode , typeInfo . Kind , typeInfo . IsArray , typeInfo . TypeName , typeDefinition . Module , typeDefinition . Hand le) ;
output . Write ( ' ' ) ;
output . Write ( DisassemblerHelpers . Escape ( name ) ) ;
@ -645,7 +658,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -645,7 +658,7 @@ namespace ICSharpCode.Decompiler.Disassembler
if ( typeInfo . Kind = = TypeKind . Enum | | typeInfo . Kind = = TypeKind . Primitive ) {
WritePrimitiveTypeCode ( typeCode ) ;
} else {
WriteTypeInfo ( typeCode , typeInfo . Kind , typeInfo . IsArray , typeInfo . TypeName , typeDefinition , modu le) ;
WriteTypeInfo ( module , typeInfo . T ypeCode , typeInfo . Kind , typeInfo . IsArray , typeInfo . TypeName , typeDefinition . Module , typeDefinition . Hand le) ;
}
output . Write ( '(' ) ;
DisassemblerHelpers . WriteOperand ( output , value ) ;
@ -653,7 +666,8 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -653,7 +666,8 @@ namespace ICSharpCode.Decompiler.Disassembler
}
}
private void WriteTypeInfo ( PrimitiveSerializationTypeCode typeCode , TypeKind kind , bool isArray , string typeName , Metadata . TypeDefinition type , PEFile currentModule )
private void WriteTypeInfo ( PEFile currentModule , PrimitiveSerializationTypeCode typeCode , TypeKind kind ,
bool isArray , string typeName , PEFile referencedModule , EntityHandle type )
{
switch ( kind ) {
case TypeKind . Primitive :
@ -669,12 +683,11 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -669,12 +683,11 @@ namespace ICSharpCode.Decompiler.Disassembler
output . Write ( DisassemblerHelpers . Escape ( typeName ) ) ;
break ;
}
var referencedModule = type . Module ;
if ( referencedModule ! = currentModule ) {
output . Write ( '[' ) ;
output . Write ( referencedModule . Name ) ;
output . Write ( ']' ) ;
output . WriteReference ( type . Handle . GetFullTypeName ( type . Module . Metadata ) . ToString ( ) , type ) ;
output . WriteReference ( referencedModule , type , type . GetFullTypeName ( referenced Module. Metadata ) . ToString ( ) ) ;
} else {
output . Write ( DisassemblerHelpers . Escape ( typeName ) ) ;
}
@ -932,7 +945,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -932,7 +945,7 @@ namespace ICSharpCode.Decompiler.Disassembler
if ( ! md . IsNil ) {
WriteMarshalInfo ( metadata . GetBlobReader ( md ) ) ;
}
output . WriteDefinition ( DisassemblerHelpers . Escape ( metadata . GetString ( p . Name ) ) , p ) ;
output . WriteLocalReference ( DisassemblerHelpers . Escape ( metadata . GetString ( p . Name ) ) , p , isDefinition : true ) ;
} else {
signature . ParameterTypes [ i ] ( ILNameSyntax . Signature ) ;
output . Write ( " ''" ) ;
@ -1019,11 +1032,11 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1019,11 +1032,11 @@ namespace ICSharpCode.Decompiler.Disassembler
{ FieldAttributes . NotSerialized , "notserialized" } ,
} ;
public void DisassembleField ( Metadata . FieldDefinition field )
public void DisassembleField ( PEFile module , FieldDefinitionHandle field )
{
var metadata = field . M odule. Metadata ;
var fieldDefinition = metadata . GetFieldDefinition ( field . Handle ) ;
output . WriteDefinition ( ".field " , field ) ;
var metadata = m odule. Metadata ;
var fieldDefinition = metadata . GetFieldDefinition ( field ) ;
output . WriteReference ( module , field , ".field " , isDefinition : true ) ;
int offset = fieldDefinition . GetOffset ( ) ;
if ( offset > - 1 ) {
output . Write ( "[" + offset + "] " ) ;
@ -1032,7 +1045,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1032,7 +1045,7 @@ namespace ICSharpCode.Decompiler.Disassembler
const FieldAttributes hasXAttributes = FieldAttributes . HasDefault | FieldAttributes . HasFieldMarshal | FieldAttributes . HasFieldRVA ;
WriteFlags ( fieldDefinition . Attributes & ~ ( FieldAttributes . FieldAccessMask | hasXAttributes ) , fieldAttributes ) ;
var signature = fieldDefinition . DecodeSignature ( new DisassemblerSignatureProvider ( field . M odule, output ) , new GenericContext ( fieldDefinition . GetDeclaringType ( ) , field . M odule) ) ;
var signature = fieldDefinition . DecodeSignature ( new DisassemblerSignatureProvider ( m odule, output ) , new GenericContext ( fieldDefinition . GetDeclaringType ( ) , m odule) ) ;
var marshallingDescriptor = fieldDefinition . GetMarshallingDescriptor ( ) ;
if ( ! marshallingDescriptor . IsNil ) {
@ -1056,7 +1069,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1056,7 +1069,7 @@ namespace ICSharpCode.Decompiler.Disassembler
var attributes = fieldDefinition . GetCustomAttributes ( ) ;
if ( attributes . Count > 0 ) {
output . MarkFoldStart ( ) ;
WriteAttributes ( field . M odule, fieldDefinition . GetCustomAttributes ( ) ) ;
WriteAttributes ( m odule, fieldDefinition . GetCustomAttributes ( ) ) ;
output . MarkFoldEnd ( ) ;
}
}
@ -1069,15 +1082,16 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1069,15 +1082,16 @@ namespace ICSharpCode.Decompiler.Disassembler
{ PropertyAttributes . HasDefault , "hasdefault" } ,
} ;
public void DisassembleProperty ( Metadata . PropertyDefinition property )
public void DisassembleProperty ( PEFile module , PropertyDefinitionHandle property )
{
var metadata = property . Module . Metadata ;
var propertyDefinition = metadata . GetPropertyDefinition ( property . Handle ) ;
output . WriteDefinition ( ".property " , property ) ;
var metadata = module . Metadata ;
var propertyDefinition = metadata . GetPropertyDefinition ( property ) ;
output . WriteReference ( module , property , ".property" , true ) ;
output . Write ( " " ) ;
WriteFlags ( propertyDefinition . Attributes , propertyAttributes ) ;
var accessors = propertyDefinition . GetAccessors ( ) ;
var declaringType = metadata . GetMethodDefinition ( accessors . GetAny ( ) ) . GetDeclaringType ( ) ;
var signature = propertyDefinition . DecodeSignature ( new DisassemblerSignatureProvider ( property . M odule, output ) , new GenericContext ( declaringType , property . M odule) ) ;
var signature = propertyDefinition . DecodeSignature ( new DisassemblerSignatureProvider ( m odule, output ) , new GenericContext ( declaringType , m odule) ) ;
if ( signature . Header . IsInstance )
output . Write ( "instance " ) ;
@ -1098,23 +1112,23 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1098,23 +1112,23 @@ namespace ICSharpCode.Decompiler.Disassembler
output . Write ( ')' ) ;
OpenBlock ( false ) ;
WriteAttributes ( property . M odule, propertyDefinition . GetCustomAttributes ( ) ) ;
WriteNestedMethod ( ".get" , new Metadata . MethodDefinition ( property . M odule, accessors . Getter ) ) ;
WriteNestedMethod ( ".set" , new Metadata . MethodDefinition ( property . M odule, accessors . Setter ) ) ;
WriteAttributes ( m odule, propertyDefinition . GetCustomAttributes ( ) ) ;
WriteNestedMethod ( ".get" , m odule, accessors . Getter ) ;
WriteNestedMethod ( ".set" , m odule, accessors . Setter ) ;
/ * foreach ( var method in property . OtherMethods ) {
WriteNestedMethod ( ".other" , method ) ;
} * /
CloseBlock ( ) ;
}
void WriteNestedMethod ( string keyword , Metadata . MethodDefinition method )
void WriteNestedMethod ( string keyword , PEFile module , MethodDefinitionHandle method )
{
if ( method . IsNil )
return ;
output . Write ( keyword ) ;
output . Write ( ' ' ) ;
( ( EntityHandle ) method . Handle ) . WriteTo ( method . M odule , output , GenericContext . Empty ) ;
( ( EntityHandle ) method ) . WriteTo ( module , output , GenericContext . Empty ) ;
output . WriteLine ( ) ;
}
#endregion
@ -1127,28 +1141,28 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1127,28 +1141,28 @@ namespace ICSharpCode.Decompiler.Disassembler
public void DisassembleEvent ( PEFile module , EventDefinitionHandle handle )
{
var metadata = module . Metadata ;
var eventDefinition = metadata . GetEventDefinition ( handle ) ;
var eventDefinition = module . Metadata . GetEventDefinition ( handle ) ;
var accessors = eventDefinition . GetAccessors ( ) ;
TypeDefinitionHandle declaringType ;
if ( ! accessors . Adder . IsNil ) {
declaringType = metadata . GetMethodDefinition ( accessors . Adder ) . GetDeclaringType ( ) ;
declaringType = module . M etadata . GetMethodDefinition ( accessors . Adder ) . GetDeclaringType ( ) ;
} else if ( ! accessors . Remover . IsNil ) {
declaringType = metadata . GetMethodDefinition ( accessors . Remover ) . GetDeclaringType ( ) ;
declaringType = module . M etadata . GetMethodDefinition ( accessors . Remover ) . GetDeclaringType ( ) ;
} else {
declaringType = metadata . GetMethodDefinition ( accessors . Raiser ) . GetDeclaringType ( ) ;
declaringType = module . M etadata . GetMethodDefinition ( accessors . Raiser ) . GetDeclaringType ( ) ;
}
output . WriteDefinition ( ".event " , new Metadata . EventDefinition ( module , handle ) ) ;
output . WriteReference ( module , handle , ".event" , true ) ;
output . Write ( " " ) ;
WriteFlags ( eventDefinition . Attributes , eventAttributes ) ;
var signature = eventDefinition . DecodeSignature ( metadata , new DisassemblerSignatureProvider ( module , output ) , new GenericContext ( declaringType , module ) ) ;
var signature = eventDefinition . DecodeSignature ( module . M etadata , new DisassemblerSignatureProvider ( module , output ) , new GenericContext ( declaringType , module ) ) ;
signature ( ILNameSyntax . TypeName ) ;
output . Write ( ' ' ) ;
output . Write ( DisassemblerHelpers . Escape ( metadata . GetString ( eventDefinition . Name ) ) ) ;
output . Write ( DisassemblerHelpers . Escape ( module . M etadata . GetString ( eventDefinition . Name ) ) ) ;
OpenBlock ( false ) ;
WriteAttributes ( module , eventDefinition . GetCustomAttributes ( ) ) ;
WriteNestedMethod ( ".addon" , new Metadata . MethodDefinition ( module , accessors . Adder ) ) ;
WriteNestedMethod ( ".removeon" , new Metadata . MethodDefinition ( module , accessors . Remover ) ) ;
WriteNestedMethod ( ".fire" , new Metadata . MethodDefinition ( module , accessors . Raiser ) ) ;
WriteNestedMethod ( ".addon" , module , accessors . Adder ) ;
WriteNestedMethod ( ".removeon" , module , accessors . Remover ) ;
WriteNestedMethod ( ".fire" , module , accessors . Raiser ) ;
/ * foreach ( var method in ev . OtherMethods ) {
WriteNestedMethod ( ".other" , method ) ;
} * /
@ -1191,12 +1205,11 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1191,12 +1205,11 @@ namespace ICSharpCode.Decompiler.Disassembler
{ TypeAttributes . HasSecurity , null } ,
} ;
public void DisassembleType ( Metadata . TypeDefinition type )
public void DisassembleType ( PEFile module , TypeDefinitionHandle type )
{
var metadata = type . Module . Metadata ;
var typeDefinition = metadata . GetTypeDefinition ( type . Handle ) ;
output . WriteDefinition ( ".class " , type ) ;
var typeDefinition = module . Metadata . GetTypeDefinition ( type ) ;
output . WriteReference ( module , type , ".class" , true ) ;
output . Write ( " " ) ;
if ( ( typeDefinition . Attributes & TypeAttributes . ClassSemanticsMask ) = = TypeAttributes . Interface )
output . Write ( "interface " ) ;
WriteEnum ( typeDefinition . Attributes & TypeAttributes . VisibilityMask , typeVisibility ) ;
@ -1205,15 +1218,16 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1205,15 +1218,16 @@ namespace ICSharpCode.Decompiler.Disassembler
const TypeAttributes masks = TypeAttributes . ClassSemanticsMask | TypeAttributes . VisibilityMask | TypeAttributes . LayoutMask | TypeAttributes . StringFormatMask ;
WriteFlags ( typeDefinition . Attributes & ~ masks , typeAttributes ) ;
output . Write ( typeDefinition . GetDeclaringType ( ) . IsNil ? typeDefinition . GetFullTypeName ( metadata ) . ToILNameString ( ) : DisassemblerHelpers . Escape ( metadata . GetString ( typeDefinition . Name ) ) ) ;
WriteTypeParameters ( output , type . Module , new GenericContext ( type ) , typeDefinition . GetGenericParameters ( ) ) ;
output . Write ( typeDefinition . GetDeclaringType ( ) . IsNil ? typeDefinition . GetFullTypeName ( module . Metadata ) . ToILNameString ( ) : DisassemblerHelpers . Escape ( module . Metadata . GetString ( typeDefinition . Name ) ) ) ;
GenericContext genericContext = new GenericContext ( type , module ) ;
WriteTypeParameters ( output , module , genericContext , typeDefinition . GetGenericParameters ( ) ) ;
output . MarkFoldStart ( defaultCollapsed : ! ExpandMemberDefinitions & & isInType ) ;
output . WriteLine ( ) ;
if ( ! typeDefinition . BaseType . IsNil ) {
output . Indent ( ) ;
output . Write ( "extends " ) ;
typeDefinition . BaseType . WriteTo ( type . M odule, output , new GenericContext ( type ) , ILNameSyntax . TypeName ) ;
typeDefinition . BaseType . WriteTo ( m odule, output , genericContext , ILNameSyntax . TypeName ) ;
output . WriteLine ( ) ;
output . Unindent ( ) ;
}
@ -1229,9 +1243,9 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1229,9 +1243,9 @@ namespace ICSharpCode.Decompiler.Disassembler
else
output . Write ( " " ) ;
first = false ;
var iface = metadata . GetInterfaceImplementation ( i ) ;
WriteAttributes ( type . M odule, iface . GetCustomAttributes ( ) ) ;
iface . Interface . WriteTo ( type . M odule, output , new GenericContext ( type ) , ILNameSyntax . TypeName ) ;
var iface = module . M etadata . GetInterfaceImplementation ( i ) ;
WriteAttributes ( m odule, iface . GetCustomAttributes ( ) ) ;
iface . Interface . WriteTo ( m odule, output , genericContext , ILNameSyntax . TypeName ) ;
}
output . WriteLine ( ) ;
output . Unindent ( ) ;
@ -1241,8 +1255,8 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1241,8 +1255,8 @@ namespace ICSharpCode.Decompiler.Disassembler
output . Indent ( ) ;
bool oldIsInType = isInType ;
isInType = true ;
WriteAttributes ( type . M odule, typeDefinition . GetCustomAttributes ( ) ) ;
WriteSecurityDeclarations ( type . M odule, typeDefinition . GetDeclarativeSecurityAttributes ( ) ) ;
WriteAttributes ( m odule, typeDefinition . GetCustomAttributes ( ) ) ;
WriteSecurityDeclarations ( m odule, typeDefinition . GetDeclarativeSecurityAttributes ( ) ) ;
var layout = typeDefinition . GetLayout ( ) ;
if ( ! layout . IsDefault ) {
output . WriteLine ( ".pack {0}" , layout . PackingSize ) ;
@ -1254,7 +1268,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1254,7 +1268,7 @@ namespace ICSharpCode.Decompiler.Disassembler
output . WriteLine ( "// Nested Types" ) ;
foreach ( var nestedType in nestedTypes ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleType ( new Metadata . TypeDefinition ( type . M odule, nestedType ) ) ;
DisassembleType ( m odule, nestedType ) ;
output . WriteLine ( ) ;
}
output . WriteLine ( ) ;
@ -1264,7 +1278,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1264,7 +1278,7 @@ namespace ICSharpCode.Decompiler.Disassembler
output . WriteLine ( "// Fields" ) ;
foreach ( var field in fields ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleField ( new Metadata . FieldDefinition ( type . M odule, field ) ) ;
DisassembleField ( m odule, field ) ;
}
output . WriteLine ( ) ;
}
@ -1273,7 +1287,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1273,7 +1287,7 @@ namespace ICSharpCode.Decompiler.Disassembler
output . WriteLine ( "// Methods" ) ;
foreach ( var m in methods ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleMethod ( type . M odule, m ) ;
DisassembleMethod ( m odule, m ) ;
output . WriteLine ( ) ;
}
}
@ -1282,7 +1296,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1282,7 +1296,7 @@ namespace ICSharpCode.Decompiler.Disassembler
output . WriteLine ( "// Events" ) ;
foreach ( var ev in events ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleEvent ( type . M odule, ev ) ;
DisassembleEvent ( m odule, ev ) ;
output . WriteLine ( ) ;
}
output . WriteLine ( ) ;
@ -1292,11 +1306,11 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1292,11 +1306,11 @@ namespace ICSharpCode.Decompiler.Disassembler
output . WriteLine ( "// Properties" ) ;
foreach ( var prop in properties ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleProperty ( new Metadata . PropertyDefinition ( type . M odule, prop ) ) ;
DisassembleProperty ( m odule, prop ) ;
}
output . WriteLine ( ) ;
}
CloseBlock ( "end of class " + ( ! typeDefinition . GetDeclaringType ( ) . IsNil ? metadata . GetString ( typeDefinition . Name ) : typeDefinition . GetFullTypeName ( metadata ) . ToString ( ) ) ) ;
CloseBlock ( "end of class " + ( ! typeDefinition . GetDeclaringType ( ) . IsNil ? module . M etadata . GetString ( typeDefinition . Name ) : typeDefinition . GetFullTypeName ( module . M etadata ) . ToString ( ) ) ) ;
isInType = oldIsInType ;
}
@ -1348,27 +1362,27 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1348,27 +1362,27 @@ namespace ICSharpCode.Decompiler.Disassembler
output . Write ( ".custom " ) ;
var attr = metadata . GetCustomAttribute ( a ) ;
attr . Constructor . WriteTo ( module , output , GenericContext . Empty ) ;
byte [ ] blob = metadata . GetBlobBytes ( attr . Value ) ;
if ( blob . Length > 0 ) {
if ( ! attr . Value . IsNil ) {
output . Write ( " = " ) ;
WriteBlob ( blob ) ;
WriteBlob ( attr . Value , metadata ) ;
}
output . WriteLine ( ) ;
}
}
void WriteBlob ( byte [ ] blob )
void WriteBlob ( BlobHandle blob , MetadataReader metadata )
{
var reader = metadata . GetBlobReader ( blob ) ;
output . Write ( "(" ) ;
output . Indent ( ) ;
for ( int i = 0 ; i < blob . Length ; i + + ) {
if ( i % 1 6 = = 0 & & i < blob . Length - 1 ) {
for ( int i = 0 ; i < reader . Length ; i + + ) {
if ( i % 1 6 = = 0 & & i < reader . Length - 1 ) {
output . WriteLine ( ) ;
} else {
output . Write ( ' ' ) ;
}
output . Write ( blob [ i ] . ToString ( "x2" ) ) ;
output . Write ( reader . ReadByte ( ) . ToString ( "x2" ) ) ;
}
output . WriteLine ( ) ;
@ -1449,7 +1463,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1449,7 +1463,7 @@ namespace ICSharpCode.Decompiler.Disassembler
}
#endregion
public void DisassembleNamespace ( string nameSpace , IEnumerable < Metadata . TypeDefinition > types )
public void DisassembleNamespace ( string nameSpace , PEFile module , IEnumerable < TypeDefinitionHandle > types )
{
if ( ! string . IsNullOrEmpty ( nameSpace ) ) {
output . Write ( ".namespace " + DisassemblerHelpers . Escape ( nameSpace ) ) ;
@ -1459,7 +1473,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1459,7 +1473,7 @@ namespace ICSharpCode.Decompiler.Disassembler
isInType = true ;
foreach ( var td in types ) {
cancellationToken . ThrowIfCancellationRequested ( ) ;
DisassembleType ( td ) ;
DisassembleType ( module , td ) ;
output . WriteLine ( ) ;
}
if ( ! string . IsNullOrEmpty ( nameSpace ) ) {
@ -1480,10 +1494,9 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1480,10 +1494,9 @@ namespace ICSharpCode.Decompiler.Disassembler
OpenBlock ( false ) ;
WriteAttributes ( module , asm . GetCustomAttributes ( ) ) ;
WriteSecurityDeclarations ( module , asm . GetDeclarativeSecurityAttributes ( ) ) ;
var publicKey = metadata . GetBlobBytes ( asm . PublicKey ) ;
if ( publicKey . Length > 0 ) {
if ( ! asm . PublicKey . IsNil ) {
output . Write ( ".publickey = " ) ;
WriteBlob ( p ublicKey) ;
WriteBlob ( asm . P ublicKey, metadata ) ;
output . WriteLine ( ) ;
}
if ( asm . HashAlgorithm ! = AssemblyHashAlgorithm . None ) {
@ -1514,7 +1527,7 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1514,7 +1527,7 @@ namespace ICSharpCode.Decompiler.Disassembler
OpenBlock ( false ) ;
if ( ! aref . PublicKeyOrToken . IsNil ) {
output . Write ( ".publickeytoken = " ) ;
WriteBlob ( metadata . GetBlobBytes ( aref . PublicKeyOrToken ) ) ;
WriteBlob ( aref . PublicKeyOrToken , metadata ) ;
output . WriteLine ( ) ;
}
if ( aref . Version ! = null ) {
@ -1590,8 +1603,8 @@ namespace ICSharpCode.Decompiler.Disassembler
@@ -1590,8 +1603,8 @@ namespace ICSharpCode.Decompiler.Disassembler
public void WriteModuleContents ( PEFile module )
{
foreach ( var handle in module . TopLevelTypeDefinitions ) {
DisassembleType ( handle ) ;
foreach ( var handle in module . Metadata . Get TopLevelTypeDefinitions( ) ) {
DisassembleType ( module , handle ) ;
output . WriteLine ( ) ;
}
}