本文整理汇总了C#中IReturnType.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# IReturnType.Equals方法的具体用法?C# IReturnType.Equals怎么用?C# IReturnType.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IReturnType
的用法示例。
在下文中一共展示了IReturnType.Equals方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBetterConversion
/// <summary>
/// Gets if the conversion from <paramref name="from"/> to <paramref name="to1"/> is better than
/// the conversion from <paramref name="from"/> to <paramref name="to2"/>.
/// </summary>
/// <returns>
/// 0 = neither conversion is better<br/>
/// 1 = from -> to1 is the better conversion<br/>
/// 2 = from -> to2 is the better conversion.
/// </returns>
public static int GetBetterConversion(IReturnType from, IReturnType to1, IReturnType to2)
{
if (from == null) return 0;
if (to1 == null) return 2;
if (to2 == null) return 1;
// See ECMA-334, § 14.4.2.3
// If T1 and T2 are the same type, neither conversion is better.
if (to1.Equals(to2)) {
return 0;
}
// If S is T1, C1 is the better conversion.
if (from.Equals(to1)) {
return 1;
}
// If S is T2, C2 is the better conversion.
if (from.Equals(to2)) {
return 2;
}
bool canConvertFrom1To2 = ConversionExists(to1, to2);
bool canConvertFrom2To1 = ConversionExists(to2, to1);
// If an implicit conversion from T1 to T2 exists, and no implicit conversion
// from T2 to T1 exists, C1 is the better conversion.
if (canConvertFrom1To2 && !canConvertFrom2To1) {
return 1;
}
// If an implicit conversion from T2 to T1 exists, and no implicit conversion
// from T1 to T2 exists, C2 is the better conversion.
if (canConvertFrom2To1 && !canConvertFrom1To2) {
return 2;
}
if (to1.IsDefaultReturnType && to2.IsDefaultReturnType) {
return GetBetterPrimitiveConversion(to1, to2);
}
// Otherwise, neither conversion is better.
return 0;
}
示例2: ConversionExists
/// <summary>
/// Checks if an implicit conversion exists from <paramref name="from"/> to <paramref name="to"/>.
/// </summary>
public static bool ConversionExists(IReturnType from, IReturnType to)
{
// ECMA-334, § 13.1 Implicit conversions
// Identity conversion:
if (from == to) return true;
if (from == null || to == null) return false;
if (from.Equals(to)) {
return true;
}
bool fromIsDefault = from.IsDefaultReturnType;
bool toIsDefault = to.IsDefaultReturnType;
if (fromIsDefault && toIsDefault) {
// Implicit numeric conversions:
int f = GetPrimitiveType(from);
int t = GetPrimitiveType(to);
if (f == SByte && (t == Short || t == Int || t == Long || t == Float || t == Double || t == Decimal))
return true;
if (f == Byte && (t == Short || t == UShort || t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal))
return true;
if (f == Short && (t == Int || t == Long || t == Float || t == Double || t == Decimal))
return true;
if (f == UShort && (t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal))
return true;
if (f == Int && (t == Long || t == Float || t == Double || t == Decimal))
return true;
if (f == UInt && (t == Long || t == ULong || t == Float || t == Double || t == Decimal))
return true;
if ((f == Long || f == ULong) && (t == Float || t == Double || t == Decimal))
return true;
if (f == Char && (t == UShort || t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal))
return true;
if (f == Float && t == Double)
return true;
}
// Implicit reference conversions:
if (toIsDefault && to.FullyQualifiedName == "System.Object") {
return true; // from any type to object
}
if (toIsDefault && (fromIsDefault || from.IsArrayReturnType)) {
IClass c1 = from.GetUnderlyingClass();
IClass c2 = to.GetUnderlyingClass();
if (c1 != null && c1.IsTypeInInheritanceTree(c2)) {
return true;
}
}
if (from.IsArrayReturnType && to.IsArrayReturnType) {
ArrayReturnType fromArt = from.CastToArrayReturnType();
ArrayReturnType toArt = to.CastToArrayReturnType();
// from array to other array type
if (fromArt.ArrayDimensions == toArt.ArrayDimensions) {
return ConversionExists(fromArt.ArrayElementType, toArt.ArrayElementType);
}
}
if (from.IsConstructedReturnType && to.IsConstructedReturnType) {
if (from.FullyQualifiedName == to.FullyQualifiedName) {
IList<IReturnType> fromTypeArguments = from.CastToConstructedReturnType().TypeArguments;
IList<IReturnType> toTypeArguments = to.CastToConstructedReturnType().TypeArguments;
if (fromTypeArguments.Count == toTypeArguments.Count) {
for (int i = 0; i < fromTypeArguments.Count; i++) {
if (fromTypeArguments[i] == toTypeArguments[i])
continue;
if (object.Equals(fromTypeArguments[i], toTypeArguments[i]))
continue;
if (!(toTypeArguments[i].IsGenericReturnType))
return false;
}
return true;
}
}
}
return false;
}