本文整理汇总了C#中RefactoringContext.ResolveType方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.ResolveType方法的具体用法?C# RefactoringContext.ResolveType怎么用?C# RefactoringContext.ResolveType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.ResolveType方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAnonymousMethodExpression
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out ITypeDefinition delegateType)
{
delegateType = null;
var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
return null;
AstType resolvedType = null;
var parent = anonymousMethodExpression.Parent;
if (parent is AssignmentExpression) {
resolvedType = context.ResolveType (((AssignmentExpression)parent).Left);
} else if (parent is VariableDeclarationStatement) {
resolvedType = context.ResolveType (((VariableDeclarationStatement)parent).Type);
} else if (parent is InvocationExpression) {
// TODO: handle invocations
}
if (resolvedType == null)
return null;
delegateType = context.GetDefinition (resolvedType);
if (delegateType == null || delegateType.ClassType != ClassType.Delegate)
return null;
return anonymousMethodExpression;
}
示例2: Run
public void Run (RefactoringContext context)
{
var foreachStatement = GetForeachStatement (context);
var result = context.ResolveType (foreachStatement.InExpression);
var countProperty = GetCountProperty (result);
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);
}
}
示例3: IsValid
public bool IsValid (RefactoringContext context)
{
var identifier = CreateField.GetIdentifier (context);
if (identifier == null)
return false;
return context.ResolveType (identifier) == null && CreateField.GuessType (context, identifier) != null;
}
示例4: Run
public void Run (RefactoringContext context)
{
var varDecl = GetVariableDeclarationStatement (context);
using (var script = context.StartScript ()) {
script.Replace (varDecl.Type, context.ResolveType (varDecl.Variables.First ().Initializer));
}
}
示例5: GuessType
internal static AstType GuessType (RefactoringContext context, IdentifierExpression identifier)
{
if (identifier.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)identifier.Parent;
var other = assign.Left == identifier ? assign.Right : assign.Left;
return context.ResolveType (other);
}
return null;
}
示例6: GetVariableDeclarationStatement
static VariableDeclarationStatement GetVariableDeclarationStatement (RefactoringContext context)
{
var result = context.GetNode<VariableDeclarationStatement> ();
if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Type.Contains (context.Location.Line, context.Location.Column) && result.Type.IsMatch (new SimpleType ("var"))) {
if (context.ResolveType (result.Variables.First ().Initializer) == null)
return null;
return result;
}
return null;
}
示例7: IsValid
public bool IsValid (RefactoringContext context)
{
var switchStatement = GetSwitchStatement (context);
if (switchStatement == null)
return false;
var result = context.ResolveType (switchStatement.Expression);
if (result == null)
return false;
var type = context.GetDefinition (result);
return type != null && type.ClassType == ClassType.Enum;
}
示例8: IsValid
public bool IsValid (RefactoringContext context)
{
if (GetUnresolvedArguments (context).Count > 0)
return true;
var identifier = CreateField.GetIdentifier (context);
if (identifier == null)
return false;
if (context.GetNode<Statement> () == null)
return false;
return context.ResolveType (identifier) == null && GuessType (context, identifier) != null;
}
示例9: GetVariableDeclarationStatement
static VariableDeclarationStatement GetVariableDeclarationStatement (RefactoringContext context, out AstType resolvedType)
{
var result = context.GetNode<VariableDeclarationStatement> ();
if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Variables.First ().NameToken.Contains (context.Location.Line, context.Location.Column)) {
resolvedType = context.ResolveType (result.Variables.First ().Initializer);
if (resolvedType == null)
return null;
return result;
}
resolvedType = null;
return null;
}
示例10: Run
public void Run (RefactoringContext context)
{
VariableInitializer initializer;
var eventDeclaration = GetEventDeclaration (context, out initializer);
var type = context.GetDefinition (context.ResolveType (eventDeclaration.ReturnType));
if (type == null)
return;
var invokeMethod = type.Methods.Where (m => m.Name == "Invoke").FirstOrDefault ();
if (invokeMethod == null)
return;
bool hasSenderParam = false;
IEnumerable<IParameter> pars = invokeMethod.Parameters;
if (invokeMethod.Parameters.Any ()) {
var first = invokeMethod.Parameters [0];
if (first.Name == "sender" /*&& first.Type == "System.Object"*/) {
hasSenderParam = true;
pars = invokeMethod.Parameters.Skip (1);
}
}
const string handlerName = "handler";
var arguments = new List<Expression> ();
if (hasSenderParam)
arguments.Add (new ThisReferenceExpression ());
foreach (var par in pars)
arguments.Add (new IdentifierExpression (par.Name));
var methodDeclaration = new MethodDeclaration () {
Name = "On" + initializer.Name,
ReturnType = context.CreateShortType (eventDeclaration.ReturnType),
Modifiers = ICSharpCode.NRefactory.CSharp.Modifiers.Protected | ICSharpCode.NRefactory.CSharp.Modifiers.Virtual,
Body = new BlockStatement () {
new VariableDeclarationStatement (context.CreateShortType (eventDeclaration.ReturnType), handlerName, new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name)),
new IfElseStatement () {
Condition = new BinaryOperatorExpression (new IdentifierExpression (handlerName), BinaryOperatorType.InEquality, new PrimitiveExpression (null)),
TrueStatement = new ExpressionStatement (new InvocationExpression (new IdentifierExpression (handlerName), arguments))
}
}
};
foreach (var par in pars) {
var typeName = context.CreateShortType (par.Type.Resolve (context.TypeResolveContext));
var decl = new ParameterDeclaration (typeName, par.Name);
methodDeclaration.Parameters.Add (decl);
}
using (var script = context.StartScript ()) {
script.InsertWithCursor ("Create event invocator", methodDeclaration, Script.InsertPosition.After);
}
}
示例11: GetActions
public System.Collections.Generic.IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var catchClause = context.GetNode<CatchClause>();
if (catchClause == null)
yield break;
if (catchClause.Type.IsNull)
yield break;
var exceptionType = context.ResolveType(catchClause.Type);
if (exceptionType != context.Compilation.FindType(typeof(Exception)))
yield break;
var syntaxTree = context.RootNode as SyntaxTree;
if (syntaxTree == null)
yield break;
var exceptionIdentifierRR = context.Resolve(catchClause.VariableNameToken) as LocalResolveResult;
if (exceptionIdentifierRR != null &&
IsReferenced(exceptionIdentifierRR.Variable, catchClause.Body, syntaxTree, context))
yield break;
yield return new CodeAction(context.TranslateString("Remove type specifier"), script => {
script.Replace(catchClause, new CatchClause() {
Body = catchClause.Body.Clone() as BlockStatement
});
}, catchClause.Type);
}
示例12: Run
public void Run (RefactoringContext context)
{
var switchStatement = GetSwitchStatement (context);
var result = context.ResolveType (switchStatement.Expression);
var type = context.GetDefinition (result);
var newSwitch = (SwitchStatement)switchStatement.Clone ();
var target = new TypeReferenceExpression (context.CreateShortType (result));
foreach (var field in type.Fields) {
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);
}
}
示例13: GetUnresolvedArguments
public List<IdentifierExpression> GetUnresolvedArguments (RefactoringContext context)
{
var expressions = new List<IdentifierExpression> ();
var invocation = GetInvocation (context);
if (invocation != null) {
foreach (var arg in invocation.Arguments) {
IdentifierExpression identifier;
if (arg is DirectionExpression) {
identifier = ((DirectionExpression)arg).Expression as IdentifierExpression;
} else if (arg is NamedArgumentExpression) {
identifier = ((NamedArgumentExpression)arg).Expression as IdentifierExpression;
} else {
identifier = arg as IdentifierExpression;
}
if (identifier == null)
continue;
if (context.ResolveType (identifier) == null && GuessType (context, identifier) != null)
expressions.Insert (0, identifier);
}
}
return expressions;
}
示例14: GetForeachStatement
static ForeachStatement GetForeachStatement (RefactoringContext context)
{
var astNode = context.GetNode ();
if (astNode == null)
return null;
var result = (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement;
if (result == null || context.ResolveType (result.InExpression) == null)
return null;
return result;
}