mirror of https://github.com/icsharpcode/ILSpy.git
28 changed files with 2937 additions and 1085 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,92 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.Disassembler |
||||||
|
{ |
||||||
|
public struct OpCodeInfo : IEquatable<OpCodeInfo> |
||||||
|
{ |
||||||
|
public readonly int Code; |
||||||
|
public readonly string Name; |
||||||
|
|
||||||
|
string encodedName; |
||||||
|
|
||||||
|
public OpCodeInfo(int code, string name) |
||||||
|
{ |
||||||
|
this.Code = code; |
||||||
|
this.Name = name; |
||||||
|
this.encodedName = null; |
||||||
|
} |
||||||
|
|
||||||
|
public bool Equals(OpCodeInfo other) |
||||||
|
{ |
||||||
|
return other.Code == this.Code && other.Name == this.Name; |
||||||
|
} |
||||||
|
|
||||||
|
public static bool operator ==(OpCodeInfo lhs, OpCodeInfo rhs) => lhs.Equals(rhs); |
||||||
|
public static bool operator !=(OpCodeInfo lhs, OpCodeInfo rhs) => !(lhs == rhs); |
||||||
|
|
||||||
|
public string Link => "http://msdn.microsoft.com/library/system.reflection.emit.opcodes." + EncodedName.ToLowerInvariant() + ".aspx"; |
||||||
|
public string EncodedName { |
||||||
|
get { |
||||||
|
if (encodedName != null) |
||||||
|
return encodedName; |
||||||
|
switch (Name) { |
||||||
|
case "constrained.": |
||||||
|
encodedName = "Constrained"; |
||||||
|
return encodedName; |
||||||
|
case "no.": |
||||||
|
encodedName = "No"; |
||||||
|
return encodedName; |
||||||
|
case "readonly.": |
||||||
|
encodedName = "Reaonly"; |
||||||
|
return encodedName; |
||||||
|
case "tail.": |
||||||
|
encodedName = "Tailcall"; |
||||||
|
return encodedName; |
||||||
|
case "unaligned.": |
||||||
|
encodedName = "Unaligned"; |
||||||
|
return encodedName; |
||||||
|
case "volatile.": |
||||||
|
encodedName = "Volatile"; |
||||||
|
return encodedName; |
||||||
|
} |
||||||
|
string text = ""; |
||||||
|
bool toUpperCase = true; |
||||||
|
foreach (var ch in Name) { |
||||||
|
if (ch == '.') { |
||||||
|
text += '_'; |
||||||
|
toUpperCase = true; |
||||||
|
} else if (toUpperCase) { |
||||||
|
text += char.ToUpperInvariant(ch); |
||||||
|
toUpperCase = false; |
||||||
|
} else { |
||||||
|
text += ch; |
||||||
|
} |
||||||
|
} |
||||||
|
encodedName = text; |
||||||
|
return encodedName; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
File diff suppressed because it is too large
Load Diff
@ -1,87 +0,0 @@ |
|||||||
//
|
|
||||||
// AssemblyLoader.cs
|
|
||||||
//
|
|
||||||
// Author:
|
|
||||||
// Mike Krüger <mkrueger@xamarin.com>
|
|
||||||
//
|
|
||||||
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Reflection; |
|
||||||
using System.Threading; |
|
||||||
using ICSharpCode.Decompiler.TypeSystem.Implementation; |
|
||||||
|
|
||||||
namespace ICSharpCode.Decompiler.TypeSystem |
|
||||||
{ |
|
||||||
public enum AssemblyLoaderBackend { |
|
||||||
Auto, |
|
||||||
Cecil, |
|
||||||
IKVM |
|
||||||
} |
|
||||||
|
|
||||||
public abstract class AssemblyLoader |
|
||||||
{ |
|
||||||
public static AssemblyLoader Create () |
|
||||||
{ |
|
||||||
return Create (AssemblyLoaderBackend.Auto); |
|
||||||
} |
|
||||||
|
|
||||||
public static AssemblyLoader Create (AssemblyLoaderBackend backend) |
|
||||||
{ |
|
||||||
switch (backend) { |
|
||||||
case AssemblyLoaderBackend.Auto: |
|
||||||
case AssemblyLoaderBackend.Cecil: |
|
||||||
return (AssemblyLoader)Assembly.Load ("ICSharpCode.Decompiler.Cecil").CreateInstance ("ICSharpCode.Decompiler.TypeSystem.CecilLoader"); |
|
||||||
case AssemblyLoaderBackend.IKVM: |
|
||||||
return (AssemblyLoader)Assembly.Load ("ICSharpCode.Decompiler.IKVM").CreateInstance ("ICSharpCode.Decompiler.TypeSystem.IkvmLoader"); |
|
||||||
default: |
|
||||||
throw new ArgumentOutOfRangeException (); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Specifies whether to include internal members. The default is false.
|
|
||||||
/// </summary>
|
|
||||||
public bool IncludeInternalMembers { get; set; } |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets/Sets the cancellation token used by the assembly loader.
|
|
||||||
/// </summary>
|
|
||||||
public CancellationToken CancellationToken { get; set; } |
|
||||||
|
|
||||||
protected InterningProvider interningProvider = new SimpleInterningProvider(); |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets/Sets the interning provider.
|
|
||||||
/// </summary>
|
|
||||||
public InterningProvider InterningProvider { |
|
||||||
get { return interningProvider; } |
|
||||||
set { |
|
||||||
if (value == null) |
|
||||||
throw new ArgumentNullException(); |
|
||||||
interningProvider = value; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public abstract IUnresolvedAssembly LoadAssemblyFile(string fileName); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@ -0,0 +1,214 @@ |
|||||||
|
// Copyright (c) 2017 Siegfried Pammer
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Collections.Immutable; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using System.Linq; |
||||||
|
using System.Reflection.Metadata; |
||||||
|
using System.Reflection.Metadata.Ecma335; |
||||||
|
using ICSharpCode.Decompiler.Util; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.TypeSystem.Implementation |
||||||
|
{ |
||||||
|
class SignatureTypeReference : ITypeReference |
||||||
|
{ |
||||||
|
readonly TypeSpecification typeSpecification; |
||||||
|
readonly MetadataReader reader; |
||||||
|
|
||||||
|
public SignatureTypeReference(TypeSpecificationHandle handle, MetadataReader reader) |
||||||
|
{ |
||||||
|
this.typeSpecification = reader.GetTypeSpecification(handle); |
||||||
|
this.reader = reader; |
||||||
|
} |
||||||
|
|
||||||
|
public IType Resolve(ITypeResolveContext context) |
||||||
|
{ |
||||||
|
return typeSpecification.DecodeSignature(new TypeReferenceSignatureDecoder(), default(Unit)).Resolve(context); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public sealed class PinnedType : TypeWithElementType |
||||||
|
{ |
||||||
|
public PinnedType(IType elementType) |
||||||
|
: base(elementType) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public override string NameSuffix => " pinned"; |
||||||
|
|
||||||
|
public override bool? IsReferenceType => elementType.IsReferenceType; |
||||||
|
|
||||||
|
public override TypeKind Kind => TypeKind.Other; |
||||||
|
|
||||||
|
public override ITypeReference ToTypeReference() |
||||||
|
{ |
||||||
|
return new PinnedTypeReference(elementType.ToTypeReference()); |
||||||
|
} |
||||||
|
|
||||||
|
public override IType VisitChildren(TypeVisitor visitor) |
||||||
|
{ |
||||||
|
var newType = elementType.AcceptVisitor(visitor); |
||||||
|
if (newType == elementType) |
||||||
|
return this; |
||||||
|
return new PinnedType(newType); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public sealed class PinnedTypeReference : ITypeReference |
||||||
|
{ |
||||||
|
readonly ITypeReference elementType; |
||||||
|
|
||||||
|
public PinnedTypeReference(ITypeReference elementType) |
||||||
|
{ |
||||||
|
this.elementType = elementType; |
||||||
|
} |
||||||
|
|
||||||
|
public IType Resolve(ITypeResolveContext context) |
||||||
|
{ |
||||||
|
return new PinnedType(elementType.Resolve(context)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public struct Unit { } |
||||||
|
|
||||||
|
class TypeReferenceSignatureDecoder : ISignatureTypeProvider<ITypeReference, Unit> |
||||||
|
{ |
||||||
|
public ITypeReference GetArrayType(ITypeReference elementType, ArrayShape shape) |
||||||
|
{ |
||||||
|
return new ArrayTypeReference(elementType, shape.Rank); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetByReferenceType(ITypeReference elementType) |
||||||
|
{ |
||||||
|
return new ByReferenceTypeReference(elementType); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetFunctionPointerType(MethodSignature<ITypeReference> signature) |
||||||
|
{ |
||||||
|
return KnownTypeReference.IntPtr; |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetGenericInstantiation(ITypeReference genericType, ImmutableArray<ITypeReference> typeArguments) |
||||||
|
{ |
||||||
|
return new ParameterizedTypeReference(genericType, typeArguments); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetGenericMethodParameter(Unit genericContext, int index) |
||||||
|
{ |
||||||
|
return new TypeParameterReference(SymbolKind.Method, index); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetGenericTypeParameter(Unit genericContext, int index) |
||||||
|
{ |
||||||
|
return new TypeParameterReference(SymbolKind.TypeDefinition, index); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetModifiedType(ITypeReference modifier, ITypeReference unmodifiedType, bool isRequired) |
||||||
|
{ |
||||||
|
return unmodifiedType; |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetPinnedType(ITypeReference elementType) |
||||||
|
{ |
||||||
|
return new PinnedTypeReference(elementType); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetPointerType(ITypeReference elementType) |
||||||
|
{ |
||||||
|
return new PointerTypeReference(elementType); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetPrimitiveType(PrimitiveTypeCode typeCode) |
||||||
|
{ |
||||||
|
return KnownTypeReference.Get(typeCode.ToKnownTypeCode()); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetSZArrayType(ITypeReference elementType) |
||||||
|
{ |
||||||
|
return new ArrayTypeReference(elementType); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) |
||||||
|
{ |
||||||
|
return new DefaultUnresolvedTypeDefinition(handle.GetFullTypeName(reader).ReflectionName); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) |
||||||
|
{ |
||||||
|
var asmref = handle.GetDeclaringAssembly(reader); |
||||||
|
if (asmref.IsNil) |
||||||
|
return new GetClassTypeReference(handle.GetFullTypeName(reader), DefaultAssemblyReference.CurrentAssembly); |
||||||
|
var asm = reader.GetAssemblyReference(asmref); |
||||||
|
return new GetClassTypeReference(handle.GetFullTypeName(reader), new DefaultAssemblyReference(reader.GetString(asm.Name))); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromSpecification(MetadataReader reader, Unit genericContext, TypeSpecificationHandle handle, byte rawTypeKind) |
||||||
|
{ |
||||||
|
return new SignatureTypeReference(handle, reader); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class TypeSystemAttributeTypeProvider : ICustomAttributeTypeProvider<ITypeReference> |
||||||
|
{ |
||||||
|
public ITypeReference GetPrimitiveType(PrimitiveTypeCode typeCode) |
||||||
|
{ |
||||||
|
return KnownTypeReference.Get(typeCode.ToKnownTypeCode()); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetSystemType() |
||||||
|
{ |
||||||
|
return KnownTypeReference.Get(KnownTypeCode.Type); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetSZArrayType(ITypeReference elementType) |
||||||
|
{ |
||||||
|
return new ArrayTypeReference(elementType); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) |
||||||
|
{ |
||||||
|
var type = reader.GetTypeDefinition(handle); |
||||||
|
return new DefaultUnresolvedTypeDefinition(type.GetFullTypeName(reader).ToString()); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) |
||||||
|
{ |
||||||
|
return new DefaultUnresolvedTypeDefinition(handle.GetFullTypeName(reader).ToString()); |
||||||
|
} |
||||||
|
|
||||||
|
public ITypeReference GetTypeFromSerializedName(string name) |
||||||
|
{ |
||||||
|
return new GetClassTypeReference(new FullTypeName(name)); |
||||||
|
} |
||||||
|
|
||||||
|
public PrimitiveTypeCode GetUnderlyingEnumType(ITypeReference type) |
||||||
|
{ |
||||||
|
var def = type.GetEnumUnderlyingType().GetDefinition(); |
||||||
|
if (def == null) |
||||||
|
throw new InvalidOperationException(); |
||||||
|
return def.KnownTypeCode.ToPrimtiveTypeCode(); |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsSystemType(IType type) |
||||||
|
{ |
||||||
|
return type.IsKnownType(KnownTypeCode.Type); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,199 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Reflection.Metadata; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.TypeSystem |
||||||
|
{ |
||||||
|
static class MetadataExtensions |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of the attribute.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Either <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/>,<see cref="TypeSpecificationHandle".</returns>
|
||||||
|
public static EntityHandle GetAttributeType(this CustomAttribute attribute, MetadataReader reader) |
||||||
|
{ |
||||||
|
switch (attribute.Constructor.Kind) { |
||||||
|
case HandleKind.MethodDefinition: |
||||||
|
var md = reader.GetMethodDefinition((MethodDefinitionHandle)attribute.Constructor); |
||||||
|
return md.GetDeclaringType(); |
||||||
|
case HandleKind.MemberReference: |
||||||
|
var mr = reader.GetMemberReference((MemberReferenceHandle)attribute.Constructor); |
||||||
|
return mr.Parent; |
||||||
|
default: |
||||||
|
throw new NotSupportedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static FullTypeName GetFullTypeName(this EntityHandle handle, MetadataReader reader) |
||||||
|
{ |
||||||
|
switch (handle.Kind) { |
||||||
|
case HandleKind.TypeDefinition: |
||||||
|
return ((TypeDefinitionHandle)handle).GetFullTypeName(reader); |
||||||
|
case HandleKind.TypeReference: |
||||||
|
return ((TypeReferenceHandle)handle).GetFullTypeName(reader); |
||||||
|
default: |
||||||
|
throw new NotSupportedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static FullTypeName GetFullTypeName(this TypeReferenceHandle handle, MetadataReader reader) |
||||||
|
{ |
||||||
|
var tr = reader.GetTypeReference(handle); |
||||||
|
TypeReferenceHandle declaringTypeHandle; |
||||||
|
if ((declaringTypeHandle = tr.GetDeclaringType()).IsNil) { |
||||||
|
string @namespace = tr.Namespace.IsNil ? "" : reader.GetString(tr.Namespace); |
||||||
|
return new FullTypeName(new TopLevelTypeName(@namespace, reader.GetString(tr.Name))); |
||||||
|
} else { |
||||||
|
return declaringTypeHandle.GetFullTypeName(reader).NestedType(reader.GetString(tr.Name), 0); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static FullTypeName GetFullTypeName(this TypeDefinitionHandle handle, MetadataReader reader) |
||||||
|
{ |
||||||
|
return reader.GetTypeDefinition(handle).GetFullTypeName(reader); |
||||||
|
} |
||||||
|
|
||||||
|
public static FullTypeName GetFullTypeName(this TypeDefinition td, MetadataReader reader) |
||||||
|
{ |
||||||
|
TypeDefinitionHandle declaringTypeHandle; |
||||||
|
if ((declaringTypeHandle = td.GetDeclaringType()).IsNil) { |
||||||
|
string @namespace = td.Namespace.IsNil ? "" : reader.GetString(td.Namespace); |
||||||
|
return new FullTypeName(new TopLevelTypeName(@namespace, reader.GetString(td.Name), td.GetGenericParameters().Count)); |
||||||
|
} else { |
||||||
|
return declaringTypeHandle.GetFullTypeName(reader).NestedType(reader.GetString(td.Name), td.GetGenericParameters().Count); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static TypeReferenceHandle GetDeclaringType(this TypeReference tr) |
||||||
|
{ |
||||||
|
switch (tr.ResolutionScope.Kind) { |
||||||
|
case HandleKind.TypeReference: |
||||||
|
return (TypeReferenceHandle)tr.ResolutionScope; |
||||||
|
default: |
||||||
|
return default(TypeReferenceHandle); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static AssemblyReferenceHandle GetDeclaringAssembly(this TypeReferenceHandle handle, MetadataReader reader) |
||||||
|
{ |
||||||
|
var tr = reader.GetTypeReference(handle); |
||||||
|
switch (tr.ResolutionScope.Kind) { |
||||||
|
case HandleKind.TypeReference: |
||||||
|
return ((TypeReferenceHandle)tr.ResolutionScope).GetDeclaringAssembly(reader); |
||||||
|
case HandleKind.AssemblyReference: |
||||||
|
return (AssemblyReferenceHandle)tr.ResolutionScope; |
||||||
|
default: |
||||||
|
return default(AssemblyReferenceHandle); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static bool IsValueType(this TypeDefinition typeDefinition, MetadataReader reader) |
||||||
|
{ |
||||||
|
if (typeDefinition.BaseType.IsNil) |
||||||
|
return false; |
||||||
|
var baseType = typeDefinition.BaseType.GetFullTypeName(reader).ToString(); |
||||||
|
if (baseType == "System.Enum") |
||||||
|
return true; |
||||||
|
var thisType = typeDefinition.GetFullTypeName(reader).ToString(); |
||||||
|
return baseType == "System.ValueType" && thisType != "System.Enum"; |
||||||
|
} |
||||||
|
|
||||||
|
public static bool IsEnum(this TypeDefinition typeDefinition, MetadataReader reader) |
||||||
|
{ |
||||||
|
if (typeDefinition.BaseType.IsNil) |
||||||
|
return false; |
||||||
|
return typeDefinition.BaseType.GetFullTypeName(reader).ToString() == "System.Enum"; |
||||||
|
} |
||||||
|
|
||||||
|
public static PrimitiveTypeCode ToPrimtiveTypeCode(this KnownTypeCode typeCode) |
||||||
|
{ |
||||||
|
switch (typeCode) { |
||||||
|
case KnownTypeCode.Object: |
||||||
|
return PrimitiveTypeCode.Object; |
||||||
|
case KnownTypeCode.Boolean: |
||||||
|
return PrimitiveTypeCode.Boolean; |
||||||
|
case KnownTypeCode.Char: |
||||||
|
return PrimitiveTypeCode.Char; |
||||||
|
case KnownTypeCode.SByte: |
||||||
|
return PrimitiveTypeCode.SByte; |
||||||
|
case KnownTypeCode.Byte: |
||||||
|
return PrimitiveTypeCode.Byte; |
||||||
|
case KnownTypeCode.Int16: |
||||||
|
return PrimitiveTypeCode.Int16; |
||||||
|
case KnownTypeCode.UInt16: |
||||||
|
return PrimitiveTypeCode.UInt16; |
||||||
|
case KnownTypeCode.Int32: |
||||||
|
return PrimitiveTypeCode.Int32; |
||||||
|
case KnownTypeCode.UInt32: |
||||||
|
return PrimitiveTypeCode.UInt32; |
||||||
|
case KnownTypeCode.Int64: |
||||||
|
return PrimitiveTypeCode.Int64; |
||||||
|
case KnownTypeCode.UInt64: |
||||||
|
return PrimitiveTypeCode.UInt64; |
||||||
|
case KnownTypeCode.Single: |
||||||
|
return PrimitiveTypeCode.Single; |
||||||
|
case KnownTypeCode.Double: |
||||||
|
return PrimitiveTypeCode.Double; |
||||||
|
case KnownTypeCode.String: |
||||||
|
return PrimitiveTypeCode.String; |
||||||
|
case KnownTypeCode.Void: |
||||||
|
return PrimitiveTypeCode.Void; |
||||||
|
case KnownTypeCode.TypedReference: |
||||||
|
return PrimitiveTypeCode.TypedReference; |
||||||
|
case KnownTypeCode.IntPtr: |
||||||
|
return PrimitiveTypeCode.IntPtr; |
||||||
|
case KnownTypeCode.UIntPtr: |
||||||
|
return PrimitiveTypeCode.UIntPtr; |
||||||
|
default: |
||||||
|
throw new NotSupportedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static KnownTypeCode ToKnownTypeCode(this PrimitiveTypeCode typeCode) |
||||||
|
{ |
||||||
|
switch (typeCode) { |
||||||
|
case PrimitiveTypeCode.Boolean: |
||||||
|
return KnownTypeCode.Boolean; |
||||||
|
case PrimitiveTypeCode.Byte: |
||||||
|
return KnownTypeCode.Byte; |
||||||
|
case PrimitiveTypeCode.SByte: |
||||||
|
return KnownTypeCode.SByte; |
||||||
|
case PrimitiveTypeCode.Char: |
||||||
|
return KnownTypeCode.Byte; |
||||||
|
case PrimitiveTypeCode.Int16: |
||||||
|
return KnownTypeCode.Byte; |
||||||
|
case PrimitiveTypeCode.UInt16: |
||||||
|
return KnownTypeCode.Byte; |
||||||
|
case PrimitiveTypeCode.Int32: |
||||||
|
return KnownTypeCode.Int32; |
||||||
|
case PrimitiveTypeCode.UInt32: |
||||||
|
return KnownTypeCode.UInt32; |
||||||
|
case PrimitiveTypeCode.Int64: |
||||||
|
return KnownTypeCode.Int64; |
||||||
|
case PrimitiveTypeCode.UInt64: |
||||||
|
return KnownTypeCode.UInt64; |
||||||
|
case PrimitiveTypeCode.Single: |
||||||
|
return KnownTypeCode.Single; |
||||||
|
case PrimitiveTypeCode.Double: |
||||||
|
return KnownTypeCode.Double; |
||||||
|
case PrimitiveTypeCode.IntPtr: |
||||||
|
return KnownTypeCode.IntPtr; |
||||||
|
case PrimitiveTypeCode.UIntPtr: |
||||||
|
return KnownTypeCode.UIntPtr; |
||||||
|
case PrimitiveTypeCode.Object: |
||||||
|
return KnownTypeCode.Object; |
||||||
|
case PrimitiveTypeCode.String: |
||||||
|
return KnownTypeCode.String; |
||||||
|
case PrimitiveTypeCode.TypedReference: |
||||||
|
return KnownTypeCode.TypedReference; |
||||||
|
case PrimitiveTypeCode.Void: |
||||||
|
return KnownTypeCode.Void; |
||||||
|
default: |
||||||
|
return KnownTypeCode.None; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,65 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using ICSharpCode.Decompiler.TypeSystem; |
||||||
|
using Mono.Cecil; |
||||||
|
using Mono.Cecil.Cil; |
||||||
|
using ArrayType = Mono.Cecil.ArrayType; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.Cecil |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Cecil helper methods.
|
||||||
|
/// </summary>
|
||||||
|
public static class CecilExtensions |
||||||
|
{ |
||||||
|
public static HashSet<MethodDefinition> GetAccessorMethods(this TypeDefinition type) |
||||||
|
{ |
||||||
|
HashSet<MethodDefinition> accessorMethods = new HashSet<MethodDefinition>(); |
||||||
|
foreach (var property in type.Properties) { |
||||||
|
accessorMethods.Add(property.GetMethod); |
||||||
|
accessorMethods.Add(property.SetMethod); |
||||||
|
if (property.HasOtherMethods) { |
||||||
|
foreach (var m in property.OtherMethods) |
||||||
|
accessorMethods.Add(m); |
||||||
|
} |
||||||
|
} |
||||||
|
foreach (EventDefinition ev in type.Events) { |
||||||
|
accessorMethods.Add(ev.AddMethod); |
||||||
|
accessorMethods.Add(ev.RemoveMethod); |
||||||
|
accessorMethods.Add(ev.InvokeMethod); |
||||||
|
if (ev.HasOtherMethods) { |
||||||
|
foreach (var m in ev.OtherMethods) |
||||||
|
accessorMethods.Add(m); |
||||||
|
} |
||||||
|
} |
||||||
|
return accessorMethods; |
||||||
|
} |
||||||
|
|
||||||
|
public static TypeDefinition ResolveWithinSameModule(this TypeReference type) |
||||||
|
{ |
||||||
|
if (type != null && type.GetElementType().Module == type.Module) |
||||||
|
return type.Resolve(); |
||||||
|
else |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue