本文整理汇总了C#中MemberDeclarationSyntax类的典型用法代码示例。如果您正苦于以下问题:C# MemberDeclarationSyntax类的具体用法?C# MemberDeclarationSyntax怎么用?C# MemberDeclarationSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MemberDeclarationSyntax类属于命名空间,在下文中一共展示了MemberDeclarationSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateAsync
/// <inheritdoc />
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(MemberDeclarationSyntax applyTo, Document document, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
{
var type = (ClassDeclarationSyntax)applyTo;
var generatedType = type
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>());
var methodsWithNativePointers =
from method in type.Members.OfType<MethodDeclarationSyntax>()
where WhereIsPointerParameter(method.ParameterList.Parameters).Any() || method.ReturnType is PointerTypeSyntax
select method;
foreach (var method in methodsWithNativePointers)
{
var intPtrOverload = method
.WithParameterList(TransformParameterList(method.ParameterList))
.WithReturnType(TransformReturnType(method.ReturnType))
.WithIdentifier(TransformMethodName(method))
.WithModifiers(RemoveModifier(method.Modifiers, SyntaxKind.ExternKeyword))
.WithAttributeLists(SyntaxFactory.List<AttributeListSyntax>())
.WithLeadingTrivia(method.GetLeadingTrivia().Where(t => !t.IsDirective))
.WithTrailingTrivia(method.GetTrailingTrivia().Where(t => !t.IsDirective))
.WithBody(CallNativePointerOverload(method))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None));
generatedType = generatedType.AddMembers(intPtrOverload);
}
return Task.FromResult(SyntaxFactory.SingletonList<MemberDeclarationSyntax>(generatedType));
}
示例2: Create
public static ClassDeclarationSyntax Create(string name, BaseTypeSyntax[] baseTypes, ConstructorDeclarationSyntax constructor, MemberDeclarationSyntax[] body)
{
return SyntaxFactory.ClassDeclaration(name)
.AddBaseListTypes(baseTypes)
.AddMembers(constructor)
.AddMembers(body);
}
示例3: CheckDeclarationName
private static void CheckDeclarationName(MemberDeclarationSyntax member, SyntaxToken identifier, SyntaxNodeAnalysisContext context)
{
var symbol = context.SemanticModel.GetDeclaredSymbol(member);
if (ClassName.IsTypeComRelated(symbol?.ContainingType) ||
symbol.IsInterfaceImplementationOrMemberOverride() ||
symbol.IsExtern)
{
return;
}
if (identifier.ValueText.StartsWith("_", StringComparison.Ordinal) ||
identifier.ValueText.EndsWith("_", StringComparison.Ordinal))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), MethodKindNameMapping[member.Kind()],
identifier.ValueText, MessageFormatUnderscore));
return;
}
string suggestion;
if (TryGetChangedName(identifier.ValueText, out suggestion))
{
var messageEnding = string.Format(MessageFormatNonUnderscore, suggestion);
context.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), MethodKindNameMapping[member.Kind()],
identifier.ValueText, messageEnding));
}
}
示例4: ShouldUseOverrideKeyword
private static bool ShouldUseOverrideKeyword(MemberDeclarationSyntax member, bool isInterface)
{
ISymbol symbol = TypeProcessor.GetDeclaredSymbol(member);
if (symbol is IMethodSymbol)
{
if ((symbol as IMethodSymbol).IsGenericMethod)
return false;
}
if (symbol.ContainingType.TypeKind == TypeKind.Struct ||
symbol.ContainingType.TypeKind == TypeKind.Interface)
{
return false;
// Structs dont have a base class to override (maybe opEquals) ... but that will be handled separately
//Interfaces are contracts, so no overriding here// maybe we should compare the methods
}
if (member.GetModifiers().Any(SyntaxKind.OverrideKeyword) && !isInterface)
return true;
if (member.GetModifiers().Any(SyntaxKind.StaticKeyword))
return false;
// if (method.Modifiers.Any(SyntaxKind.NewKeyword))
// return symbol.ContainingType.BaseType.GetMembers(symbol.Name).Any(k=>k.IsAbstract || k.IsVirtual);
if (member.GetModifiers().Any(SyntaxKind.PartialKeyword))
//partial methods seem exempt from C#'s normal override keyword requirement, so we have to check manually to see if it exists in a base class
return symbol.ContainingType.BaseType.GetMembers(symbol.Name).Any();
return member.GetModifiers().Any(SyntaxKind.OverrideKeyword);
}
示例5: GetDeclaredAccessibility
private Accessibility GetDeclaredAccessibility(MemberDeclarationSyntax declaration)
{
var result = Accessibility.Private;
SyntaxTokenList? syntaxTokenList;
syntaxTokenList = (declaration as PropertyDeclarationSyntax)?.Modifiers ??
(declaration as FieldDeclarationSyntax)?.Modifiers ??
(declaration as ConstructorDeclarationSyntax)?.Modifiers ??
(declaration as MethodDeclarationSyntax)?.Modifiers;
var accessibilityToken = syntaxTokenList?.Where(x => accessibilityTypes.Contains(x.Kind())).FirstOrDefault();
if (accessibilityToken.HasValue)
{
switch (accessibilityToken.Value.Kind())
{
case SyntaxKind.PublicKeyword:
result = Accessibility.Public;
break;
case SyntaxKind.PrivateKeyword:
result = Accessibility.Private;
break;
case SyntaxKind.InternalKeyword:
result = Accessibility.Internal;
break;
case SyntaxKind.ProtectedKeyword:
result = Accessibility.Protected;
break;
default:
result = Accessibility.Private;
break;
}
}
return result;
}
示例6: YieldClassGenerator
public YieldClassGenerator(Compilation compilation, ClassDeclarationSyntax classDeclarationSyntax, MemberDeclarationSyntax node)
{
this.compilation = compilation;
this.classDeclarationSyntax = classDeclarationSyntax;
this.node = node;
method = ModelExtensions.GetDeclaredSymbol(compilation.GetSemanticModel(node.SyntaxTree), node);
}
示例7: MemberOrderHelper
/// <summary>
/// Initializes a new instance of the <see cref="MemberOrderHelper"/> struct.
/// </summary>
/// <param name="member">The member to wrap.</param>
public MemberOrderHelper(MemberDeclarationSyntax member)
{
this.Member = member;
var modifiers = member.GetModifiers();
this.modifierFlags = GetModifierFlags(modifiers);
this.accessibilty = AccessLevelHelper.GetAccessLevel(modifiers);
}
示例8: HasYield
public static bool HasYield(MemberDeclarationSyntax method)
{
if (method.IsAsync())
return false;
var yieldChecker = new YieldChecker(true);
method.Accept(yieldChecker);
return yieldChecker.isSpecial;
}
示例9: VisitMemberDeclaration
private SyntaxNode VisitMemberDeclaration(MemberDeclarationSyntax node, SyntaxNode newNode)
{
var newMember = ((MemberDeclarationSyntax) newNode).WithoutAttributes();
var isSolutionPart = node.HasAttribute<ExcludeFromSolutionAttribute>() || node.HasAttribute<ExerciseAttribute>();
if (node.HasAttribute<ExcludeFromSolutionAttribute>()
|| (node is TypeDeclarationSyntax && node.HasAttribute<ExerciseAttribute>()))
Exercise.EthalonSolution += newMember.ToFullString();
return isSolutionPart ? null : newMember;
}
示例10: Sort
internal static int Sort(MemberDeclarationSyntax first, MemberDeclarationSyntax second)
{
Requires.NotNull(first, "first");
Requires.NotNull(second, "second");
int firstOrder = GetMemberDeclarationTypeOrder(first);
int secondOrder = GetMemberDeclarationTypeOrder(second);
int compareResult = firstOrder.CompareTo(secondOrder);
if (compareResult == 0)
{
firstOrder = GetMemberDeclarationVisibilityOrder(first);
secondOrder = GetMemberDeclarationVisibilityOrder(second);
compareResult = firstOrder.CompareTo(secondOrder);
if (compareResult == 0)
{
bool firstIsStatic = first.HasModifier(SyntaxKind.StaticKeyword);
bool secondIsStatic = second.HasModifier(SyntaxKind.StaticKeyword);
if (firstIsStatic && !secondIsStatic)
{
compareResult = -1;
}
else if (!firstIsStatic && secondIsStatic)
{
compareResult = 1;
}
if (compareResult == 0)
{
bool firstIsReadOnly = first.HasModifier(SyntaxKind.ReadOnlyKeyword);
bool secondIsReadOnly = second.HasModifier(SyntaxKind.ReadOnlyKeyword);
if (firstIsReadOnly && !secondIsReadOnly)
{
compareResult = -1;
}
else if (!firstIsReadOnly && secondIsReadOnly)
{
compareResult = 1;
}
if (compareResult == 0)
{
var firstName = GetName(first);
var secondName = GetName(second);
if (firstName.HasValue && secondName.HasValue)
{
compareResult = string.Compare(firstName.Value.ValueText, secondName.Value.ValueText, StringComparison.CurrentCulture);
}
}
}
}
}
return compareResult;
}
示例11: MemberOrderHelper
/// <summary>
/// Initializes a new instance of the <see cref="MemberOrderHelper"/> struct.
/// </summary>
/// <param name="member">The member to wrap.</param>
/// <param name="elementOrder">The element ordering traits.</param>
internal MemberOrderHelper(MemberDeclarationSyntax member, ImmutableArray<OrderingTrait> elementOrder)
{
this.Member = member;
var modifiers = member.GetModifiers();
var type = member.Kind();
type = type == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : type;
this.Priority = 0;
foreach (OrderingTrait trait in elementOrder)
{
switch (trait)
{
case OrderingTrait.Kind:
// 4 bits are required to store this.
this.Priority <<= 4;
this.Priority |= TypeMemberOrder.IndexOf(type) & 0x0F;
break;
case OrderingTrait.Accessibility:
// 3 bits are required to store this.
this.Priority <<= 3;
this.Priority |= (int)GetAccessLevelForOrdering(member, modifiers) & 0x07;
break;
case OrderingTrait.Constant:
this.Priority <<= 1;
if (modifiers.Any(SyntaxKind.ConstKeyword))
{
this.Priority |= 1;
}
break;
case OrderingTrait.Static:
this.Priority <<= 1;
if (modifiers.Any(SyntaxKind.StaticKeyword))
{
this.Priority |= 1;
}
break;
case OrderingTrait.Readonly:
this.Priority <<= 1;
if (modifiers.Any(SyntaxKind.ReadOnlyKeyword))
{
this.Priority |= 1;
}
break;
default:
continue;
}
}
}
示例12: VisitMemberDeclaration
private SyntaxNode VisitMemberDeclaration(MemberDeclarationSyntax node, SyntaxNode newNode)
{
var parent = node.GetParents().OfType<BaseTypeDeclarationSyntax>().FirstOrDefault();
if (!ShowOnSlide(node)) return null;
if (parent == null
|| parent.HasAttribute<SlideAttribute>()
|| parent.HasAttribute<ShowBodyOnSlideAttribute>())
AddCodeBlock(((MemberDeclarationSyntax)newNode));
return ((MemberDeclarationSyntax)newNode).WithoutAttributes();
}
示例13: GetDeclaredSymbols
internal static IEnumerable<ISymbol> GetDeclaredSymbols(SemanticModel semanticModel, MemberDeclarationSyntax memberDeclaration, CancellationToken cancellationToken)
{
if (memberDeclaration is FieldDeclarationSyntax)
{
return ((FieldDeclarationSyntax)memberDeclaration).Declaration.Variables.Select(
v => semanticModel.GetDeclaredSymbol(v, cancellationToken));
}
return SpecializedCollections.SingletonEnumerable(
semanticModel.GetDeclaredSymbol(memberDeclaration, cancellationToken));
}
示例14: UpdateNamedTypeDeclaration
public static MemberDeclarationSyntax UpdateNamedTypeDeclaration(
ICodeGenerationService service,
MemberDeclarationSyntax declaration,
IList<ISymbol> newMembers,
CodeGenerationOptions options,
CancellationToken cancellationToken)
{
declaration = RemoveAllMembers(declaration);
declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
return AddCleanupAnnotationsTo(declaration);
}
示例15: ExtractToExpressionTree
private async Task<Document> ExtractToExpressionTree(Document document, MemberDeclarationSyntax declaration, CancellationToken c)
{
var typeSyntax = declaration.Ancestors().OfType<TypeDeclarationSyntax>().First();
var sm = await document.GetSemanticModelAsync(c);
var type = sm.GetDeclaredSymbol(typeSyntax, c);
var symbol = sm.GetDeclaredSymbol(declaration);
string name = declaration is MethodDeclarationSyntax ?
((MethodDeclarationSyntax)declaration).Identifier.ToString() :
((PropertyDeclarationSyntax)declaration).Identifier.ToString();
string candidateName = name + "Expression";
if (type.MemberNames.Contains(candidateName))
candidateName = Enumerable.Range(2, 1000).Select(i => name + "Expression" + i).First(n => !type.MemberNames.Contains(n));
List<ParameterSyntax> parameterList = GetParameters(declaration, type, symbol);
TypeSyntax returnType = GetReturnType(declaration);
var bodyExpression = GetSingleBody(declaration);
var newField = GetStaticField(candidateName, parameterList, returnType, bodyExpression, sm, type);
var newBody = GetNewBody(candidateName, parameterList);
var newDeclaration = declaration.ReplaceNode(bodyExpression, newBody);
MoveInitialTrivia(ref newField, ref newDeclaration);
var newMembers = typeSyntax.Members.ReplaceRange(declaration, new[] { newField, newDeclaration });
var newType = typeSyntax is StructDeclarationSyntax ?
(TypeDeclarationSyntax)((StructDeclarationSyntax)typeSyntax).WithMembers(newMembers) :
(TypeDeclarationSyntax)((ClassDeclarationSyntax)typeSyntax).WithMembers(newMembers);
var docRoot = await document.GetSyntaxRootAsync();
var newDoc = docRoot.ReplaceNode(typeSyntax, newType);
var usings = newDoc.ChildNodes().OfType<UsingDirectiveSyntax>();
if (usings.Any() && !usings.Any(a => a.Name.ToString() == "System.Linq.Expressions"))
newDoc = newDoc.InsertNodesAfter(usings.LastOrDefault(), new[] {
SyntaxFactory.UsingDirective(
SyntaxFactory.IdentifierName("System").Qualified("Linq").Qualified("Expressions")
).NormalizeWhitespace().WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed)
});
return document.WithSyntaxRoot(newDoc);
}