本文整理汇总了C#中ISyntaxFactsService.GetContainingTypeDeclaration方法的典型用法代码示例。如果您正苦于以下问题:C# ISyntaxFactsService.GetContainingTypeDeclaration方法的具体用法?C# ISyntaxFactsService.GetContainingTypeDeclaration怎么用?C# ISyntaxFactsService.GetContainingTypeDeclaration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISyntaxFactsService
的用法示例。
在下文中一共展示了ISyntaxFactsService.GetContainingTypeDeclaration方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetNameObjectPart
/// <summary>
/// Take another look at the LHS of the += node -- we need to figure out a default name
/// for the event handler, and that's usually based on the object (which is usually a
/// field of 'this', but not always) to which the event belongs. So, if the event is
/// something like 'button1.Click' or 'this.listBox1.Select', we want the names
/// 'button1' and 'listBox1' respectively. If the field belongs to 'this', then we use
/// the name of this class, as we do if we can't make any sense out of the parse tree.
/// </summary>
private string GetNameObjectPart(IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel, ISyntaxFactsService syntaxFactsService)
{
AssertIsBackground();
var parentToken = plusEqualsToken.Parent as AssignmentExpressionSyntax;
var memberAccessExpression = parentToken.Left as MemberAccessExpressionSyntax;
if (memberAccessExpression != null)
{
// This is expected -- it means the last thing is(probably) the event name. We
// already have that in eventSymbol. What we need is the LHS of that dot.
var lhs = memberAccessExpression.Expression;
var lhsMemberAccessExpression = lhs as MemberAccessExpressionSyntax;
if (lhsMemberAccessExpression != null)
{
// Okay, cool. The name we're after is in the RHS of this dot.
return lhsMemberAccessExpression.Name.ToString();
}
var lhsNameSyntax = lhs as NameSyntax;
if (lhsNameSyntax != null)
{
// Even easier -- the LHS of the dot is the name itself
return lhsNameSyntax.ToString();
}
}
// If we didn't find an object name above, then the object name is the name of this class.
// Note: For generic, it's ok(it's even a good idea) to exclude type variables,
// because the name is only used as a prefix for the method name.
var typeDeclaration = syntaxFactsService.GetContainingTypeDeclaration(
semanticModel.SyntaxTree.GetRoot(),
plusEqualsToken.SpanStart) as BaseTypeDeclarationSyntax;
return typeDeclaration != null
? typeDeclaration.Identifier.Text
: eventSymbol.ContainingType.Name;
}