本文整理汇总了C#中RefactoringContext.CreateShortType方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.CreateShortType方法的具体用法?C# RefactoringContext.CreateShortType怎么用?C# RefactoringContext.CreateShortType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.CreateShortType方法的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: 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);
}
}
示例3: AddBaseTypesAccordingToNamingRules
static TypeDeclaration AddBaseTypesAccordingToNamingRules(RefactoringContext context, NamingConventionService service, TypeDeclaration result)
{
if (service.HasValidRule(result.Name, AffectedEntity.CustomAttributes, Modifiers.Public)) {
result.BaseTypes.Add(context.CreateShortType("System", "Attribute"));
} else if (service.HasValidRule(result.Name, AffectedEntity.CustomEventArgs, Modifiers.Public)) {
result.BaseTypes.Add(context.CreateShortType("System", "EventArgs"));
} else if (service.HasValidRule(result.Name, AffectedEntity.CustomExceptions, Modifiers.Public)) {
result.BaseTypes.Add(context.CreateShortType("System", "Exception"));
}
return result;
}
示例4: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var pexpr = context.GetNode<PrimitiveExpression>();
if (pexpr == null)
yield break;
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
var resolveResult = context.Resolve(pexpr);
yield return new CodeAction(context.TranslateString("Create local constant"), script => {
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null)
name = service.CheckName(context, name, AffectedEntity.LocalConstant);
var initializer = new VariableInitializer(name, pexpr.Clone());
var decl = new VariableDeclarationStatement() {
Type = context.CreateShortType(resolveResult.Type),
Modifiers = Modifiers.Const,
Variables = { initializer }
};
script.InsertBefore(statement, decl);
var variableUsage = new IdentifierExpression(name);
script.Replace(pexpr, variableUsage);
script.Link(initializer.NameToken, variableUsage);
});
yield return new CodeAction(context.TranslateString("Create constant field"), script => {
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null)
name = service.CheckName(context, name, AffectedEntity.ConstantField);
var initializer = new VariableInitializer(name, pexpr.Clone());
var decl = new FieldDeclaration() {
ReturnType = context.CreateShortType(resolveResult.Type),
Modifiers = Modifiers.Const,
Variables = { initializer }
};
var variableUsage = new IdentifierExpression(name);
script.Replace(pexpr, variableUsage);
// script.Link(initializer.NameToken, variableUsage);
script.InsertWithCursor(context.TranslateString("Create constant"), Script.InsertPosition.Before, decl);
});
}
示例5: 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);
}
}
示例6: 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));
}
}
}
示例7: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var createExpression = context.GetNode<Expression>() as ObjectCreateExpression;
if (createExpression == null)
yield break;
var resolveResult = context.Resolve(createExpression) as CSharpInvocationResolveResult;
if (resolveResult == null || !resolveResult.IsError || resolveResult.Member.DeclaringTypeDefinition == null || resolveResult.Member.DeclaringTypeDefinition.IsSealed || resolveResult.Member.DeclaringTypeDefinition.Region.IsEmpty)
yield break;
yield return new CodeAction(context.TranslateString("Create constructor"), script => {
var decl = new ConstructorDeclaration() {
Name = resolveResult.Member.DeclaringTypeDefinition.Name,
Modifiers = Modifiers.Public,
Body = new BlockStatement() {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
script.InsertWithCursor(
context.TranslateString("Create constructor"),
resolveResult.Member.DeclaringTypeDefinition,
decl
);
}, createExpression);
}
示例8: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration>();
if (property == null || !property.NameToken.Contains(context.Location))
yield break;
var field = RemoveBackingStoreAction.GetBackingField(context);
if (field == null)
yield break;
var resolvedType = ReflectionHelper.ParseReflectionName ("System.EventHandler").Resolve (context.Compilation);
if (resolvedType == null)
yield break;
var type = (TypeDeclaration)property.Parent;
yield return new CodeAction(context.TranslateString("Create changed event"), script => {
var eventDeclaration = CreateChangedEventDeclaration (context, property);
var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator (context, type, eventDeclaration, eventDeclaration.Variables.First (), resolvedType.GetDelegateInvokeMethod (), false);
var stmt = new ExpressionStatement (new InvocationExpression (
new IdentifierExpression (methodDeclaration.Name),
new MemberReferenceExpression (context.CreateShortType("System", "EventArgs"), "Empty")
));
script.InsertWithCursor(
context.TranslateString("Create event invocator"),
Script.InsertPosition.After,
new AstNode[] { eventDeclaration, methodDeclaration }
).ContinueScript(delegate {
script.InsertBefore (property.Setter.Body.RBraceToken, stmt);
script.FormatText (stmt);
});
}, property.NameToken);
}
示例9: 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 ());
}
});
}
示例10: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
IType type;
var anonymousMethodExpression = GetAnonymousMethodExpression(context, out type);
if (anonymousMethodExpression == null) {
yield break;
}
yield return new CodeAction (context.TranslateString("Insert anonymous method signature"), script => {
var delegateMethod = type.GetDelegateInvokeMethod();
var sb = new StringBuilder ("(");
for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
if (k > 0) {
sb.Append(", ");
}
var paramType = delegateMethod.Parameters [k].Type;
sb.Append(context.CreateShortType(paramType));
sb.Append(" ");
sb.Append(delegateMethod.Parameters [k].Name);
}
sb.Append(")");
script.InsertText(context.GetOffset(anonymousMethodExpression.DelegateToken.EndLocation), sb.ToString());
}, anonymousMethodExpression);
}
示例11: AddImplementation
static void AddImplementation(RefactoringContext context, TypeDeclaration result, ICSharpCode.NRefactory.TypeSystem.IType guessedType)
{
foreach (var property in guessedType.GetProperties ()) {
if (!property.IsAbstract)
continue;
if (property.IsIndexer) {
var indexerDecl = new IndexerDeclaration() {
ReturnType = context.CreateShortType(property.ReturnType),
Modifiers = GetModifiers(property),
Name = property.Name
};
indexerDecl.Parameters.AddRange(ConvertParameters(context, property.Parameters));
if (property.CanGet)
indexerDecl.Getter = new Accessor();
if (property.CanSet)
indexerDecl.Setter = new Accessor();
result.AddChild(indexerDecl, Roles.TypeMemberRole);
continue;
}
var propDecl = new PropertyDeclaration() {
ReturnType = context.CreateShortType(property.ReturnType),
Modifiers = GetModifiers (property),
Name = property.Name
};
if (property.CanGet)
propDecl.Getter = new Accessor();
if (property.CanSet)
propDecl.Setter = new Accessor();
result.AddChild(propDecl, Roles.TypeMemberRole);
}
foreach (var method in guessedType.GetMethods ()) {
if (!method.IsAbstract)
continue;
var decl = new MethodDeclaration() {
ReturnType = context.CreateShortType(method.ReturnType),
Modifiers = GetModifiers (method),
Name = method.Name,
Body = new BlockStatement() {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
decl.Parameters.AddRange(ConvertParameters(context, method.Parameters));
result.AddChild(decl, Roles.TypeMemberRole);
}
foreach (var evt in guessedType.GetEvents ()) {
if (!evt.IsAbstract)
continue;
var decl = new EventDeclaration() {
ReturnType = context.CreateShortType(evt.ReturnType),
Modifiers = GetModifiers (evt),
Name = evt.Name
};
result.AddChild(decl, Roles.TypeMemberRole);
}
}
示例12: 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);
}
示例13: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var switchStatement = GetSwitchStatement(context);
if (switchStatement == null) {
yield break;
}
var result = context.Resolve(switchStatement.Expression);
if (result.Type.Kind != TypeKind.Enum) {
yield break;
}
yield return new CodeAction (context.TranslateString("Create switch labels"), script => {
var type = result.Type;
var newSwitch = (SwitchStatement)switchStatement.Clone();
var target = new TypeReferenceExpression (context.CreateShortType(result.Type));
foreach (var field in type.GetFields ()) {
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")))
}
});
script.Replace(switchStatement, newSwitch);
}, switchStatement);
}
示例14: CreateChangedEventDeclaration
EventDeclaration CreateChangedEventDeclaration (RefactoringContext context, PropertyDeclaration propertyDeclaration)
{
return new EventDeclaration {
Modifiers = propertyDeclaration.HasModifier (Modifiers.Static) ? Modifiers.Public | Modifiers.Static : Modifiers.Public,
ReturnType = context.CreateShortType("System", "EventHandler"),
Variables = {
new VariableInitializer {
Name = propertyDeclaration.Name + "Changed"
}
}
};
}
示例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 usedVariables = VariableLookupVisitor.Analyze(context, expression);
var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables);
usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin));
var target = new IdentifierExpression(methodName);
var invocation = new InvocationExpression(target);
foreach (var variable in usedVariables) {
Expression argumentExpression = new IdentifierExpression(variable.Name);
var mod = ParameterModifier.None;
if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) {
mod = ParameterModifier.Ref;
argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression);
}
method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod));
invocation.Arguments.Add(argumentExpression);
}
var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);
Action<Task> replaceStatements = delegate {
script.Replace(expression, invocation);
script.Link(target, method.NameToken);
};
if (task.IsCompleted) {
replaceStatements (null);
} else {
task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
}
}, expression);
}