本文整理汇总了C#中TypeReference.SupportsCeq方法的典型用法代码示例。如果您正苦于以下问题:C# TypeReference.SupportsCeq方法的具体用法?C# TypeReference.SupportsCeq怎么用?C# TypeReference.SupportsCeq使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeReference
的用法示例。
在下文中一共展示了TypeReference.SupportsCeq方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InjectEqualityCheck
void InjectEqualityCheck(Instruction targetInstruction, TypeReference targetType)
{
var nopInstruction = instructions.First();
if (nopInstruction.OpCode != OpCodes.Nop)
{
nopInstruction = Instruction.Create(OpCodes.Nop);
instructions.Insert(0, nopInstruction);
}
if (targetType.Name == "String")
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ldc_I4, typeEqualityFinder.OrdinalStringComparison),
Instruction.Create(OpCodes.Call, typeEqualityFinder.StringEquals),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
return;
}
var typeEqualityMethod = typeEqualityFinder.FindTypeEquality(targetType);
if (typeEqualityMethod == null)
{
if (targetType.IsGenericParameter)
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Box, targetType),
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Box, targetType),
Instruction.Create(OpCodes.Call, typeEqualityFinder.ObjectEqualsMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
else if (targetType.SupportsCeq())
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ceq),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
}
else
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Call, typeEqualityMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
}
示例2: InjectEqualityCheck
void InjectEqualityCheck(Instruction targetInstruction, TypeReference targetType)
{
if (ShouldSkipEqualityCheck())
{
return;
}
var nopInstruction = instructions.First();
if (nopInstruction.OpCode != OpCodes.Nop)
{
nopInstruction = Instruction.Create(OpCodes.Nop);
instructions.Insert(0, nopInstruction);
}
if (targetType.Name == "String")
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ldc_I4, typeEqualityFinder.OrdinalStringComparison),
Instruction.Create(OpCodes.Call, typeEqualityFinder.StringEquals),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
return;
}
var typeEqualityMethod = typeEqualityFinder.FindTypeEquality(targetType);
if (typeEqualityMethod == null)
{
if (targetType.SupportsCeq() && targetType.IsValueType)
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ceq),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
else if (targetType.IsValueType && typeEqualityFinder.EqualityComparerTypeReference != null)
{
var module = typeEqualityFinder.ModuleDefinition;
var ec = typeEqualityFinder.EqualityComparerTypeReference.Resolve();
var specificEqualityComparerType = module.ImportReference(ec.MakeGenericInstanceType(targetType));
var defaultProperty = module.ImportReference(ec.Properties.Single(p => p.Name == "Default").GetMethod);
var equalsMethod = module.ImportReference(ec.Methods.Single(p => p.Name == "Equals" && p.Parameters.Count == 2));
defaultProperty.DeclaringType = specificEqualityComparerType;
equalsMethod.DeclaringType = specificEqualityComparerType;
instructions.Prepend(
Instruction.Create(OpCodes.Call, defaultProperty),
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Callvirt, equalsMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
else if (targetType.IsValueType || targetType.IsGenericParameter)
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Box, targetType),
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Box, targetType),
Instruction.Create(OpCodes.Call, typeEqualityFinder.ObjectEqualsMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
else
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Call, typeEqualityFinder.ObjectEqualsMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
}
else
{
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
targetInstruction,
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Call, typeEqualityMethod),
Instruction.Create(OpCodes.Brfalse_S, nopInstruction),
Instruction.Create(OpCodes.Ret));
}
}