private static TypeSignature ReadTypeSignature( MetadataImage image, IBinaryStreamReader reader, RecursionProtection protection) { var elementType = (ElementType)reader.ReadByte(); switch (elementType) { case ElementType.Array: return(ArrayTypeSignature.FromReader(image, reader, protection)); case ElementType.Boxed: return(BoxedTypeSignature.FromReader(image, reader, protection)); case ElementType.ByRef: return(ByReferenceTypeSignature.FromReader(image, reader, protection)); case ElementType.CModOpt: return(OptionalModifierSignature.FromReader(image, reader, protection)); case ElementType.CModReqD: return(RequiredModifierSignature.FromReader(image, reader, protection)); case ElementType.Class: return(TypeDefOrRefSignature.FromReader(image, reader, protection)); case ElementType.FnPtr: return(FunctionPointerTypeSignature.FromReader(image, reader, protection)); case ElementType.GenericInst: return(GenericInstanceTypeSignature.FromReader(image, reader, protection)); case ElementType.MVar: return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Method)); case ElementType.Pinned: return(PinnedTypeSignature.FromReader(image, reader, protection)); case ElementType.Ptr: return(PointerTypeSignature.FromReader(image, reader, protection)); case ElementType.Sentinel: return(SentinelTypeSignature.FromReader(image, reader, protection)); case ElementType.SzArray: return(SzArrayTypeSignature.FromReader(image, reader, protection)); case ElementType.ValueType: var type = TypeDefOrRefSignature.FromReader(image, reader, protection); type.IsValueType = true; return(type); case ElementType.Var: return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Type)); default: return(MsCorLibTypeSignature.FromElementType(image, elementType)); } }
public static TypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader) { var elementType = (ElementType)reader.ReadByte(); switch (elementType) { case ElementType.Array: return(ArrayTypeSignature.FromReader(header, reader)); case ElementType.Boxed: return(BoxedTypeSignature.FromReader(header, reader)); case ElementType.ByRef: return(ByReferenceTypeSignature.FromReader(header, reader)); case ElementType.CModOpt: return(OptionalModifierSignature.FromReader(header, reader)); case ElementType.CModReqD: return(RequiredModifierSignature.FromReader(header, reader)); case ElementType.Class: return(TypeDefOrRefSignature.FromReader(header, reader)); case ElementType.FnPtr: return(FunctionPointerTypeSignature.FromReader(header, reader)); case ElementType.GenericInst: return(GenericInstanceTypeSignature.FromReader(header, reader)); case ElementType.MVar: return(GenericParameterSignature.FromReader(header, reader, GenericParameterType.Method)); case ElementType.Pinned: return(PinnedTypeSignature.FromReader(header, reader)); case ElementType.Ptr: return(PointerTypeSignature.FromReader(header, reader)); case ElementType.Sentinel: return(SentinelTypeSignature.FromReader(header, reader)); case ElementType.SzArray: return(SzArrayTypeSignature.FromReader(header, reader)); case ElementType.ValueType: var type = TypeDefOrRefSignature.FromReader(header, reader); type.IsValueType = true; return(type); case ElementType.Var: return(GenericParameterSignature.FromReader(header, reader, GenericParameterType.Type)); default: return(MsCorLibTypeSignature.FromElementType(header, elementType)); } throw new NotSupportedException(); }
private TypeDefOrRefSignature ImportTypeDefOrRefSignature(TypeDefOrRefSignature signature) { return new TypeDefOrRefSignature(ImportType(signature.Type)) { IsValueType = signature.IsValueType }; }
/// <summary> /// Determines whether two types are considered equal according to their signature. /// </summary> /// <param name="signature1">The first type to compare.</param> /// <param name="descriptor">The second type to compare.</param> /// <returns><c>True</c> if the types are considered equal, <c>False</c> otherwise.</returns> public bool MatchTypes(TypeDefOrRefSignature signature1, ITypeDescriptor descriptor) { if (signature1 == null && descriptor == null) return true; if (signature1 == null || descriptor == null) return false; var signature2 = descriptor as TypeDefOrRefSignature; if (signature2 != null) return MatchTypes(signature1.Type, signature2.Type); var corlibType = descriptor as MsCorLibTypeSignature; if (corlibType != null) return MatchTypes(signature1.Type, corlibType.Type); var typeDefOrRef = descriptor as ITypeDefOrRef; if (typeDefOrRef != null) return MatchTypes(signature1.Type, typeDefOrRef); return false; }
public void MatchTypeDefOrRefSignature() { const string typeNamespace = "SomeNamespace"; const string typeName = "SomeType"; var typeRef1 = new TypeReference(CreateAssemblyReference(), typeNamespace, typeName); var typeRef2 = new TypeReference(CreateAssemblyReference(), typeNamespace, typeName); var typeRef3 = new TypeReference(CreateAssemblyReference(), typeNamespace, typeName + "1"); var resolutionScope = CreateAssemblyReference(); resolutionScope.Name += "1"; var typeRef4 = new TypeReference(resolutionScope, typeNamespace, typeName); ITypeDescriptor type1 = new TypeDefOrRefSignature(typeRef1); ITypeDescriptor type2 = new TypeDefOrRefSignature(typeRef2); ITypeDescriptor type3 = new TypeDefOrRefSignature(typeRef3); ITypeDescriptor type4 = new TypeDefOrRefSignature(typeRef4); Assert.IsTrue(_comparer.MatchTypes(type1, type2), "The same types did not match each other."); Assert.IsFalse(_comparer.MatchTypes(type1, type3), "A name change matched the original."); Assert.IsFalse(_comparer.MatchTypes(type1, type4), "A resolution scope change matched the original."); }