本文整理汇总了C#中CSharpSyntaxNode.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpSyntaxNode.Ancestors方法的具体用法?C# CSharpSyntaxNode.Ancestors怎么用?C# CSharpSyntaxNode.Ancestors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSharpSyntaxNode
的用法示例。
在下文中一共展示了CSharpSyntaxNode.Ancestors方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDefaultParameterValue
/// <summary>
/// Gets the default value for the <paramref name="parameter"/>.
/// </summary>
/// <param name="syntax">
/// A syntax node corresponding to the invocation.
/// </param>
/// <param name="parameter">
/// A parameter to get the default value for.
/// </param>
/// <param name="enableCallerInfo">
/// Indicates if caller info is to be enabled when processing this optional parameter.
/// The value <see cref="ThreeState.Unknown"/> means the decision is to be made based on the shape of the <paramref name="syntax"/> node.
/// </param>
/// <remarks>
/// DELIBERATE SPEC VIOLATION: When processing an implicit invocation of an <c>Add</c> method generated
/// for an element-initializer in a collection-initializer, the parameter <paramref name="enableCallerInfo"/>
/// is set to <see cref="ThreeState.True"/>. It means that if the optional parameter is annotated with <see cref="CallerLineNumberAttribute"/>,
/// <see cref="CallerFilePathAttribute"/> or <see cref="CallerMemberNameAttribute"/>, and there is no explicit argument corresponding to it,
/// we will provide caller information as a value of this parameter.
/// This is done to match the native compiler behavior and user requests (see http://roslyn.codeplex.com/workitem/171). This behavior
/// does not match the C# spec that currently requires to provide caller information only in explicit invocations and query expressions.
/// </remarks>
private BoundExpression GetDefaultParameterValue(CSharpSyntaxNode syntax, ParameterSymbol parameter, ThreeState enableCallerInfo)
{
// TODO: Ideally, the enableCallerInfo parameter would be of just bool type with only 'true' and 'false' values, and all callers
// explicitly provided one of those values, so that we do not rely on shape of syntax nodes in the rewriter. There are not many immediate callers,
// but often the immediate caller does not have the required information, so all possible call chains should be analyzed and possibly updated
// to pass this information, and this might be a big task. We should consider doing this when the time permits.
TypeSymbol parameterType = parameter.Type;
Debug.Assert(parameter.IsOptional);
ConstantValue defaultConstantValue = parameter.ExplicitDefaultConstantValue;
BoundExpression defaultValue;
SourceLocation callerSourceLocation;
if (parameter.IsCallerLineNumber && ((callerSourceLocation = GetCallerLocation(syntax, enableCallerInfo)) != null))
{
int line = callerSourceLocation.SourceTree.GetDisplayLineNumber(callerSourceLocation.SourceSpan);
BoundExpression lineLiteral = MakeLiteral(syntax, ConstantValue.Create(line), _compilation.GetSpecialType(SpecialType.System_Int32));
if (parameterType.IsNullableType())
{
defaultValue = MakeConversion(lineLiteral, parameterType.GetNullableUnderlyingType(), false);
// wrap it in a nullable ctor.
defaultValue = new BoundObjectCreationExpression(
syntax,
GetNullableMethod(syntax, parameterType, SpecialMember.System_Nullable_T__ctor),
defaultValue);
}
else
{
defaultValue = MakeConversion(lineLiteral, parameterType, false);
}
}
else if (parameter.IsCallerFilePath && ((callerSourceLocation = GetCallerLocation(syntax, enableCallerInfo)) != null))
{
string path = callerSourceLocation.SourceTree.GetDisplayPath(callerSourceLocation.SourceSpan, _compilation.Options.SourceReferenceResolver);
BoundExpression memberNameLiteral = MakeLiteral(syntax, ConstantValue.Create(path), _compilation.GetSpecialType(SpecialType.System_String));
defaultValue = MakeConversion(memberNameLiteral, parameterType, false);
}
else if (parameter.IsCallerMemberName && ((callerSourceLocation = GetCallerLocation(syntax, enableCallerInfo)) != null))
{
string memberName;
switch (_factory.TopLevelMethod.MethodKind)
{
case MethodKind.Constructor:
case MethodKind.StaticConstructor:
// See if the code is actually part of a field, field-like event or property initializer and return the name of the corresponding member.
var memberDecl = syntax.Ancestors().OfType<MemberDeclarationSyntax>().FirstOrDefault();
if (memberDecl != null)
{
BaseFieldDeclarationSyntax fieldDecl;
if (memberDecl.Kind() == SyntaxKind.PropertyDeclaration)
{
var propDecl = (PropertyDeclarationSyntax)memberDecl;
EqualsValueClauseSyntax initializer = propDecl.Initializer;
if (initializer != null && initializer.Span.Contains(syntax.Span))
{
memberName = propDecl.Identifier.ValueText;
break;
}
}
else if ((fieldDecl = memberDecl as BaseFieldDeclarationSyntax) != null)
{
memberName = null;
foreach (VariableDeclaratorSyntax varDecl in fieldDecl.Declaration.Variables)
{
EqualsValueClauseSyntax initializer = varDecl.Initializer;
if (initializer != null && initializer.Span.Contains(syntax.Span))
{
memberName = varDecl.Identifier.ValueText;
break;
//.........这里部分代码省略.........