本文整理汇总了C#中RefactoringContext.Resolve方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.Resolve方法的具体用法?C# RefactoringContext.Resolve怎么用?C# RefactoringContext.Resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.Resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var varDecl = GetVariableDeclarationStatement(context);
IType type;
if (varDecl != null) {
type = context.Resolve(varDecl.Variables.First().Initializer).Type;
} else {
var foreachStatement = GetForeachStatement(context);
if (foreachStatement == null) {
yield break;
}
type = context.Resolve(foreachStatement.VariableType).Type;
}
if (!(!type.Equals(SpecialType.NullType) && !type.Equals(SpecialType.UnknownType))) {
yield break;
}
yield return new CodeAction (context.TranslateString("Use explicit type"), script => {
if (varDecl != null) {
script.Replace (varDecl.Type, context.CreateShortType (type));
} else {
var foreachStatement = GetForeachStatement (context);
script.Replace (foreachStatement.VariableType, context.CreateShortType (type));
}
});
}
示例2: GuessType
public static IType GuessType(RefactoringContext 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;
}
示例3: GetAnonymousMethodExpression
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
{
delegateType = null;
var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
return null;
IType resolvedType = null;
var parent = anonymousMethodExpression.Parent;
if (parent is AssignmentExpression) {
resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type;
} else if (parent is VariableDeclarationStatement) {
resolvedType = context.Resolve (((VariableDeclarationStatement)parent).Type).Type;
} else if (parent is InvocationExpression) {
// TODO: handle invocations
}
if (resolvedType == null)
return null;
delegateType = resolvedType;
if (delegateType.Kind != TypeKind.Delegate)
return null;
return anonymousMethodExpression;
}
示例4: GetActionsForAddNamespaceUsing
IEnumerable<CodeAction> GetActionsForAddNamespaceUsing(RefactoringContext context, AstNode node)
{
var nrr = context.Resolve(node) as NamespaceResolveResult;
if (nrr == null)
return EmptyList<CodeAction>.Instance;
var trr = context.Resolve(node.Parent) as TypeResolveResult;
if (trr == null)
return EmptyList<CodeAction>.Instance;
ITypeDefinition typeDef = trr.Type.GetDefinition();
if (typeDef == null)
return EmptyList<CodeAction>.Instance;
IList<IType> typeArguments;
ParameterizedType parameterizedType = trr.Type as ParameterizedType;
if (parameterizedType != null)
typeArguments = parameterizedType.TypeArguments;
else
typeArguments = EmptyList<IType>.Instance;
var resolver = context.GetResolverStateBefore(node.Parent);
if (resolver.ResolveSimpleName(typeDef.Name, typeArguments) is UnknownIdentifierResolveResult) {
// It's possible to remove the explicit namespace usage and introduce a using instead
return new[] { NewUsingAction(context, node, typeDef.Namespace) };
}
return EmptyList<CodeAction>.Instance;
}
示例5: Run
public void Run (RefactoringContext context)
{
using (var script = context.StartScript ()) {
var varDecl = GetVariableDeclarationStatement (context);
if (varDecl != null) {
var type = context.Resolve (varDecl.Variables.First ().Initializer).Type;
script.Replace (varDecl.Type, context.CreateShortType (type));
} else {
var foreachStatement = GetForeachStatement (context);
var type = context.Resolve (foreachStatement.VariableType).Type;
script.Replace (foreachStatement.VariableType, context.CreateShortType (type));
}
}
}
示例6: IsValid
public bool IsValid (RefactoringContext context)
{
var varDecl = GetVariableDeclarationStatement (context);
IType type;
if (varDecl != null) {
type = context.Resolve (varDecl.Variables.First ().Initializer).Type;
} else {
var foreachStatement = GetForeachStatement (context);
if (foreachStatement == null)
return false;
type = context.Resolve (foreachStatement.VariableType).Type;
}
return !type.Equals (SpecialType.NullType) && !type.Equals (SpecialType.UnknownType);
}
示例7: GetActionsForType
IEnumerable<CodeAction> GetActionsForType(RefactoringContext context, AstNode node)
{
var rr = context.Resolve(node) as UnknownIdentifierResolveResult;
if (rr == null)
return EmptyList<CodeAction>.Instance;
string identifier = rr.Identifier;
int tc = rr.TypeArgumentCount;
string attributeIdentifier = null;
if (node.Parent is Attribute)
attributeIdentifier = identifier + "Attribute";
var lookup = new MemberLookup(null, context.Compilation.MainAssembly);
List<CodeAction> actions = new List<CodeAction>();
foreach (var typeDefinition in context.Compilation.GetAllTypeDefinitions()) {
if ((typeDefinition.Name == identifier || typeDefinition.Name == attributeIdentifier)
&& typeDefinition.TypeParameterCount == tc
&& lookup.IsAccessible(typeDefinition, false))
{
if (typeDefinition.DeclaringTypeDefinition == null) {
actions.Add(NewUsingAction(context, node, typeDefinition.Namespace));
}
actions.Add(ReplaceWithFullTypeNameAction(context, node, typeDefinition));
}
}
return actions;
}
示例8: Run
public void Run (RefactoringContext context)
{
var foreachStatement = GetForeachStatement (context);
var result = context.Resolve (foreachStatement.InExpression);
var countProperty = GetCountProperty (result.Type);
var initializer = new VariableDeclarationStatement (new PrimitiveType ("int"), "i", new PrimitiveExpression (0));
var id1 = new IdentifierExpression ("i");
var id2 = id1.Clone ();
var id3 = id1.Clone ();
var forStatement = new ForStatement () {
Initializers = { initializer },
Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)),
Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) },
EmbeddedStatement = new BlockStatement {
new VariableDeclarationStatement (foreachStatement.VariableType.Clone (), foreachStatement.VariableName, new IndexerExpression (foreachStatement.InExpression.Clone (), id3))
}
};
if (foreachStatement.EmbeddedStatement is BlockStatement) {
foreach (var child in ((BlockStatement)foreachStatement.EmbeddedStatement).Statements) {
forStatement.EmbeddedStatement.AddChild (child.Clone (), BlockStatement.StatementRole);
}
} else {
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
}
using (var script = context.StartScript ()) {
script.Replace (foreachStatement, forStatement);
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
}
}
示例9: Run
public void Run (RefactoringContext context)
{
var switchStatement = GetSwitchStatement (context);
var result = context.Resolve (switchStatement.Expression);
var type = result.Type;
var newSwitch = (SwitchStatement)switchStatement.Clone ();
var target = new TypeReferenceExpression (context.CreateShortType (result.Type.Resolve (context.TypeResolveContext)));
foreach (var field in type.GetFields (context.TypeResolveContext)) {
if (field.IsSynthetic || !field.IsConst)
continue;
newSwitch.SwitchSections.Add (new SwitchSection () {
CaseLabels = {
new CaseLabel (new MemberReferenceExpression (target.Clone (), field.Name))
},
Statements = {
new BreakStatement ()
}
});
}
newSwitch.SwitchSections.Add (new SwitchSection () {
CaseLabels = {
new CaseLabel ()
},
Statements = {
new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System", "ArgumentOutOfRangeException")))
}
});
using (var script = context.StartScript ()) {
script.Replace (switchStatement, newSwitch);
}
}
示例10: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));
if (service == null)
yield break;
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
yield break;
var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(
context.TranslateString("Implement abstract members"),
script => script.InsertWithCursor(
context.TranslateString("Implement abstract members"),
state.CurrentTypeDefinition, (s, c) => ImplementInterfaceAction.GenerateImplementation(c, toImplement.Select(m => Tuple.Create(m, false)), true)
.Select(entity => {
var decl = entity as EntityDeclaration;
if (decl != null)
decl.Modifiers |= Modifiers.Override;
return entity;
}).ToList()), type);
}
示例11: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var expr = GetCreatePropertyOrFieldNode(context);
if (expr == null)
yield break;
if (!(expr is MemberReferenceExpression))
yield break;
var propertyName = CreatePropertyAction.GetPropertyName(expr);
if (propertyName == null)
yield break;
if (IsInvocationTarget(expr))
yield break;
var statement = expr.GetParent<Statement>();
if (statement == null)
yield break;
if (!(context.Resolve(expr).IsError))
yield break;
var guessedType = CreateFieldAction.GuessType(context, expr);
if (guessedType == null || guessedType.Kind != TypeKind.Enum)
yield break;
var state = context.GetResolverStateBefore(expr);
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
yield return new CodeAction(context.TranslateString("Create enum value"), script => {
var decl = new EnumMemberDeclaration {
Name = propertyName
};
script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition (), decl);
});
}
示例12: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface)
yield break;
var toImplement = ImplementInterfaceAction.CollectMembersToImplement(
state.CurrentTypeDefinition,
resolveResult.Type,
false
);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(context.TranslateString("Implement interface explicit"), script => {
script.InsertWithCursor(
context.TranslateString("Implement Interface"),
state.CurrentTypeDefinition,
ImplementInterfaceAction.GenerateImplementation (context, toImplement.Select (t => Tuple.Create (t.Item1, true)))
);
});
}
示例13: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));
if (service == null)
yield break;
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface)
yield break;
bool interfaceMissing;
var toImplement = ImplementInterfaceAction.CollectMembersToImplement(
state.CurrentTypeDefinition,
resolveResult.Type,
false,
out interfaceMissing
);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(context.TranslateString("Implement interface explicit"), script =>
script.InsertWithCursor(
context.TranslateString("Implement Interface"),
state.CurrentTypeDefinition,
(s, c) => ImplementInterfaceAction.GenerateImplementation (c, toImplement.Select (t => Tuple.Create (t.Item1, true)), interfaceMissing).ToList()
)
, type);
}
示例14: ActionFromUsingStatement
CodeAction ActionFromUsingStatement(RefactoringContext context)
{
var initializer = context.GetNode<VariableInitializer>();
if (initializer == null)
return null;
var initializerRR = context.Resolve(initializer) as LocalResolveResult;
if (initializerRR == null)
return null;
var elementType = GetElementType(initializerRR, context);
if (elementType == null)
return null;
var usingStatement = initializer.Parent.Parent as UsingStatement;
if (usingStatement == null)
return null;
return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
if (usingStatement.EmbeddedStatement is EmptyStatement) {
var blockStatement = new BlockStatement();
blockStatement.Statements.Add(iterator);
script.Replace(usingStatement.EmbeddedStatement, blockStatement);
script.FormatText(blockStatement);
} else if (usingStatement.EmbeddedStatement is BlockStatement) {
var anchorNode = usingStatement.EmbeddedStatement.FirstChild;
script.InsertAfter(anchorNode, iterator);
script.FormatText(usingStatement.EmbeddedStatement);
}
});
}
示例15: CreateFromExpression
CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
{
var resolveResult = context.Resolve(expression);
if (resolveResult.IsError)
return null;
return new CodeAction(context.TranslateString("Extract method"), script => {
string methodName = "NewMethod";
var method = new MethodDeclaration() {
ReturnType = context.CreateShortType(resolveResult.Type),
Name = methodName,
Body = new BlockStatement() {
new ReturnStatement(expression.Clone())
}
};
if (!StaticVisitor.UsesNotStaticMember(context, expression))
method.Modifiers |= Modifiers.Static;
var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);
Action<Task> replaceStatements = delegate {
var target = new IdentifierExpression(methodName);
script.Replace(expression, new InvocationExpression(target));
script.Link(target, method.NameToken);
};
if (task.IsCompleted) {
replaceStatements (null);
} else {
task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
}
});
}