本文整理汇总了C#中VariableInitializer类的典型用法代码示例。如果您正苦于以下问题:C# VariableInitializer类的具体用法?C# VariableInitializer怎么用?C# VariableInitializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
VariableInitializer类属于命名空间,在下文中一共展示了VariableInitializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitVariableInitializer
public override void VisitVariableInitializer (VariableInitializer variableInitializer)
{
base.VisitVariableInitializer (variableInitializer);
var resolveResult = ctx.Resolve (variableInitializer) as LocalResolveResult;
CollectIssues (variableInitializer, variableInitializer.GetParent<BlockStatement> (), resolveResult);
}
示例2: EmitEventAccessor
protected void EmitEventAccessor(EventDeclaration e, VariableInitializer evtVar, bool add)
{
string name = evtVar.Name;
this.Write(add ? "add" : "remove", name, " : ");
this.WriteFunction();
this.WriteOpenParentheses();
this.Write("value");
this.WriteCloseParentheses();
this.WriteSpace();
this.BeginBlock();
this.WriteThis();
this.WriteDot();
this.Write(this.Emitter.GetEntityName(e));
this.Write(" = ");
this.Write(Bridge.Translator.Emitter.ROOT, ".", add ? Bridge.Translator.Emitter.DELEGATE_COMBINE : Bridge.Translator.Emitter.DELEGATE_REMOVE);
this.WriteOpenParentheses();
this.WriteThis();
this.WriteDot();
this.Write(this.Emitter.GetEntityName(e));
this.WriteComma();
this.WriteSpace();
this.Write("value");
this.WriteCloseParentheses();
this.WriteSemiColon();
this.WriteNewLine();
this.EndBlock();
}
示例3: Run
public void Run(AstNode node)
{
Run(node, null);
// Declare all the variables at the end, after all the logic has run.
// This is done so that definite assignment analysis can work on a single representation and doesn't have to be updated
// when we change the AST.
foreach (var v in variablesToDeclare) {
if (v.ReplacedAssignment == null) {
BlockStatement block = (BlockStatement)v.InsertionPoint.Parent;
block.Statements.InsertBefore(
v.InsertionPoint,
new VariableDeclarationStatement((AstType)v.Type.Clone(), v.Name));
}
}
// First do all the insertions, then do all the replacements. This is necessary because a replacement might remove our reference point from the AST.
foreach (var v in variablesToDeclare) {
if (v.ReplacedAssignment != null) {
// We clone the right expression so that it doesn't get removed from the old ExpressionStatement,
// which might be still in use by the definite assignment graph.
VariableInitializer initializer = new VariableInitializer(v.Name, v.ReplacedAssignment.Right.Detach()).CopyAnnotationsFrom(v.ReplacedAssignment).WithAnnotation(v.ILVariable);
VariableDeclarationStatement varDecl = new VariableDeclarationStatement {
Type = (AstType)v.Type.Clone(),
Variables = { initializer }
};
ExpressionStatement es = v.ReplacedAssignment.Parent as ExpressionStatement;
if (es != null) {
// Note: if this crashes with 'Cannot replace the root node', check whether two variables were assigned the same name
es.ReplaceWith(varDecl.CopyAnnotationsFrom(es));
} else {
v.ReplacedAssignment.ReplaceWith(varDecl);
}
}
}
variablesToDeclare = null;
}
示例4: Run
public void Run (RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration> ();
string backingStoreName = context.GetNameProposal (property.Name);
// create field
var backingStore = new FieldDeclaration ();
backingStore.ReturnType = property.ReturnType.Clone ();
var initializer = new VariableInitializer (backingStoreName);
backingStore.Variables.Add (initializer);
// create new property & implement the get/set bodies
var newProperty = (PropertyDeclaration)property.Clone ();
var id1 = new IdentifierExpression (backingStoreName);
var id2 = new IdentifierExpression (backingStoreName);
newProperty.Getter.Body = new BlockStatement () {
new ReturnStatement (id1)
};
newProperty.Setter.Body = new BlockStatement () {
new ExpressionStatement (new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value")))
};
using (var script = context.StartScript ()) {
script.Replace (property, newProperty);
script.InsertBefore (property, backingStore);
script.Link (initializer, id1, id2);
}
}
示例5: VisitVariableInitializer
public override void VisitVariableInitializer(VariableInitializer variableInitializer)
{
base.VisitVariableInitializer (variableInitializer);
// check if variable is assigned
if (!variableInitializer.Initializer.IsNull)
return;
var decl = variableInitializer.Parent as VariableDeclarationStatement;
if (decl == null)
return;
var resolveResult = ctx.Resolve (variableInitializer) as LocalResolveResult;
if (resolveResult == null)
return;
if (IsUsed (decl.Parent, resolveResult.Variable, variableInitializer))
return;
AddIssue (variableInitializer.NameToken, ctx.TranslateString ("Remove unused local variable"),
script =>
{
if (decl.Variables.Count == 1) {
script.Remove (decl);
} else {
var newDeclaration = (VariableDeclarationStatement)decl.Clone ();
newDeclaration.Variables.Remove (
newDeclaration.Variables.FirstOrNullObject (v => v.Name == variableInitializer.Name));
script.Replace (decl, newDeclaration);
}
});
}
示例6: VisitVariableInitializer
public override void VisitVariableInitializer (VariableInitializer variableInitializer)
{
base.VisitVariableInitializer (variableInitializer);
if (!(ctx.Resolve (variableInitializer) is LocalResolveResult))
return;
if (HidesMember (ctx, variableInitializer, variableInitializer.Name))
AddIssue (variableInitializer.NameToken,
ctx.TranslateString ("Local variable has the same name as a member and hides it"));
}
示例7: ContainsGetter
bool ContainsGetter (PropertyDeclaration property, VariableInitializer initializer)
{
if (property.Getter.IsNull || property.Getter.Body.Statements.Count () != 1)
return false;
var ret = property.Getter.Body.Statements.Single () as ReturnStatement;
if (ret == null)
return false;
return ret.Expression.IsMatch (new IdentifierExpression (initializer.Name)) ||
ret.Expression.IsMatch (new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name));
}
示例8: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration> ();
if (property == null || !property.NameToken.Contains(context.Location))
yield break;
if (!IsNotImplemented (context, property.Getter.Body) ||
!IsNotImplemented (context, property.Setter.Body)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Implement property"), script => {
string backingStoreName = context.GetNameProposal (property.Name);
// create field
var backingStore = new FieldDeclaration ();
if (property.Modifiers.HasFlag (Modifiers.Static))
backingStore.Modifiers |= Modifiers.Static;
if (property.Setter.IsNull)
backingStore.Modifiers |= Modifiers.Readonly;
backingStore.ReturnType = property.ReturnType.Clone ();
var initializer = new VariableInitializer (backingStoreName);
backingStore.Variables.Add (initializer);
// create new property & implement the get/set bodies
var newProperty = (PropertyDeclaration)property.Clone ();
Expression id1;
if (backingStoreName == "value")
id1 = new ThisReferenceExpression().Member("value");
else
id1 = new IdentifierExpression (backingStoreName);
Expression id2 = id1.Clone();
newProperty.Getter.Body = new BlockStatement () {
new ReturnStatement (id1)
};
if (!property.Setter.IsNull) {
newProperty.Setter.Body = new BlockStatement () {
new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value"))
};
}
script.Replace (property, newProperty);
script.InsertBefore (property, backingStore);
if (!property.Setter.IsNull)
script.Link (initializer, id1, id2);
else
script.Link (initializer, id1);
}, property.NameToken);
}
示例9: CreateEventInvocator
public static MethodDeclaration CreateEventInvocator (RefactoringContext context, TypeDeclaration declaringType, EventDeclaration eventDeclaration, VariableInitializer initializer, IMethod invokeMethod, bool useExplictType)
{
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(eventDeclaration.HasModifier (Modifiers.Static) ? (Expression)new PrimitiveExpression (null) : new ThisReferenceExpression());
bool useThisMemberReference = false;
foreach (var par in pars) {
arguments.Add(new IdentifierExpression(par.Name));
useThisMemberReference |= par.Name == initializer.Name;
}
var proposedHandlerName = GetNameProposal(initializer);
var modifiers = eventDeclaration.HasModifier(Modifiers.Static) ? Modifiers.Static : Modifiers.Protected | Modifiers.Virtual;
if (declaringType.HasModifier (Modifiers.Sealed)) {
modifiers = Modifiers.None;
}
var methodDeclaration = new MethodDeclaration {
Name = proposedHandlerName,
ReturnType = new PrimitiveType ("void"),
Modifiers = modifiers,
Body = new BlockStatement {
new VariableDeclarationStatement (
useExplictType ? eventDeclaration.ReturnType.Clone () : new PrimitiveType ("var"), handlerName,
useThisMemberReference ?
(Expression)new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name)
: new IdentifierExpression (initializer.Name)
),
new IfElseStatement {
Condition = new BinaryOperatorExpression (new IdentifierExpression (handlerName), BinaryOperatorType.InEquality, new PrimitiveExpression (null)),
TrueStatement = new InvocationExpression (new IdentifierExpression (handlerName), arguments)
}
}
};
foreach (var par in pars) {
var typeName = context.CreateShortType(par.Type);
var decl = new ParameterDeclaration(typeName, par.Name);
methodDeclaration.Parameters.Add(decl);
}
return methodDeclaration;
}
示例10: 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);
});
}
示例11: ConvertToInitializer
public VariableInitializer ConvertToInitializer(VariableInitializer variableInitializer, ref IList<AstNode> statements)
{
if (variableInitializer == null)
throw new ArgumentNullException("variableInitializer");
if (statements == null)
throw new ArgumentNullException("statements");
Initialize(variableInitializer);
accessPaths [mainAccessPath] = variableInitializer.Initializer.Clone();
Convert(statements);
statements = ReplacementNodeHelper.GetReplacedNodes(accessPaths [mainAccessPath]);
return new VariableInitializer(mainAccessPath.RootName, accessPaths [mainAccessPath]);
}
示例12: MoveDeclarationAction
static CodeAction MoveDeclarationAction(RefactoringContext context, AstNode insertAnchor,
VariableDeclarationStatement declarationStatement, VariableInitializer initializer)
{
var type = declarationStatement.Type.Clone();
var name = initializer.Name;
return new CodeAction(context.TranslateString("Move declaration to outer scope"), script => {
script.InsertBefore(declarationStatement, new ExpressionStatement() {
Expression = new AssignmentExpression(new IdentifierExpression(name), initializer.Initializer.Clone())
});
script.Remove(declarationStatement);
script.InsertBefore(insertAnchor, new VariableDeclarationStatement(type, name, Expression.Null));
});
}
示例13: MoveInitializerAction
static CodeAction MoveInitializerAction(RefactoringContext context, AstNode insertAnchor,
VariableDeclarationStatement declaration, VariableInitializer initializer)
{
var type = declaration.Type.Clone();
var name = initializer.Name;
return new CodeAction(context.TranslateString("Move initializer to outer scope"), script => {
if (declaration.Variables.Count != 1) {
var innerDeclaration = RemoveInitializer(declaration, initializer);
script.InsertBefore(declaration, innerDeclaration);
}
script.Remove(declaration);
var outerDeclaration = new VariableDeclarationStatement(type, name, initializer.Initializer.Clone());
script.InsertBefore(insertAnchor, outerDeclaration);
});
}
示例14: HandleInitializer
CodeAction HandleInitializer(RefactoringContext context, VariableInitializer initializer)
{
var objectCreateExpression = initializer.Initializer as ObjectCreateExpression;
if (objectCreateExpression == null)
return null;
var initializerRR = context.Resolve(initializer) as LocalResolveResult;
if (initializerRR == null)
return null;
IList<AstNode> statements = GetNodes(context.GetNode<Statement>());
var converter = new StatementsToInitializerConverter(context);
var newInitializer = converter.ConvertToInitializer(initializer, ref statements);
if (statements.Count == 0)
return null;
return MakeAction(context, initializer, newInitializer, statements);
}
示例15: VisitVariableInitializer
public override void VisitVariableInitializer (VariableInitializer variableInitializer)
{
base.VisitVariableInitializer (variableInitializer);
var decl = variableInitializer.Parent as VariableDeclarationStatement;
if (decl == null)
return;
var resolveResult = ctx.Resolve (variableInitializer) as LocalResolveResult;
if (resolveResult == null)
return;
if (!TestOnlyAssigned (ctx, unit, resolveResult.Variable))
return;
AddIssue (variableInitializer.NameToken,
ctx.TranslateString ("Local variable is assigned by its value is never used"));
}