本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.DescendantNodes方法的典型用法代码示例。如果您正苦于以下问题:C# ClassDeclarationSyntax.DescendantNodes方法的具体用法?C# ClassDeclarationSyntax.DescendantNodes怎么用?C# ClassDeclarationSyntax.DescendantNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax
的用法示例。
在下文中一共展示了ClassDeclarationSyntax.DescendantNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CallConstuctor
private static ObjectCreationExpressionSyntax CallConstuctor(ClassDeclarationSyntax @class)
{
var typeName = F.ParseName(@class.Identifier.Text);
var args = F.ArgumentList();
var ctor = @class.DescendantNodes().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
if(ctor != null)
{
var parameters = ctor.ParameterList.DescendantNodes().OfType<ParameterSyntax>().ToList();
foreach (var parameter in parameters)
{
var ident = parameter.DescendantNodes().OfType<IdentifierNameSyntax>().Single();
var parameterType = ident.Identifier.Text;
var invocation = F.InvocationExpression(F.MemberAccessExpression(
K.SimpleMemberAccessExpression,
F.IdentifierName(parameterType),
F.IdentifierName("Factory")));
args = args.AddArguments(F.Argument(invocation));
}
}
return F.ObjectCreationExpression(typeName)
.WithNewKeyword(F.Token(F.TriviaList(), K.NewKeyword, F.TriviaList(F.Space)))
.WithArgumentList(args);
}
示例2: IsOverridingMethod
private static bool IsOverridingMethod(ClassDeclarationSyntax classDeclaration, string identifierName)
{
var methodDeclarations =
classDeclaration.DescendantNodes()
.Where(node => node.IsKind(SyntaxKind.MethodDeclaration))
.Cast<MethodDeclarationSyntax>();
var publicOverrideMethods = methodDeclarations.Where(node => node.Modifiers.Any(SyntaxKind.OverrideKeyword)
&& node.Modifiers.Any(SyntaxKind.PublicKeyword));
return publicOverrideMethods.Any(node => node.Identifier.Text == identifierName);
}
示例3: CreateClassNode
private static ClassNode CreateClassNode(SemanticModel model, ClassDeclarationSyntax c)
{
var declaredSymbol = model.GetDeclaredSymbol(c);
var subnodes = c.DescendantNodes();
var symbols = subnodes.Select(node => model.GetSymbolInfo(node).Symbol).ToList();
var dependencies = symbols.OfType<INamedTypeSymbol>();
var nrOfMethods = subnodes.OfType<MethodDeclarationSyntax>().Count();
IEnumerable<TypeSyntax> basetypes = new List<TypeSyntax>();
if (c.BaseList != null && c.BaseList.Types.Any())
basetypes = c.BaseList.Types.Select(x => x.Type);
return new ClassNode(declaredSymbol, basetypes, nrOfMethods) {SymbolDependencies = dependencies};
}
示例4: VisitClassDeclaration
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
LookForAggregates(node);
LookForCommands(node);
LookForCommandHandlers(node);
LookForEvents(node);
LookForEventHandlers(node);
var methods = node.DescendantNodes().OfType<MethodDeclarationSyntax>();
foreach (var method in methods.Where(x => x.Body != null))
{
Visit(method);
}
}
示例5: IsPropertyBetweenRegion
/// <summary>
/// checks if a property with the given name is enclosed
/// by a region with the given name
/// </summary>
/// <param name="classDeclaration"></param>
/// <param name="regionName"></param>
/// <param name="propertyName"></param>
/// <returns>true if there is a property between the region,
/// false otherwise</returns>
public static bool IsPropertyBetweenRegion(
ClassDeclarationSyntax classDeclaration,
string regionName,
string propertyName)
{
// assert: the method must be between the region
// get begin and end region
var beginRegion = classDeclaration.FindRegionByText(regionName);
var endRegion = classDeclaration.FindEndRegion(regionName);
// get all nodes between the regions
var span = new TextSpan(beginRegion.Span.End, endRegion.Span.Start);
var nodesBetweenRegion = classDeclaration.DescendantNodes(span);
// check that a property declaration for a "Name" property is there
var isPropertyBetweenRegion = nodesBetweenRegion
.OfType<PropertyDeclarationSyntax>()
.Any(x => x.Identifier.ToString() == propertyName);
return isPropertyBetweenRegion;
}
示例6: CheckForInternalFields
/// <summary>
/// Checks that machine fields are non-internal.
/// </summary>
/// <param name="machine">Machine</param>
private void CheckForInternalFields(ClassDeclarationSyntax machine)
{
var fieldIdentifiers = machine.DescendantNodes().OfType<FieldDeclarationSyntax>().
Where(val => val.Modifiers.Any(SyntaxKind.InternalKeyword)).
SelectMany(val => val.Declaration.Variables).
Select(val => val.Identifier).
ToList();
foreach (var identifier in fieldIdentifiers)
{
base.ErrorLog.Add(Tuple.Create(identifier, "Not allowed to declare field '" +
identifier.ValueText + "' of " + this.GetTypeOfMachine().ToLower() + " '" +
machine.Identifier.ValueText + "' as internal."));
}
}
示例7: CheckForPublicMethods
/// <summary>
/// Checks that machine methods are non-public.
/// </summary>
/// <param name="machine">Machine</param>
private void CheckForPublicMethods(ClassDeclarationSyntax machine)
{
var methodIdentifiers = machine.DescendantNodes().OfType<MethodDeclarationSyntax>().
Where(val => val.Modifiers.Any(SyntaxKind.PublicKeyword)).
Select(val => val.Identifier).
ToList();
foreach (var identifier in methodIdentifiers)
{
base.ErrorLog.Add(Tuple.Create(identifier, "Not allowed to declare method '" +
identifier.ValueText + "' of " + this.GetTypeOfMachine().ToLower() + " '" +
machine.Identifier.ValueText + "' as public."));
}
}
示例8: CheckForStructs
/// <summary>
/// Checks that no structs are declared inside the state.
/// </summary>
/// <param name="state">State</param>
private void CheckForStructs(ClassDeclarationSyntax state)
{
var structs = state.DescendantNodes().OfType<StructDeclarationSyntax>().
ToList();
if (structs.Count > 0)
{
base.ErrorLog.Add(Tuple.Create(state.Identifier, "State '" +
state.Identifier.ValueText + "' cannot declare structs."));
}
}
示例9: ProcessApplyToClassDeclaration
public override ClassDeclarationSyntax ProcessApplyToClassDeclaration(ClassDeclarationSyntax applyTo)
{
applyTo = base.ProcessApplyToClassDeclaration(applyTo);
if (this.applyTo.IsRecursiveParentOrDerivative)
{
// Add the lookupTable parameter to the constructor's signature.
var origCtor = applyTo.Members.OfType<ConstructorDeclarationSyntax>().Single();
var alteredCtor = origCtor.AddParameterListParameters(SyntaxFactory.Parameter(LookupTableFieldName.Identifier).WithType(Syntax.OptionalOf(this.lookupTableType)));
// If this type isn't itself the recursive parent then we derive from it. And we must propagate the value to the chained base type.
if (!this.applyTo.IsRecursiveParent)
{
Assumes.NotNull(alteredCtor.Initializer); // we expect a chained call to the base constructor.
alteredCtor = alteredCtor.WithInitializer(
alteredCtor.Initializer.AddArgumentListArguments(
SyntaxFactory.Argument(SyntaxFactory.NameColon(LookupTableFieldName), NoneToken, LookupTableFieldName)));
}
// Apply the updated constructor back to the generated type.
applyTo = applyTo.ReplaceNode(origCtor, alteredCtor);
// Search for invocations of the constructor that we now have to update.
var invocations = (
from n in applyTo.DescendantNodes()
let ctorInvocation = n as ObjectCreationExpressionSyntax
let instantiatedTypeName = ctorInvocation?.Type?.ToString()
where instantiatedTypeName == this.applyTo.TypeSyntax.ToString() || instantiatedTypeName == this.applyTo.TypeSymbol.Name
select ctorInvocation).ToImmutableArray();
var trackedTree = applyTo.TrackNodes(invocations);
var recursiveField = this.applyTo.RecursiveParent.RecursiveField;
foreach (var ctorInvocation in invocations)
{
var currentInvocation = trackedTree.GetCurrentNode(ctorInvocation);
ExpressionSyntax lookupTableValue = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
var containingMethod = currentInvocation.FirstAncestorOrSelf<MethodDeclarationSyntax>();
if (containingMethod != null)
{
if (containingMethod.ParameterList.Parameters.Any(p => p.Identifier.ToString() == recursiveField.Name))
{
// We're in a method that accepts the recursive field as a parameter.
// The value we want to pass in for the lookup table is:
// (children.IsDefined && children.Value != this.Children) ? default(Optional<ImmutableDictionary<uint, KeyValuePair<RecursiveType, uint>>>) : Optional.For(this.lookupTable);
lookupTableValue = SyntaxFactory.ConditionalExpression(
SyntaxFactory.ParenthesizedExpression(
SyntaxFactory.BinaryExpression(
SyntaxKind.LogicalAndExpression,
Syntax.OptionalIsDefined(recursiveField.NameAsField),
SyntaxFactory.BinaryExpression(
SyntaxKind.NotEqualsExpression,
Syntax.OptionalValue(recursiveField.NameAsField),
Syntax.ThisDot(recursiveField.NameAsProperty)))),
SyntaxFactory.DefaultExpression(Syntax.OptionalOf(this.lookupTableType)),
Syntax.OptionalFor(Syntax.ThisDot(LookupTableFieldName)));
}
}
var alteredInvocation = currentInvocation.AddArgumentListArguments(
SyntaxFactory.Argument(SyntaxFactory.NameColon(LookupTableFieldName), NoneToken, lookupTableValue));
trackedTree = trackedTree.ReplaceNode(currentInvocation, alteredInvocation);
}
applyTo = trackedTree;
}
return applyTo;
}
示例10: CheckForAtLeastOneState
/// <summary>
/// Checks that at least one state is declared inside the machine.
/// </summary>
/// <param name="machine">Machine</param>
/// <param name="compilation">Compilation</param>
private void CheckForAtLeastOneState(ClassDeclarationSyntax machine,
CodeAnalysis.Compilation compilation)
{
var states = machine.DescendantNodes().OfType<ClassDeclarationSyntax>().
Where(val => this.IsState(compilation, val)).
ToList();
if (states.Count == 0)
{
base.WarningLog.Add(Tuple.Create(machine.Identifier, $"{this.GetTypeOfMachine().ToLower()} " +
$"'{machine.Identifier.ValueText}' must declare at least one state (unless the machine " +
"inherits at least one state from a base machine, or is partial, and one state has " +
"been already declared in another part of the declaration)."));
}
}
示例11: CheckForMethods
/// <summary>
/// Checks that no methods are declared inside the machine (beside the P# API ones).
/// </summary>
/// <param name="state">State</param>
private void CheckForMethods(ClassDeclarationSyntax state)
{
var methods = state.DescendantNodes().OfType<MethodDeclarationSyntax>().
Where(val => !val.Modifiers.Any(SyntaxKind.OverrideKeyword)).
ToList();
if (methods.Count > 0)
{
base.ErrorLog.Add(Tuple.Create(state.Identifier, "State '" +
state.Identifier.ValueText + "' cannot declare methods."));
}
}
示例12: CheckForStartState
/// <summary>
/// Checks that a machine has an start state.
/// </summary>
/// <param name="machine">Machine</param>
/// <param name="compilation">Compilation</param>
private void CheckForStartState(ClassDeclarationSyntax machine, CodeAnalysis.Compilation compilation)
{
var model = compilation.GetSemanticModel(machine.SyntaxTree);
var stateAttributes = machine.DescendantNodes().OfType<ClassDeclarationSyntax>().
Where(val => this.IsState(compilation, val)).
SelectMany(val => val.AttributeLists).
SelectMany(val => val.Attributes).
Where(val => model.GetTypeInfo(val).Type.ToDisplayString().Equals("Microsoft.PSharp.Start")).
ToList();
if (stateAttributes.Count == 0)
{
base.ErrorLog.Add(Tuple.Create(machine.Identifier, this.GetTypeOfMachine().ToLower() + " '" +
machine.Identifier.ValueText + "' must declare a start state."));
}
else if (stateAttributes.Count > 1)
{
base.ErrorLog.Add(Tuple.Create(machine.Identifier, this.GetTypeOfMachine().ToLower() + " '" +
machine.Identifier.ValueText + "' must declare only one start state."));
}
}
示例13: CheckForStartState
/// <summary>
/// Checks that a machine has an start state.
/// </summary>
/// <param name="machine">Machine</param>
/// <param name="compilation">Compilation</param>
private void CheckForStartState(ClassDeclarationSyntax machine, CodeAnalysis.Compilation compilation)
{
var model = compilation.GetSemanticModel(machine.SyntaxTree);
var stateAttributes = machine.DescendantNodes().OfType<ClassDeclarationSyntax>().
Where(val => this.IsState(compilation, val)).
SelectMany(val => val.AttributeLists).
SelectMany(val => val.Attributes).
Where(val => model.GetTypeInfo(val).Type.ToDisplayString().Equals("Microsoft.PSharp.Start")).
ToList();
if (stateAttributes.Count == 0)
{
base.WarningLog.Add(Tuple.Create(machine.Identifier, $"{this.GetTypeOfMachine().ToLower()} " +
$"'{machine.Identifier.ValueText}' must declare a start state (unless the machine " +
"inherits a start state from a base machine, or is partial, and one state has been " +
"already declared in another part of the declaration)."));
}
else if (stateAttributes.Count > 1)
{
base.ErrorLog.Add(Tuple.Create(machine.Identifier, $"{this.GetTypeOfMachine().ToLower()} " +
$"'{machine.Identifier.ValueText}' must declare only one start state."));
}
}
示例14: GetObjectCreations
private static IEnumerable<ObjectCreationExpressionSyntax> GetObjectCreations(SyntaxNodeAnalysisContext context, ClassDeclarationSyntax classDeclaration, PropertyDeclarationSyntax propertyDeclarationSyntax, IPropertySymbol propertySymbol)
{
var objectCreations = classDeclaration.DescendantNodes()
.OfType<AssignmentExpressionSyntax>()
.Where(a => context.SemanticModel.GetSymbolInfo(a.Left).Symbol.Equals(propertySymbol) && a.Right is ObjectCreationExpressionSyntax)
.Select(a => a.Right as ObjectCreationExpressionSyntax).ToList();
var arrowExpressionClause = propertyDeclarationSyntax.DescendantNodes()
.OfType<ArrowExpressionClauseSyntax>()
.SingleOrDefault(a => a.Expression is ObjectCreationExpressionSyntax)
?.Expression as ObjectCreationExpressionSyntax;
if (arrowExpressionClause != null)
{
objectCreations.Add(arrowExpressionClause);
}
var getAcessorDeclararion = propertyDeclarationSyntax.DescendantNodes()
.OfType<AccessorDeclarationSyntax>()
.SingleOrDefault(a => a.IsKind(SyntaxKind.GetAccessorDeclaration));
if (getAcessorDeclararion != null)
{
objectCreations.AddRange(getAcessorDeclararion.DescendantNodes()
.OfType<ObjectCreationExpressionSyntax>());
}
return objectCreations;
}
开发者ID:MvvmCross,项目名称:MvvmCross,代码行数:27,代码来源:CommandWithCanExecuteWithoutCanExecuteChangedAnalyzer.cs
示例15: CheckForNonStateNonEventClasses
/// <summary>
/// Checks that no non-state or non-event classes are declared inside the machine.
/// </summary>
/// <param name="machine">Machine</param>
/// <param name="compilation">Compilation</param>
private void CheckForNonStateNonEventClasses(ClassDeclarationSyntax machine, CodeAnalysis.Compilation compilation)
{
var classIdentifiers = machine.DescendantNodes().OfType<ClassDeclarationSyntax>().
Where(val => !this.IsState(compilation, val) && !Querying.IsEventDeclaration(compilation, val)).
Select(val => val.Identifier).
ToList();
foreach (var identifier in classIdentifiers)
{
base.ErrorLog.Add(Tuple.Create(identifier, "Not allowed to declare non-state class '" +
identifier.ValueText + "' inside " + this.GetTypeOfMachine().ToLower() + " '" +
machine.Identifier.ValueText + "'."));
}
}