public static bool AreEqual(TypeReference firstType, TypeReference secondType) { if (firstType.GetFriendlyFullName(null) == secondType.GetFriendlyFullName(null)) { return true; } TypeReference firstTypeRef = firstType.GetElementType(); TypeReference secondTypeRef = secondType.GetElementType(); if (firstTypeRef.HasGenericParameters && secondTypeRef.HasGenericParameters) { if (firstTypeRef.FullName == secondTypeRef.FullName && firstTypeRef.GenericParameters.Count == secondTypeRef.GenericParameters.Count) { return true; } } return false; }
/// <summary> /// Checks if <paramref name="supposedSubType"/> is subtype of <paramref name="type"/>. /// </summary> /// <param name="type">The supposed parent type.</param> /// <param name="supposedSubType">The supposed inheriting type.</param> /// <returns>Returns True if <paramref name="supposedSubType"/> is subtype of <paramref name="type"/>.</returns> private bool IsSubtype(TypeReference type, TypeReference supposedSubType) { type = RemoveModifiers(type); supposedSubType = RemoveModifiers(supposedSubType); if (supposedSubType.GetFriendlyFullName(null) == type.GetFriendlyFullName(null) || type.FullName == "System.Object") { ///The types are the same, or the check is if a type inherits Object return true; } if (IsArrayAssignable(type, supposedSubType)) { return true; } TypeDefinition supposedSubTypeDef = supposedSubType.Resolve(); if (supposedSubTypeDef == null) { //happens with generics only; return true; } if (type is GenericInstanceType) { type = type.GetElementType(); } while (supposedSubTypeDef != null) { if (type.GetFriendlyFullName(null) == supposedSubTypeDef.GetFriendlyFullName(null)) { ///Must be here, since mono fails on resolving types sometimes. return true; } foreach (TypeReference @interface in supposedSubTypeDef.Interfaces) { if (@interface.Name == type.Name) { return true; } } if (supposedSubTypeDef.BaseType == null) { ///We are at the root of the class hierarcy, and no match was made. return false; } supposedSubTypeDef = supposedSubTypeDef.BaseType.Resolve(); } return false; }
protected void WriteType(TypeReference type, ILNameSyntax syntax) { if (type is ArrayType) { WriteArrayType(type, syntax); return; } string name = ILHelpers.PrimitiveTypeName(type.GetFriendlyFullName(Language)); if (syntax == ILNameSyntax.ShortTypeName) { if (name != null) WriteReference(name, type); else WriteReference(ILHelpers.Escape(type.Name), type); } else if ((syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) && name != null) { WriteReference(name, type); } else { if (syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) WriteKeyWordPostSpace(type.IsValueType ? "valuetype" : "class"); if (type.DeclaringType != null) { WriteTypeReference(type.DeclaringType, ILNameSyntax.TypeName); WriteKeyword("/"); WriteReference(ILHelpers.Escape(type.Name), type); } else { if (!type.IsDefinition && type.Scope != null && !(type is TypeSpecification)) { WriteToken("["); WriteEscaped(type.Scope.Name); WriteToken("]"); } WriteReference(ILHelpers.Escape(type.FullName), type); } } }