public void ImportGenericInstanceType() { var assembly = Utilities.CreateTempNetAssembly(); var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var importer = new ReferenceImporter(tableStream); var signature = new GenericInstanceTypeSignature(CreateTypeReference(typeof(List <>))); var genericArg = CreateTypeDefOrRef(typeof(Form)); signature.GenericArguments.Add(genericArg); var newSignature = importer.ImportTypeSignature(signature); Assert.AreNotSame(signature, newSignature, "Imported signature is the same object as original."); Assert.IsTrue(_comparer.MatchTypes(signature, newSignature), "Imported signature does not match original."); Assert.IsTrue(tableStream.GetTable <TypeReference>().FirstOrDefault(x => _comparer.MatchTypes(x, signature.GenericType)) != null, "Generic type reference not added to table."); Assert.IsTrue(tableStream.GetTable <TypeReference>().FirstOrDefault(x => _comparer.MatchTypes(x, genericArg.Type)) != null, "Generic type argument not added to table."); }
public void CreateGenericInstanceField() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var fieldTable = tableStream.GetTable <FieldDefinition>(); var importer = new ReferenceImporter(tableStream); // create field. var typeSignature = new GenericInstanceTypeSignature(importer.ImportType(typeof(List <>))); typeSignature.GenericArguments.Add(typeSystem.String); var field = new FieldDefinition(FieldName, FieldAttributes.Public | FieldAttributes.Static, new FieldSignature(typeSignature)); fieldTable.Add(field); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); fieldTable = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>().GetTable <FieldDefinition>(); field = fieldTable.First(x => x.Name == FieldName); Assert.IsInstanceOfType(field.Signature.FieldType, typeof(GenericInstanceTypeSignature)); var newTypeSignature = (GenericInstanceTypeSignature)field.Signature.FieldType; Utilities.ValidateType(typeSignature.GenericType, newTypeSignature.GenericType); Assert.AreEqual(typeSignature.GenericArguments.Count, newTypeSignature.GenericArguments.Count); for (int i = 0; i < typeSignature.GenericArguments.Count; i++) { Assert.AreEqual(typeSignature.GenericArguments[i].FullName, newTypeSignature.GenericArguments[i].FullName); } }
private TypeSignature ParseGenericTypeSpec(TypeSignature typeName) { var result = new GenericInstanceTypeSignature(typeName.ToTypeDefOrRef(), typeName.IsValueType); result.TypeArguments.Add(ParseGenericTypeArgument(result)); bool stop = false; while (!stop) { var nextToken = Expect(TypeNameTerminal.CloseBracket, TypeNameTerminal.Comma); switch (nextToken.Terminal) { case TypeNameTerminal.CloseBracket: stop = true; break; case TypeNameTerminal.Comma: result.TypeArguments.Add(ParseGenericTypeArgument(result)); break; } } return(result); }
public void ImportGenericInstanceType() { var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true); var image = assembly.NetDirectory.MetadataHeader.LockMetadata(); var importer = new ReferenceImporter(image); var signature = new GenericInstanceTypeSignature(CreateTypeReference(typeof(List <>))); var genericArg = CreateTypeDefOrRef(typeof(Form)); signature.GenericArguments.Add(genericArg); var newSignature = importer.ImportTypeSignature(signature); Assert.NotSame(signature, newSignature); Assert.Equal(signature, newSignature, _comparer); var newGenericSiganture = (GenericInstanceTypeSignature)newSignature; Assert.Equal(image, newGenericSiganture.GenericType.Image); var genericArgElementType = newGenericSiganture.GenericArguments[0].GetElementType(); Assert.IsAssignableFrom <ITypeDefOrRef>(genericArgElementType); Assert.Equal(image, ((ITypeDefOrRef)genericArgElementType).Image); }
/// <inheritdoc /> public bool Equals(TypeSignature x, TypeSignature y) { if (ReferenceEquals(x, y)) { return(true); } if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) { return(false); } return(x switch { CorLibTypeSignature corLibType => Equals(corLibType, y as CorLibTypeSignature), TypeDefOrRefSignature typeDefOrRef => Equals(typeDefOrRef, y as TypeDefOrRefSignature), SzArrayTypeSignature szArrayType => Equals(szArrayType, y as SzArrayTypeSignature), ArrayTypeSignature arrayType => Equals(arrayType, y as ArrayTypeSignature), ByReferenceTypeSignature byRefType => Equals(byRefType, y as ByReferenceTypeSignature), BoxedTypeSignature boxedType => Equals(boxedType, y as BoxedTypeSignature), GenericInstanceTypeSignature genericInstanceType => Equals(genericInstanceType, y as GenericInstanceTypeSignature), GenericParameterSignature genericParameter => Equals(genericParameter, y as GenericParameterSignature), PointerTypeSignature pointerType => Equals(pointerType, y as PointerTypeSignature), PinnedTypeSignature pinnedType => Equals(pinnedType, y as PinnedTypeSignature), CustomModifierTypeSignature modifierType => Equals(modifierType, y as CustomModifierTypeSignature), _ => throw new NotSupportedException() });
public void ComplexGenericTypeInstance() { var spec = CreateDummyType(); var image = spec.Image; var importer = new ReferenceImporter(image); // Tuple<Tuple<decimal, decimal>, Tuple<decimal, decimal>> var signature = new GenericInstanceTypeSignature(importer.ImportType(typeof(Tuple <,>)), new GenericInstanceTypeSignature( importer.ImportType(typeof(Tuple <,>)), importer.ImportTypeSignature(typeof(decimal)), importer.ImportTypeSignature(typeof(decimal))), new GenericInstanceTypeSignature( importer.ImportType(typeof(Tuple <,>)), importer.ImportTypeSignature(typeof(decimal)), importer.ImportTypeSignature(typeof(decimal)))); spec.Signature = signature; var header = image.Header; var mapping = header.UnlockMetadata(); var newImage = header.LockMetadata(); var newSpec = (TypeSpecification)newImage.ResolveMember(mapping[spec]); Assert.Equal(signature, newSpec.Signature, Comparer); }
private TypeSignature ParseGenericTypeArgument(GenericInstanceTypeSignature genericInstance) { Expect(TypeNameTerminal.OpenBracket); var result = ParseTypeSpec(); Expect(TypeNameTerminal.CloseBracket); return(result); }
/// <inheritdoc /> public int GetHashCode(GenericInstanceTypeSignature obj) { unchecked { int hashCode = (int)obj.ElementType << ElementTypeOffset; hashCode = (hashCode * 397) ^ (obj.GenericType != null ? obj.GenericType.GetHashCode() : 0); hashCode = (hashCode * 397) ^ GetHashCode(obj.TypeArguments); return(hashCode); } }
private TypeSignature ParseGenericTypeArgument(GenericInstanceTypeSignature genericInstance) { var extraBracketToken = TryExpect(TypeNameTerminal.OpenBracket); var result = ParseTypeSpec(); if (extraBracketToken.HasValue) { Expect(TypeNameTerminal.CloseBracket); } return(result); }
public void InstantiateSimpleGenericInstanceType() { var signature = new GenericInstanceTypeSignature(_dummyGenericType, false, new GenericParameterSignature(GenericParameterType.Type, 0)); var context = new GenericContext(GetProvider(_module.CorLibTypeFactory.String), null); var newSignature = signature.InstantiateGenericTypes(context); Assert.Equal(new GenericInstanceTypeSignature(_dummyGenericType, false, _module.CorLibTypeFactory.String), newSignature, Comparer); }
public void ResolveMethodGenericParameterWithOnlyTypeShouldThrow() { var genericInstance = new GenericInstanceTypeSignature(_importer.ImportType(typeof(List <>)), false); genericInstance.TypeArguments.Add(_importer.ImportTypeSignature(typeof(string))); var context = new GenericContext(genericInstance, null); var parameter = new GenericParameterSignature(GenericParameterType.Method, 0); Assert.Throws <ArgumentOutOfRangeException>(() => context.GetTypeArgument(parameter)); }
public void ResolveTypeGenericParameterWithType() { var genericInstance = new GenericInstanceTypeSignature(_importer.ImportType(typeof(List <>)), false); genericInstance.TypeArguments.Add(_importer.ImportTypeSignature(typeof(string))); var context = new GenericContext(genericInstance, null); var parameter = new GenericParameterSignature(GenericParameterType.Type, 0); Assert.Equal("System.String", context.GetTypeArgument(parameter).FullName); }
/// <inheritdoc /> public TypeMemoryLayout VisitGenericInstanceType(GenericInstanceTypeSignature signature) { // Enter new generic context. var oldContext = _currentGenericContext; _currentGenericContext = _currentGenericContext.WithType(signature); var result = VisitTypeDefOrRef(signature.GenericType); // Leave generic context. _currentGenericContext = oldContext; return(result); }
private GenericInstanceTypeSignature ImportGenericInstanceTypeSignature(Type type) { var signature = new GenericInstanceTypeSignature(ImportType(type)) { IsValueType = type.IsValueType }; foreach (var argument in type.GetGenericArguments()) { signature.GenericArguments.Add(ImportTypeSignature(argument)); } return(signature); }
private GenericInstanceTypeSignature ImportGenericInstanceTypeSignature(GenericInstanceTypeSignature signature) { var newSignature = new GenericInstanceTypeSignature(ImportType(signature.GenericType)) { IsValueType = signature.IsValueType }; foreach (var argument in signature.GenericArguments) { newSignature.GenericArguments.Add(ImportTypeSignature(argument)); } return(newSignature); }
public void GenericTypeSingleBrackets() { const string ns = "MyNamespace"; const string name = "MyType"; var elementType = new TypeReference(_module, ns, name); var argumentType = _module.CorLibTypeFactory.Object; var expected = new GenericInstanceTypeSignature(elementType, false, argumentType); var actual = TypeNameParser.Parse(_module, $"{ns}.{name}[{argumentType.Namespace}.{argumentType.Name}]"); Assert.Equal(expected, actual, _comparer); }
public void GenericTypeArgument(bool rebuild) { // https://github.com/Washi1337/AsmResolver/issues/92 var attribute = GetCustomAttributeTestCase(nameof(CustomAttributesTestClass.GenericType), rebuild); var argument = attribute.Signature.FixedArguments[0]; var module = attribute.Constructor.Module; var nestedClass = (TypeDefinition)module.LookupMember(typeof(TestGenericType <>).MetadataToken); var expected = new GenericInstanceTypeSignature(nestedClass, false, module.CorLibTypeFactory.Object); Assert.IsAssignableFrom <TypeSignature>(argument.Element); Assert.Equal(expected, (TypeSignature)argument.Element, _comparer); }
/// <summary> /// Determines whether two types are considered equal according to their signature. /// </summary> /// <param name="signature1">The first type to compare.</param> /// <param name="signature2">The second type to compare.</param> /// <returns><c>True</c> if the types are considered equal, <c>False</c> otherwise.</returns> public bool Equals(GenericInstanceTypeSignature signature1, GenericInstanceTypeSignature signature2) { if (signature1 == null && signature2 == null) { return(true); } if (signature1 == null || signature2 == null) { return(false); } return(Equals(signature1.GenericType, signature2.GenericType) && EqualsManyTypes(signature1.GenericArguments, signature2.GenericArguments)); }
public void GenericTypeMultiBrackets(string argNs, string argName) { const string ns = "MyNamespace"; const string name = "MyType"; var elementType = new TypeReference(_module, ns, name); var argumentType = new TypeReference(_module, _module, argNs, argName) .ToTypeSignature(); var expected = new GenericInstanceTypeSignature(elementType, false, argumentType); var actual = TypeNameParser.Parse(_module, $"{ns}.{name}[[{argumentType.Namespace}.{argumentType.Name}]]"); Assert.Equal(expected, actual, _comparer); }
public void InstantiateSimpleGenericInstanceType() { var genericType = new TypeReference( new AssemblyReference("SomeAssembly", new Version()), "SomeNamespace", "SomeType"); var signature = new GenericInstanceTypeSignature(genericType, false, new GenericParameterSignature(GenericParameterType.Type, 0)); var context = new GenericContext(GetProvider(_module.CorLibTypeFactory.String), null); var newSignature = signature.InstantiateGenericTypes(context); Assert.Equal(new GenericInstanceTypeSignature(genericType, false, _module.CorLibTypeFactory.String), newSignature, Comparer); }
/// <inheritdoc /> public bool Equals(GenericInstanceTypeSignature x, GenericInstanceTypeSignature y) { if (ReferenceEquals(x, y)) { return(true); } if (x is null || y is null) { return(false); } return(x.IsValueType == y.IsValueType && Equals(x.GenericType, y.GenericType) && Equals(x.TypeArguments, y.TypeArguments)); }
public void GenericType(GenericParameterType[] parameterTypes, int[] parameterIndices) { // Tuple<!0, !1, !2> var genericInstance = new GenericInstanceTypeSignature(_importer.ImportType(typeof(Tuple <, ,>)), new GenericParameterSignature(parameterTypes[0], parameterIndices[0]), new GenericParameterSignature(parameterTypes[1], parameterIndices[1]), new GenericParameterSignature(parameterTypes[2], parameterIndices[2]) ); Assert.Equal(new GenericInstanceTypeSignature(_importer.ImportType(typeof(Tuple <, ,>)), GetTypeArguments(parameterTypes[0])[parameterIndices[0]], GetTypeArguments(parameterTypes[1])[parameterIndices[1]], GetTypeArguments(parameterTypes[2])[parameterIndices[2]] ), genericInstance.InstantiateGenericTypes(_context), Comparer); }
public void InstantiateGenericInstanceTypeWithTypeAndMethodArgument() { var signature = new GenericInstanceTypeSignature(_dummyGenericType, false, new GenericParameterSignature(GenericParameterType.Type, 0), new GenericParameterSignature(GenericParameterType.Method, 0)); var context = new GenericContext( GetProvider(_module.CorLibTypeFactory.String), GetProvider(_module.CorLibTypeFactory.Int32)); var newSignature = signature.InstantiateGenericTypes(context); Assert.Equal(new GenericInstanceTypeSignature(_dummyGenericType, false, _module.CorLibTypeFactory.String, _module.CorLibTypeFactory.Int32), newSignature, Comparer); }
public void ResolveMethodGenericParameterWithTypeAndMethod() { var genericType = new GenericInstanceTypeSignature(_importer.ImportType(typeof(List <>)), false); genericType.TypeArguments.Add(_importer.ImportTypeSignature(typeof(string))); var genericMethod = new GenericInstanceMethodSignature(); genericMethod.TypeArguments.Add(_importer.ImportTypeSignature(typeof(int))); var context = new GenericContext(genericType, genericMethod); var parameter = new GenericParameterSignature(GenericParameterType.Method, 0); Assert.Equal("System.Int32", context.GetTypeArgument(parameter).FullName); }
public void MatchGenericTypeSignatures() { var expected = new GenericInstanceTypeSignature(CreateTypeRef1()); expected.GenericArguments.Add(CreateTypeSig3()); var match = new GenericInstanceTypeSignature(CreateTypeRef1()); match.GenericArguments.Add(CreateTypeSig3()); var fail1 = new GenericInstanceTypeSignature(CreateTypeRef2()); fail1.GenericArguments.Add(CreateTypeSig1()); var fail2 = new GenericInstanceTypeSignature(CreateTypeRef1()); fail2.GenericArguments.Add(CreateTypeSig2()); VerifyMatching(expected, match, fail1, fail2); }
public void FixedComplexTypeArgument(bool rebuild) { var attribute = GetCustomAttributeTestCase(nameof(CustomAttributesTestClass.FixedComplexTypeArgument), rebuild); Assert.Single(attribute.Signature.FixedArguments); Assert.Empty(attribute.Signature.NamedArguments); var argument = attribute.Signature.FixedArguments[0]; var factory = attribute.Constructor.Module.CorLibTypeFactory; var listRef = new TypeReference(factory.CorLibScope, "System.Collections.Generic", "KeyValuePair`2"); var instance = new GenericInstanceTypeSignature(listRef, false, new SzArrayTypeSignature(factory.String), new SzArrayTypeSignature(factory.Int32)); Assert.Equal(instance, argument.Element as TypeSignature, _comparer); }
public void SingleArgGenericTypeInstance() { var spec = CreateDummyType(); var image = spec.Image; var importer = new ReferenceImporter(image); var signature = new GenericInstanceTypeSignature(importer.ImportType(typeof(List <>)), image.TypeSystem.String); spec.Signature = signature; var header = image.Header; var mapping = header.UnlockMetadata(); var newImage = header.LockMetadata(); var newSpec = (TypeSpecification)newImage.ResolveMember(mapping[spec]); Assert.Equal(signature, newSpec.Signature, Comparer); }
/// <inheritdoc /> public object VisitGenericInstanceType(GenericInstanceTypeSignature signature) { WriteSimpleTypeName(signature.GenericType); _writer.Write('['); for (int i = 0; i < signature.TypeArguments.Count; i++) { _writer.Write('['); WriteTypeAssemblyQualifiedName(signature.TypeArguments[i]); _writer.Write(']'); if (i < signature.TypeArguments.Count - 1) { _writer.Write(','); } } _writer.Write(']'); return(null); }
public void NestedGenericTypes(GenericParameterType[] parameterTypes, int[] parameterIndices) { // Tuple<0!, List<!0>, Dictionary<!0, !1>> var genericInstance = new GenericInstanceTypeSignature(_importer.ImportType(typeof(Tuple <, ,>)), new GenericParameterSignature(parameterTypes[0], parameterIndices[0]), new GenericInstanceTypeSignature(_importer.ImportType(typeof(List <>)), new GenericParameterSignature(parameterTypes[1], parameterIndices[1])), new GenericInstanceTypeSignature(_importer.ImportType(typeof(Dictionary <,>)), new GenericParameterSignature(parameterTypes[2], parameterIndices[2]), new GenericParameterSignature(parameterTypes[3], parameterIndices[3])) ); Assert.Equal(new GenericInstanceTypeSignature(_importer.ImportType(typeof(Tuple <, ,>)), GetTypeArguments(parameterTypes[0])[parameterIndices[0]], new GenericInstanceTypeSignature(_importer.ImportType(typeof(List <>)), GetTypeArguments(parameterTypes[1])[parameterIndices[1]]), new GenericInstanceTypeSignature(_importer.ImportType(typeof(Dictionary <,>)), GetTypeArguments(parameterTypes[2])[parameterIndices[2]], GetTypeArguments(parameterTypes[3])[parameterIndices[3]]) ), genericInstance.InstantiateGenericTypes(_context), Comparer); }