本文整理汇总了C#中VariableDeclaratorSyntax类的典型用法代码示例。如果您正苦于以下问题:C# VariableDeclaratorSyntax类的具体用法?C# VariableDeclaratorSyntax怎么用?C# VariableDeclaratorSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
VariableDeclaratorSyntax类属于命名空间,在下文中一共展示了VariableDeclaratorSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SourceMemberFieldSymbol
internal SourceMemberFieldSymbol(
SourceMemberContainerTypeSymbol containingType,
VariableDeclaratorSyntax declarator,
DeclarationModifiers modifiers,
bool modifierErrors,
DiagnosticBag diagnostics)
: base(containingType, declarator.Identifier.ValueText, declarator.GetReference(), declarator.Identifier.GetLocation())
{
this.modifiers = modifiers;
this.CheckAccessibility(diagnostics);
var location = Location;
if (modifierErrors)
{
// skip the following checks
}
else if (containingType.IsSealed && (DeclaredAccessibility == Accessibility.Protected || DeclaredAccessibility == Accessibility.ProtectedOrInternal))
{
diagnostics.Add(AccessCheck.GetProtectedMemberInSealedTypeError(containingType), location, this);
}
else if (IsVolatile && IsReadOnly)
{
diagnostics.Add(ErrorCode.ERR_VolatileAndReadonly, location, this);
}
else if (containingType.IsStatic && !IsStatic)
{
diagnostics.Add(ErrorCode.ERR_InstanceMemberInStaticClass, location, this);
}
// TODO: Consider checking presence of core type System.Runtime.CompilerServices.IsVolatile
// if there is a volatile modifier. Perhaps an appropriate error should be reported if the
// type isn’t available.
}
示例2: AddDisposeDeclarationToDisposeMethod
private static TypeDeclarationSyntax AddDisposeDeclarationToDisposeMethod(VariableDeclaratorSyntax variableDeclarator, TypeDeclarationSyntax type, INamedTypeSymbol typeSymbol)
{
var disposableMethod = typeSymbol.GetMembers("Dispose").OfType<IMethodSymbol>().FirstOrDefault(d => d.Arity == 0);
var disposeStatement = SyntaxFactory.ParseStatement($"{variableDeclarator.Identifier.ToString()}.Dispose();");
TypeDeclarationSyntax newType;
if (disposableMethod == null)
{
var disposeMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "Dispose")
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)))
.WithBody(SyntaxFactory.Block(disposeStatement))
.WithAdditionalAnnotations(Formatter.Annotation);
newType = ((dynamic)type).AddMembers(disposeMethod);
}
else
{
var existingDisposeMethod = (MethodDeclarationSyntax)disposableMethod.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax();
if (type.Members.Contains(existingDisposeMethod))
{
var newDisposeMethod = existingDisposeMethod.AddBodyStatements(disposeStatement)
.WithAdditionalAnnotations(Formatter.Annotation);
newType = type.ReplaceNode(existingDisposeMethod, newDisposeMethod);
}
else
{
//we will simply anotate the code for now, but ideally we would change another document
//for this to work we have to be able to fix more than one doc
var fieldDeclaration = variableDeclarator.Parent.Parent;
var newFieldDeclaration = fieldDeclaration.WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia($"//add {disposeStatement.ToString()} to the Dispose method on another file.").AddRange(fieldDeclaration.GetTrailingTrivia()))
.WithLeadingTrivia(fieldDeclaration.GetLeadingTrivia());
newType = type.ReplaceNode(fieldDeclaration, newFieldDeclaration);
}
}
return newType;
}
示例3: VisitVariableDeclarator
/// <summary>
/// Normalizes the <paramref name="declaration" />.
/// </summary>
public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax declaration)
{
var objectCreation = declaration?.Initializer?.Value as ObjectCreationExpressionSyntax;
if (objectCreation == null)
return declaration;
var symbol = SemanticModel.GetDeclaredSymbol(declaration);
if (symbol == null)
return declaration;
ITypeSymbol type;
string name;
switch (symbol.Kind)
{
case SymbolKind.Field:
var fieldSymbol = ((IFieldSymbol)symbol);
type = fieldSymbol.Type;
name = fieldSymbol.Name;
break;
case SymbolKind.Local:
var localSymbol = ((ILocalSymbol)symbol);
type = localSymbol.Type;
name = localSymbol.Name;
break;
default:
return declaration;
}
var fault = SemanticModel.GetTypeSymbol<Fault>();
if (!type.Equals(fault) && !type.IsDerivedFrom(fault))
return declaration;
return declaration.WithInitializer(declaration.Initializer.WithValue(AddNameInitializer(fault, objectCreation, name)));
}
示例4: VisitVariableDeclarator
public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
if (node.Identifier.ToString() == renameFrom.ToString())
node = node.WithIdentifier(renameTo);
return base.VisitVariableDeclarator(node);
}
示例5: UseDeclarationExpression
private async Task<Document> UseDeclarationExpression(Document document, ArgumentSyntax argument, VariableDeclaratorSyntax declarator,
CancellationToken cancellationToken)
{
// get variable declaration
var declaration = declarator.Parent;
// get statement which contains both local declaration statement and method call with out argument
var statement = DiagnosticAnalyzer.GetContainingStatement(declaration.Parent);
// remove entire local declaration statement or just single variable declaration
// depending on how many variables are declared within single local declaration statement
var nodeToRemove = declaration.ChildNodes().OfType<VariableDeclaratorSyntax>().Count() > 1 ? declarator : declaration.Parent;
var newStatement = statement.RemoveNode(nodeToRemove, SyntaxRemoveOptions.KeepEndOfLine);
// get variable type
var type = declaration.ChildNodes().First() as TypeSyntax;
// create new Declaration Expression using variable type and declarator
var newDeclarationExpression = SyntaxFactory.DeclarationExpression(type, declarator);
// fix the trivia aroung Declaration Expression
var firstToken = newDeclarationExpression.GetFirstToken();
var leadingTrivia = firstToken.LeadingTrivia;
var trimmedDeclarationExpression = newDeclarationExpression.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty));
// get ArgumentSyntax from newStatement which is equivalent to argument from original syntax tree
var newArgument = newStatement.DescendantNodes()
.FirstOrDefault(n => n.IsEquivalentTo(argument));
// replace argument with new version, containing Declaration Expression
newStatement = newStatement.ReplaceNode(newArgument.ChildNodes().First(), trimmedDeclarationExpression);
// get root for current document and replace statement with new version
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(statement, newStatement);
// return document with modified syntax
return document.WithSyntaxRoot(newRoot);
}
示例6: FieldMemberBuilder
internal FieldMemberBuilder(NamedTypeSymbol owner, Binder enclosing, FieldDeclarationSyntax declaration, TypeSymbol type, VariableDeclaratorSyntax declarator)
: base(enclosing.Location(declarator) as SourceLocation, owner, enclosing)
{
this.owner = owner;
this.declaration = declaration;
this.declarator = declarator;
this.Type = type;
}
示例7: CompileDefaultInitialization
protected override void CompileDefaultInitialization(VariableDeclaratorSyntax declarator, TypeSyntax type)
{
Write("=");
Write(Space);
Write("(");
CompileExpression(type);
Write(")0");
}
示例8: VisitVariableDeclarator
public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
var substitution = result.Find(node);
return substitution != null
? substitution.Substitute()
: base.VisitVariableDeclarator(node);
}
示例9: FlowsIntoTarget
/// <summary>
/// Returns true if the given expression flows in the target.
/// </summary>
/// <param name="variable">Variable</param>
/// <param name="target">Target</param>
/// <param name="syntaxNode">SyntaxNode</param>
/// <param name="cfgNode">ControlFlowGraphNode</param>
/// <param name="targetSyntaxNode">Target syntaxNode</param>
/// <param name="targetCfgNode">Target controlFlowGraphNode</param>
/// <param name="model">SemanticModel</param>
/// <returns>Boolean</returns>
internal static bool FlowsIntoTarget(VariableDeclaratorSyntax variable, ISymbol target,
SyntaxNode syntaxNode, ControlFlowGraphNode cfgNode, SyntaxNode targetSyntaxNode,
ControlFlowGraphNode targetCfgNode, SemanticModel model)
{
ISymbol reference = model.GetDeclaredSymbol(variable);
return DataFlowAnalysis.FlowsIntoTarget(reference, target, syntaxNode,
cfgNode, targetSyntaxNode, targetCfgNode);
}
示例10: TypeChecks
private void TypeChecks(TypeSymbol type, BaseFieldDeclarationSyntax fieldSyntax, VariableDeclaratorSyntax declarator, DiagnosticBag diagnostics)
{
if (type.IsStatic)
{
// Cannot declare a variable of static type '{0}'
diagnostics.Add(ErrorCode.ERR_VarDeclIsStaticClass, this.Location, type);
}
else if (type.SpecialType == SpecialType.System_Void)
{
diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, fieldSyntax.Declaration.Type.Location);
}
else if (type.IsRestrictedType())
{
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, fieldSyntax.Declaration.Type.Location, type);
}
else if (IsConst && !type.CanBeConst())
{
SyntaxToken constToken = default(SyntaxToken);
foreach (var modifier in fieldSyntax.Modifiers)
{
if (modifier.CSharpKind() == SyntaxKind.ConstKeyword)
{
constToken = modifier;
break;
}
}
Debug.Assert(constToken.CSharpKind() == SyntaxKind.ConstKeyword);
diagnostics.Add(ErrorCode.ERR_BadConstType, constToken.GetLocation(), type);
}
else
{
if (ContainingType.TypeKind == TypeKind.Struct && !IsStatic && !IsConst)
{
var initializerOpt = declarator.Initializer;
if (initializerOpt != null)
{
// '{0}': cannot have instance field initializers in structs
diagnostics.Add(ErrorCode.ERR_FieldInitializerInStruct, this.Location, this);
}
}
if (IsVolatile && !type.IsValidVolatileFieldType())
{
// '{0}': a volatile field cannot be of the type '{1}'
diagnostics.Add(ErrorCode.ERR_VolatileStruct, this.Location, this, type);
}
}
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
if (!this.IsNoMoreVisibleThan(type, ref useSiteDiagnostics))
{
// Inconsistent accessibility: field type '{1}' is less accessible than field '{0}'
diagnostics.Add(ErrorCode.ERR_BadVisFieldType, this.Location, this, type);
}
diagnostics.Add(this.Location, useSiteDiagnostics);
}
示例11: AddSequencePoint
internal static BoundStatement AddSequencePoint(VariableDeclaratorSyntax declaratorSyntax, BoundStatement rewrittenStatement)
{
SyntaxNode node;
TextSpan? part;
GetBreakpointSpan(declaratorSyntax, out node, out part);
var result = BoundSequencePoint.Create(declaratorSyntax, part, rewrittenStatement);
result.WasCompilerGenerated = rewrittenStatement.WasCompilerGenerated;
return result;
}
示例12: GetDescription
public static string GetDescription(this VariableDeclarationSyntax declaration, VariableDeclaratorSyntax declarator)
{
var result = new StringBuilder();
result.Append(declaration.Type.ToStringIgnoringMacroReferences());
result.Append(" ");
result.Append(declarator.Identifier.GetFullyQualifiedName());
return result.ToString().Replace(Environment.NewLine, string.Empty);
}
示例13: GetSimpleProperty
private static PropertyDeclarationSyntax GetSimpleProperty(PropertyDeclarationSyntax property, VariableDeclaratorSyntax variableDeclarator)
{
var simpleGetSetPropetie = property.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] {
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
})));
return variableDeclarator.Initializer == null ?
simpleGetSetPropetie :
simpleGetSetPropetie.WithInitializer(variableDeclarator.Initializer)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
示例14: VisitVariableDeclarator
public override void VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
if (node.Ancestors().Any(x => x is FunctionDefinitionSyntax))
CreateTag(node.Identifier, _classificationService.LocalVariableIdentifier);
else if (node.Ancestors().Any(x => x is TypeDefinitionSyntax))
CreateTag(node.Identifier, _classificationService.FieldIdentifier);
else
CreateTag(node.Identifier, _classificationService.GlobalVariableIdentifier);
base.VisitVariableDeclarator(node);
}
示例15: MakeParameterAsync
private async Task<Document> MakeParameterAsync(Document document, VariableDeclaratorSyntax typeDecl, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
string varKeyword = string.Empty, varName = string.Empty;
//Predefined variable type (string, int, etc...)
var preDefType = typeDecl.Parent.ChildNodes().OfType<PredefinedTypeSyntax>().FirstOrDefault();
if (preDefType != null)
{
varKeyword = preDefType.Keyword.ToString();
var varDecl = typeDecl.Parent.ChildNodes().OfType<VariableDeclaratorSyntax>().FirstOrDefault();
varName = varDecl?.Identifier.ToString();
}
else //var
{
var identName = typeDecl.Parent.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
//Access the semantic model to determine actual type
var model = document.GetSemanticModelAsync().Result;
var type = model.GetTypeInfo(identName).Type;
varKeyword = type.ToMinimalDisplayString(model, typeDecl.SpanStart);
var varDecl = typeDecl.Parent.ChildNodes().OfType<VariableDeclaratorSyntax>().FirstOrDefault();
varName = varDecl?.Identifier.ToString();
}
MethodDeclarationSyntax mds = typeDecl.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault();
//Add the existing and new parameters
ParameterSyntax ps = SyntaxFactory.Parameter(SyntaxFactory.Identifier(string.Concat(varKeyword, " ", varName)));
var newList = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList<ParameterSyntax>().AddRange(
mds.ParameterList.ChildNodes().OfType<ParameterSyntax>()).Add(ps));
var methodNode = typeDecl.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault();
var variableNode = typeDecl.Parent.Parent;
var variableParentNode = typeDecl.Parent.Parent.Parent;
//Track the nodes we'll be using
var newRoot = root.TrackNodes(methodNode, variableNode, variableParentNode);
//Remode/replace the variable declaration
var trackedVariableParentNode = newRoot.GetCurrentNode(variableParentNode);
var trackedVariableNode = newRoot.GetCurrentNode(variableNode);
var newVariableParentNode = trackedVariableParentNode.RemoveNode(trackedVariableNode, SyntaxRemoveOptions.KeepNoTrivia);
newRoot = newRoot.ReplaceNode(trackedVariableParentNode, newVariableParentNode);
//Replace the method parameters
var trackedMethodNode = newRoot.GetCurrentNode(methodNode);
var newMethodNode = trackedMethodNode.ReplaceNode(trackedMethodNode.ParameterList, newList);
newRoot = newRoot.ReplaceNode(trackedMethodNode, newMethodNode);
return document.WithSyntaxRoot(newRoot);
}