public ResolveObjectCreation ( IType type, ResolveResult arguments, string argumentNames = null, bool allowProtectedAccess = false, IList |
||
type | IType | Type of the object to create. |
arguments | ResolveResult |
/// Arguments passed to the constructor.
/// The resolver may mutate this array to wrap elements in |
argumentNames | string | /// The argument names. Pass the null string for positional arguments. /// |
allowProtectedAccess | bool | /// Whether to allow calling protected constructors. /// This should be false except when resolving constructor initializers. /// |
initializerStatements | IList |
/// Statements for Objects/Collections initializer.
/// |
Résultat | ResolveResult |
public ResolveResult ResolveObjectCreation(IType type, ResolveResult[] arguments, string[] argumentNames = null, bool allowProtectedAccess = false, IList<ResolveResult> initializerStatements = null)
{
if (type.Kind == TypeKind.Delegate && arguments.Length == 1) {
ResolveResult input = arguments[0];
IMethod invoke = input.Type.GetDelegateInvokeMethod();
if (invoke != null) {
input = new MethodGroupResolveResult(
input, invoke.Name,
methods: new[] { new MethodListWithDeclaringType(input.Type) { invoke } },
typeArguments: EmptyList<IType>.Instance
);
}
return Convert(input, type);
}
OverloadResolution or = CreateOverloadResolution(arguments, argumentNames);
MemberLookup lookup = CreateMemberLookup();
var allApplicable = (arguments.Any(a => a.Type.Kind == TypeKind.Dynamic) ? new List<IMethod>() : null);
foreach (IMethod ctor in type.GetConstructors()) {
if (lookup.IsAccessible(ctor, allowProtectedAccess)) {
var orErrors = or.AddCandidate(ctor);
if (allApplicable != null && OverloadResolution.IsApplicable(orErrors))
allApplicable.Add(ctor);
}
else
or.AddCandidate(ctor, OverloadResolutionErrors.Inaccessible);
}
if (allApplicable != null && allApplicable.Count > 1) {
// If we have dynamic arguments, we need to represent the invocation as a dynamic invocation if there is more than one applicable constructor.
return new DynamicInvocationResolveResult(new MethodGroupResolveResult(null, allApplicable[0].Name, new[] { new MethodListWithDeclaringType(type, allApplicable) }, null), DynamicInvocationType.ObjectCreation, AddArgumentNamesIfNecessary(arguments, argumentNames), initializerStatements);
}
if (or.BestCandidate != null) {
return or.CreateResolveResult(null, initializerStatements);
} else {
return new ErrorResolveResult(type);
}
}
#endregion
public IMethod ResolveConstructor(ITypeResolveContext context) { CSharpResolver r = new CSharpResolver(context); IType type = attributeType.Resolve(context); int totalArgumentCount = 0; if (positionalArguments != null) totalArgumentCount += positionalArguments.Count; if (namedCtorArguments != null) totalArgumentCount += namedCtorArguments.Count; ResolveResult[] arguments = new ResolveResult[totalArgumentCount]; string[] argumentNames = new string[totalArgumentCount]; int i = 0; if (positionalArguments != null) { while (i < positionalArguments.Count) { IConstantValue cv = positionalArguments[i]; arguments[i] = new ConstantResolveResult(cv.GetValueType(context), cv.GetValue(context)); i++; } } if (namedCtorArguments != null) { foreach (var pair in namedCtorArguments) { argumentNames[i] = pair.Key; arguments[i] = new ConstantResolveResult(pair.Value.GetValueType(context), pair.Value.GetValue(context)); i++; } } MemberResolveResult mrr = r.ResolveObjectCreation(type, arguments, argumentNames) as MemberResolveResult; return mrr != null ? mrr.Member as IMethod : null; }