本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Resolver.OverloadResolution类的典型用法代码示例。如果您正苦于以下问题:C# OverloadResolution类的具体用法?C# OverloadResolution怎么用?C# OverloadResolution使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OverloadResolution类属于ICSharpCode.NRefactory.CSharp.Resolver命名空间,在下文中一共展示了OverloadResolution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LessArgumentsPassedToParamsIsBetter
public void LessArgumentsPassedToParamsIsBetter()
{
OverloadResolution r = new OverloadResolution(context, MakeArgumentList(typeof(int), typeof(int), typeof(int)));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeParamsMethod(typeof(int[]))));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeParamsMethod(typeof(int), typeof(int[]))));
Assert.IsFalse(r.IsAmbiguous);
Assert.AreEqual(2, r.BestCandidate.Parameters.Count);
}
示例2: PreferIntOverUInt
public void PreferIntOverUInt()
{
OverloadResolution r = new OverloadResolution(compilation, MakeArgumentList(typeof(ushort)));
var c1 = MakeMethod(typeof(int));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(c1));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeMethod(typeof(uint))));
Assert.IsFalse(r.IsAmbiguous);
Assert.AreSame(c1, r.BestCandidate);
}
示例3: NullableIntAndNullableUIntIsAmbiguous
public void NullableIntAndNullableUIntIsAmbiguous()
{
OverloadResolution r = new OverloadResolution(context, MakeArgumentList(typeof(ushort?)));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeMethod(typeof(int?))));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeMethod(typeof(uint?))));
Assert.AreEqual(OverloadResolutionErrors.AmbiguousMatch, r.BestCandidateErrors);
// then adding a matching overload solves the ambiguity:
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(MakeMethod(typeof(ushort?))));
Assert.AreEqual(OverloadResolutionErrors.None, r.BestCandidateErrors);
Assert.IsNull(r.BestCandidateAmbiguousWith);
}
示例4: InvocationResolveResult
public InvocationResolveResult(ResolveResult targetResult, OverloadResolution or, ITypeResolveContext context)
: base(
or.IsExtensionMethodInvocation ? null : targetResult,
or.GetBestCandidateWithSubstitutedTypeArguments(),
context)
{
this.OverloadResolutionErrors = or.BestCandidateErrors;
this.argumentToParameterMap = or.GetArgumentToParameterMap();
this.Arguments = or.GetArgumentsWithConversions();
this.IsExtensionMethodInvocation = or.IsExtensionMethodInvocation;
this.IsExpandedForm = or.BestCandidateIsExpandedForm;
this.IsLiftedOperatorInvocation = or.BestCandidate is OverloadResolution.ILiftedOperator;
}
示例5: BetterConversionByLambdaReturnValue_ExpressionTree
public void BetterConversionByLambdaReturnValue_ExpressionTree()
{
var m1 = MakeMethod(typeof(Func<long>));
var m2 = MakeMethod(typeof(Expression<Func<int>>));
// M(() => default(byte));
ResolveResult[] args = {
new MockLambda(compilation.FindType(KnownTypeCode.Byte))
};
OverloadResolution r = new OverloadResolution(compilation, args);
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m2));
Assert.AreSame(m2, r.BestCandidate);
Assert.AreEqual(OverloadResolutionErrors.None, r.BestCandidateErrors);
}
示例6: Lambda_DelegateAndExpressionTreeOverloadsAreAmbiguous
public void Lambda_DelegateAndExpressionTreeOverloadsAreAmbiguous()
{
var m1 = MakeMethod(typeof(Func<int>));
var m2 = MakeMethod(typeof(Expression<Func<int>>));
// M(() => default(int));
ResolveResult[] args = {
new MockLambda(compilation.FindType(KnownTypeCode.Int32))
};
OverloadResolution r = new OverloadResolution(compilation, args);
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m2));
Assert.AreEqual(OverloadResolutionErrors.AmbiguousMatch, r.BestCandidateErrors);
}
示例7: SkeetEvilOverloadResolution
public void SkeetEvilOverloadResolution()
{
// http://msmvps.com/blogs/jon_skeet/archive/2010/11/02/evil-code-overload-resolution-workaround.aspx
// static void Foo<T>(T? ignored = default(T?)) where T : struct
var m1 = MakeUnresolvedMethod();
m1.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.Method, 0, "T") { HasValueTypeConstraint = true });
m1.Parameters.Add(MakeOptionalParameter(
NullableType.Create(new TypeParameterReference(SymbolKind.Method, 0)),
"ignored"
));
// class ClassConstraint<T> where T : class {}
var classConstraint = new DefaultUnresolvedTypeDefinition(string.Empty, "ClassConstraint");
classConstraint.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true });
// static void Foo<T>(ClassConstraint<T> ignored = default(ClassConstraint<T>))
// where T : class
var m2 = MakeUnresolvedMethod();
m2.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.Method, 0, "T") { HasReferenceTypeConstraint = true });
m2.Parameters.Add(MakeOptionalParameter(
new ParameterizedTypeReference(classConstraint, new[] { new TypeParameterReference(SymbolKind.Method, 0) }),
"ignored"
));
// static void Foo<T>()
var m3 = MakeUnresolvedMethod();
m3.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.Method, 0, "T"));
ICompilation compilation = TypeSystemHelper.CreateCompilation(classConstraint);
var context = new SimpleTypeResolveContext(compilation.MainAssembly);
IMethod resolvedM1 = (IMethod)m1.CreateResolved(context);
IMethod resolvedM2 = (IMethod)m2.CreateResolved(context);
IMethod resolvedM3 = (IMethod)m3.CreateResolved(context);
// Call: Foo<int>();
OverloadResolution o;
o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(int)) });
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM1));
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM2));
Assert.AreSame(resolvedM1, o.BestCandidate);
// Call: Foo<string>();
o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(string)) });
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM1));
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM2));
Assert.AreSame(resolvedM2, o.BestCandidate);
// Call: Foo<int?>();
o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(int?)) });
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM1));
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM2));
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM3));
Assert.AreSame(resolvedM3, o.BestCandidate);
}
示例8: PreferMethodWithoutOptionalParameters
public void PreferMethodWithoutOptionalParameters()
{
var m1 = MakeMethod();
var m2 = MakeMethod(1);
OverloadResolution r = new OverloadResolution(compilation, MakeArgumentList());
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m2));
Assert.IsFalse(r.IsAmbiguous);
Assert.AreSame(m1, r.BestCandidate);
}
示例9: ResolveIndexer
/// <summary>
/// Resolves an indexer access.
/// </summary>
/// <param name="target">Target expression.</param>
/// <param name="arguments">
/// Arguments passed to the indexer.
/// The resolver may mutate this array to wrap elements in <see cref="ConversionResolveResult"/>s!
/// </param>
/// <param name="argumentNames">
/// The argument names. Pass the null string for positional arguments.
/// </param>
/// <returns>ArrayAccessResolveResult, InvocationResolveResult, or ErrorResolveResult</returns>
public ResolveResult ResolveIndexer(ResolveResult target, ResolveResult[] arguments, string[] argumentNames = null)
{
switch (target.Type.Kind) {
case TypeKind.Dynamic:
for (int i = 0; i < arguments.Length; i++) {
arguments[i] = Convert(arguments[i], SpecialType.Dynamic);
}
return new ArrayAccessResolveResult(SpecialType.Dynamic, target, arguments);
case TypeKind.Array:
case TypeKind.Pointer:
// §7.6.6.1 Array access / §18.5.3 Pointer element access
AdjustArrayAccessArguments(arguments);
return new ArrayAccessResolveResult(((TypeWithElementType)target.Type).ElementType, target, arguments);
}
// §7.6.6.2 Indexer access
OverloadResolution or = new OverloadResolution(compilation, arguments, argumentNames, conversions: conversions);
MemberLookup lookup = CreateMemberLookup();
var indexers = lookup.LookupIndexers(target.Type);
or.AddMethodLists(indexers);
if (or.BestCandidate != null) {
return or.CreateResolveResult(target);
} else {
return ErrorResult;
}
}
示例10: CreateResolveResultForUserDefinedOperator
ResolveResult CreateResolveResultForUserDefinedOperator(OverloadResolution r)
{
return r.CreateResolveResult(null);
}
示例11: ResolveObjectCreation
/// <summary>
/// Resolves an object creation.
/// </summary>
/// <param name="type">Type of the object to create.</param>
/// <param name="arguments">
/// Arguments passed to the constructor.
/// The resolver may mutate this array to wrap elements in <see cref="ConversionResolveResult"/>s!
/// </param>
/// <param name="argumentNames">
/// The argument names. Pass the null string for positional arguments.
/// </param>
/// <param name="allowProtectedAccess">
/// Whether to allow calling protected constructors.
/// This should be false except when resolving constructor initializers.
/// </param>
/// <returns>InvocationResolveResult or ErrorResolveResult</returns>
public ResolveResult ResolveObjectCreation(IType type, ResolveResult[] arguments, string[] argumentNames = null, bool allowProtectedAccess = false)
{
if (type.Kind == TypeKind.Delegate && arguments.Length == 1) {
return Convert(arguments[0], type);
}
OverloadResolution or = new OverloadResolution(compilation, arguments, argumentNames, conversions: conversions);
MemberLookup lookup = CreateMemberLookup();
foreach (IMethod ctor in type.GetConstructors()) {
if (lookup.IsAccessible(ctor, allowProtectedAccess))
or.AddCandidate(ctor);
else
or.AddCandidate(ctor, OverloadResolutionErrors.Inaccessible);
}
if (or.BestCandidate != null) {
return or.CreateResolveResult(null);
} else {
return new ErrorResolveResult(type);
}
}
示例12: SkeetEvilOverloadResolution
public void SkeetEvilOverloadResolution()
{
// http://msmvps.com/blogs/jon_skeet/archive/2010/11/02/evil-code-overload-resolution-workaround.aspx
// static void Foo<T>(T? ignored = default(T?)) where T : struct
var m1 = MakeMethod();
m1.TypeParameters.Add(new DefaultTypeParameter(m1, 0, "T") { HasValueTypeConstraint = true });
m1.Parameters.Add(MakeOptionalParameter(
NullableType.Create(m1.TypeParameters[0], context),
"ignored"
));
// class ClassConstraint<T> where T : class {}
DefaultTypeDefinition classConstraint = new DefaultTypeDefinition(dummyClass, "ClassConstraint");
classConstraint.TypeParameters.Add(new DefaultTypeParameter(classConstraint, 0, "T") { HasReferenceTypeConstraint = true });
// static void Foo<T>(ClassConstraint<T> ignored = default(ClassConstraint<T>))
// where T : class
var m2 = MakeMethod();
m2.TypeParameters.Add(new DefaultTypeParameter(m2, 0, "T") { HasReferenceTypeConstraint = true });
m2.Parameters.Add(MakeOptionalParameter(
new ParameterizedType(classConstraint, new[] { m2.TypeParameters[0] }),
"ignored"
));
// static void Foo<T>()
var m3 = MakeMethod();
m3.TypeParameters.Add(new DefaultTypeParameter(m3, 0, "T"));
// Call: Foo<int>();
OverloadResolution o;
o = new OverloadResolution(context, new ResolveResult[0], typeArguments: new[] { typeof(int).ToTypeReference().Resolve(context) });
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(m2));
Assert.AreSame(m1, o.BestCandidate);
// Call: Foo<string>();
o = new OverloadResolution(context, new ResolveResult[0], typeArguments: new[] { typeof(string).ToTypeReference().Resolve(context) });
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(m2));
Assert.AreSame(m2, o.BestCandidate);
// Call: Foo<int?>();
o = new OverloadResolution(context, new ResolveResult[0], typeArguments: new[] { typeof(int?).ToTypeReference().Resolve(context) });
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(m2));
Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(m3));
Assert.AreSame(m3, o.BestCandidate);
}
示例13: CreateResolveResultForUserDefinedOperator
ResolveResult CreateResolveResultForUserDefinedOperator(OverloadResolution r, System.Linq.Expressions.ExpressionType operatorType)
{
if (r.BestCandidateErrors != OverloadResolutionErrors.None)
return r.CreateResolveResult(null);
IMethod method = (IMethod)r.BestCandidate;
return new OperatorResolveResult(method.ReturnType, operatorType, method,
isLiftedOperator: method is OverloadResolution.ILiftedOperator,
operands: r.GetArgumentsWithConversions());
}
示例14: BetterConversionByLambdaReturnValue
public void BetterConversionByLambdaReturnValue()
{
var m1 = MakeMethod(typeof(Func<long>));
var m2 = MakeMethod(typeof(Func<int>));
// M(() => default(byte));
ResolveResult[] args = {
new MockLambda(KnownTypeReference.Byte.Resolve(context))
};
OverloadResolution r = new OverloadResolution(context, args);
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m1));
Assert.AreEqual(OverloadResolutionErrors.None, r.AddCandidate(m2));
Assert.AreSame(m2, r.BestCandidate);
Assert.AreEqual(OverloadResolutionErrors.None, r.BestCandidateErrors);
}
示例15: CreateOverloadResolution
OverloadResolution CreateOverloadResolution(ResolveResult[] arguments, string[] argumentNames = null, IType[] typeArguments = null)
{
var or = new OverloadResolution(compilation, arguments, argumentNames, typeArguments, conversions);
or.CheckForOverflow = checkForOverflow;
return or;
}