本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Resolver.TypeInference.FindTypeInBounds方法的典型用法代码示例。如果您正苦于以下问题:C# TypeInference.FindTypeInBounds方法的具体用法?C# TypeInference.FindTypeInBounds怎么用?C# TypeInference.FindTypeInBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.CSharp.Resolver.TypeInference
的用法示例。
在下文中一共展示了TypeInference.FindTypeInBounds方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GuessAstType
public static AstType GuessAstType(RefactoringContext context, AstNode expr)
{
var type = GetValidTypes(context.Resolver, expr).ToArray();
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.Improved;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
if (inferedType.Kind == TypeKind.Unknown)
return new PrimitiveType("object");
return context.CreateShortType(inferedType);
}
示例2: CreateConstructorCompletionData
IEnumerable<ICompletionData> CreateConstructorCompletionData(IType hintType)
{
var wrapper = new CompletionDataWrapper(this);
var state = GetState();
Func<IType, IType> pred = null;
Action<ICompletionData, IType> typeCallback = null;
var inferredTypesCategory = new Category("Inferred Types", null);
var derivedTypesCategory = new Category("Derived Types", null);
if (hintType != null) {
if (hintType.Kind != TypeKind.Unknown) {
var lookup = new MemberLookup(
ctx.CurrentTypeDefinition,
Compilation.MainAssembly
);
typeCallback = (data, t) => {
//check if type is in inheritance tree.
if (hintType.GetDefinition() != null &&
t.GetDefinition() != null &&
t.GetDefinition().IsDerivedFrom(hintType.GetDefinition())) {
data.CompletionCategory = derivedTypesCategory;
}
};
pred = t => {
if (t.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array) {
return null;
}
// check for valid constructors
if (t.GetConstructors().Count() > 0) {
bool isProtectedAllowed = currentType != null ?
currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) : false;
if (!t.GetConstructors().Any(m => lookup.IsAccessible(m, isProtectedAllowed))) {
return null;
}
}
// check derived types
var typeDef = t.GetDefinition();
var hintDef = hintType.GetDefinition();
if (typeDef != null && hintDef != null && typeDef.IsDerivedFrom(hintDef)) {
var newType = wrapper.AddType(t, true);
if (newType != null) {
newType.CompletionCategory = inferredTypesCategory;
}
}
// check type inference
var typeInference = new TypeInference(Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(new [] { t }, new [] { hintType });
if (inferedType != SpecialType.UnknownType) {
var newType = wrapper.AddType(inferedType, true);
if (newType != null) {
newType.CompletionCategory = inferredTypesCategory;
}
return null;
}
return t;
};
if (!(hintType.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array)) {
var hint = wrapper.AddType(hintType, true);
if (hint != null) {
DefaultCompletionString = hint.DisplayText;
hint.CompletionCategory = derivedTypesCategory;
}
}
if (hintType is ParameterizedType && hintType.TypeParameterCount == 1 && hintType.FullName == "System.Collections.Generic.IEnumerable") {
var arg = ((ParameterizedType)hintType).TypeArguments.FirstOrDefault();
if (arg.Kind != TypeKind.TypeParameter) {
var array = new ArrayType(ctx.Compilation, arg, 1);
wrapper.AddType(array, true);
}
}
} else {
var hint = wrapper.AddType(hintType, true);
if (hint != null) {
DefaultCompletionString = hint.DisplayText;
hint.CompletionCategory = derivedTypesCategory;
}
}
}
AddTypesAndNamespaces(wrapper, state, null, pred, m => false, typeCallback, true);
if (hintType == null || hintType == SpecialType.UnknownType) {
AddKeywords(wrapper, primitiveTypesKeywords.Where(k => k != "void"));
}
CloseOnSquareBrackets = true;
AutoCompleteEmptyMatch = true;
AutoCompleteEmptyMatchOnCurlyBracket = false;
return wrapper.Result;
}
示例3: GuessType
public static IType GuessType(BaseRefactoringContext context, AstNode expr)
{
if (expr is SimpleType && expr.Role == Roles.TypeArgument) {
if (expr.Parent is MemberReferenceExpression || expr.Parent is IdentifierExpression) {
var rr = context.Resolve (expr.Parent);
var argumentNumber = expr.Parent.GetChildrenByRole (Roles.TypeArgument).TakeWhile (c => c != expr).Count ();
var mgrr = rr as MethodGroupResolveResult;
if (mgrr != null && mgrr.Methods.Any () && mgrr.Methods.First ().TypeArguments.Count > argumentNumber)
return mgrr.Methods.First ().TypeParameters[argumentNumber];
} else if (expr.Parent is MemberType || expr.Parent is SimpleType) {
var rr = context.Resolve (expr.Parent);
var argumentNumber = expr.Parent.GetChildrenByRole (Roles.TypeArgument).TakeWhile (c => c != expr).Count ();
var mgrr = rr as TypeResolveResult;
if (mgrr != null && mgrr.Type.TypeParameterCount > argumentNumber) {
return mgrr.Type.GetDefinition ().TypeParameters[argumentNumber];
}
}
}
var type = GetValidTypes(context.Resolver, expr).ToArray();
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.Improved;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
return inferedType;
}
示例4: GuessType
public static IType GuessType(RefactoringContext context, Expression expr)
{
var type = GetValidTypes(context.Resolver, expr).ToArray();
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.Improved;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
return inferedType;
}
示例5: CreateTypeCompletionData
IEnumerable<ICompletionData> CreateTypeCompletionData(IType hintType, AstType hintTypeAst)
{
var wrapper = new CompletionDataWrapper(this);
var state = GetState();
Func<IType, IType> pred = null;
if (hintType != null) {
if (hintType.Kind != TypeKind.Unknown) {
var lookup = new MemberLookup(ctx.CurrentTypeDefinition, Compilation.MainAssembly);
pred = t => {
// check if type is in inheritance tree.
if (hintType.GetDefinition() != null && !t.GetDefinition().IsDerivedFrom(hintType.GetDefinition())) {
return null;
}
if (t.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array) {
return null;
}
// check for valid constructors
if (t.GetConstructors().Count() > 0) {
bool isProtectedAllowed = currentType != null ? currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) : false;
if (!t.GetConstructors().Any(m => lookup.IsAccessible(m, isProtectedAllowed)))
return null;
}
var typeInference = new TypeInference(Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(new [] { t }, new [] { hintType });
wrapper.AddType(inferedType, amb.ConvertType(inferedType));
return null;
};
if (!(hintType.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array)) {
DefaultCompletionString = GetShortType(hintType, GetState());
wrapper.AddType(hintType, DefaultCompletionString);
}
if (hintType is ParameterizedType && hintType.TypeParameterCount == 1 && hintType.FullName == "System.Collections.Generic.IEnumerable") {
var arg = ((ParameterizedType)hintType).TypeArguments.FirstOrDefault();
var array = new ArrayTypeReference(arg.ToTypeReference(), 1).Resolve(ctx);
wrapper.AddType(array, amb.ConvertType(array));
}
} else {
DefaultCompletionString = hintTypeAst.ToString();
wrapper.AddType(hintType, DefaultCompletionString);
}
}
AddTypesAndNamespaces(wrapper, state, null, pred, m => false);
if (hintType == null || hintType == SpecialType.UnknownType)
AddKeywords(wrapper, primitiveTypesKeywords.Where(k => k != "void"));
CloseOnSquareBrackets = true;
AutoCompleteEmptyMatch = true;
return wrapper.Result;
}