本文整理汇总了C#中ObjectCreationExpressionSyntax类的典型用法代码示例。如果您正苦于以下问题:C# ObjectCreationExpressionSyntax类的具体用法?C# ObjectCreationExpressionSyntax怎么用?C# ObjectCreationExpressionSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObjectCreationExpressionSyntax类属于命名空间,在下文中一共展示了ObjectCreationExpressionSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ChangeToImmutableArrayCreateRange
private static async Task<Document> ChangeToImmutableArrayCreateRange(
ObjectCreationExpressionSyntax objectCreation,
InitializerExpressionSyntax initializer,
INamedTypeSymbol immutableArrayType,
ITypeSymbol elementType,
Document document,
CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var arrayElementType = (TypeSyntax)generator.TypeExpression(elementType);
var arrayType = SyntaxFactory.ArrayType(arrayElementType,
SyntaxFactory.SingletonList(
SyntaxFactory.ArrayRankSpecifier(
SyntaxFactory.SingletonSeparatedList(
(ExpressionSyntax)SyntaxFactory.OmittedArraySizeExpression()))));
var arrayCreationExpression = SyntaxFactory.ArrayCreationExpression(
type: arrayType,
initializer: SyntaxFactory.InitializerExpression(
kind: SyntaxKind.ArrayInitializerExpression,
expressions: initializer.Expressions))
.WithAdditionalAnnotations(Formatter.Annotation);
var type = generator.TypeExpression(immutableArrayType);
var memberAccess = generator.MemberAccessExpression(type, "CreateRange");
var invocation = generator.InvocationExpression(memberAccess, arrayCreationExpression);
var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = oldRoot.ReplaceNode(objectCreation, invocation);
return document.WithSyntaxRoot(newRoot);
}
开发者ID:jwendl,项目名称:CoreFxAnalyzers,代码行数:33,代码来源:DoNotUseImmutableArrayCollectionInitializerCodeFix.cs
示例2: CheckExceptionType
internal static bool CheckExceptionType(SemanticModel model, ObjectCreationExpressionSyntax objectCreateExpression, out ExpressionSyntax paramNode)
{
paramNode = null;
var type = model.GetTypeInfo(objectCreateExpression).Type;
if (type == null)
return false;
if (type.Name == typeof(ArgumentException).Name)
{
if (objectCreateExpression.ArgumentList.Arguments.Count >= 2)
{
paramNode = objectCreateExpression.ArgumentList.Arguments[1].Expression;
}
return paramNode != null;
}
if (type.Name == typeof(ArgumentNullException).Name ||
type.Name == typeof(ArgumentOutOfRangeException).Name ||
type.Name == "DuplicateWaitObjectException")
{
if (objectCreateExpression.ArgumentList.Arguments.Count >= 1)
{
paramNode = objectCreateExpression.ArgumentList.Arguments[0].Expression;
}
return paramNode != null;
}
return false;
}
示例3: ObjectCreationExpression
public static string ObjectCreationExpression(ObjectCreationExpressionSyntax expression)
{
//Name all the arguments, since Swift usually requires named arguments when you create new objects.
//Thanks! http://stackoverflow.com/questions/24174602/get-constructor-declaration-from-objectcreationexpressionsyntax-with-roslyn/24191494#24191494
var symbol = Model.GetSymbolInfo(expression).Symbol as IMethodSymbol;
var namedArgumentsList = new SeparatedSyntaxList<ArgumentSyntax>();
for (var i = 0; i < expression.ArgumentList.Arguments.Count; i++)
{
var oldArgumentSyntax = expression.ArgumentList.Arguments[i];
var parameterName = symbol.Parameters[i].Name;
var nameColonSyntax = SyntaxFactory
.NameColon(SyntaxFactory.IdentifierName(parameterName))
.WithTrailingTrivia(SyntaxFactory.Whitespace(" "));
var namedArgumentSyntax = SyntaxFactory.Argument(nameColonSyntax, oldArgumentSyntax.RefOrOutKeyword, oldArgumentSyntax.Expression);
namedArgumentsList = namedArgumentsList.Add(namedArgumentSyntax);
}
//NOTE: this takes out expression.parent and everything, and probably screws with SyntaxModel stuff to
return SyntaxNode(expression.Type) + SyntaxNode(SyntaxFactory.ArgumentList(namedArgumentsList));
}
示例4: CreateUsing
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
{
SyntaxNode newRoot;
if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
: CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
{
var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration));
}
else
{
newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
}
return newRoot;
}
示例5: GetDelegateTypeConstructors
private IEnumerable<SignatureHelpItem> GetDelegateTypeConstructors(
ObjectCreationExpressionSyntax objectCreationExpression,
SemanticModel semanticModel,
ISymbolDisplayService symbolDisplayService,
IAnonymousTypeDisplayService anonymousTypeDisplayService,
INamedTypeSymbol delegateType,
INamedTypeSymbol containingType,
CancellationToken cancellationToken)
{
var invokeMethod = delegateType.DelegateInvokeMethod;
if (invokeMethod == null)
{
return null;
}
var position = objectCreationExpression.SpanStart;
var item = CreateItem(
invokeMethod, semanticModel, position,
symbolDisplayService, anonymousTypeDisplayService,
isVariadic: false,
documentationFactory: null,
prefixParts: GetDelegateTypePreambleParts(invokeMethod, semanticModel, position),
separatorParts: GetSeparatorParts(),
suffixParts: GetDelegateTypePostambleParts(invokeMethod),
parameters: GetDelegateTypeParameters(invokeMethod, semanticModel, position, cancellationToken));
return SpecializedCollections.SingletonEnumerable(item);
}
开发者ID:daking2014,项目名称:roslyn,代码行数:28,代码来源:ObjectCreationExpressionSignatureHelpProvider_DelegateType.cs
示例6: TryGetObjectCreationExpression
private bool TryGetObjectCreationExpression(SyntaxNode root, int position, ISyntaxFactsService syntaxFacts, SignatureHelpTriggerReason triggerReason, CancellationToken cancellationToken, out ObjectCreationExpressionSyntax expression)
{
if (!CommonSignatureHelpUtilities.TryGetSyntax(root, position, syntaxFacts, triggerReason, IsTriggerToken, IsArgumentListToken, cancellationToken, out expression))
{
return false;
}
return expression.ArgumentList != null;
}
示例7: GetClassificationForCommandCreation
public static bool GetClassificationForCommandCreation(ISemanticModel model, ObjectCreationExpressionSyntax syntax, CancellationToken cancellationToken = new CancellationToken())
{
if (syntax == null) return false;
var cmdType = model.GetSemanticInfo(syntax, cancellationToken);
//var cmdType = model.GetSemanticInfo(syntax, cancellationToken);
return cmdType.Type.AllInterfaces.AsList().Select(x => x.Name).Any(x => x == "ICommand");
}
示例8: CreateUsing
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
{
SyntaxNode newRoot;
if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
: CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
{
var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
{
var identifierName = GetIdentifierName(objectCreation, semanticModel);
var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
.WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
.WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));
var arg = objectCreation.Parent as ArgumentSyntax;
var args = objectCreation.Parent.Parent as ArgumentListSyntax;
var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));
StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>();
if (statement != null)
{
var exprStatement = statement.ReplaceNode(args, newArgs);
var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
.WithDeclaration(variableDeclaration);
return root.ReplaceNode(statement, newUsingStatment);
}
statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
var newStatement = statement.ReplaceNode(args, newArgs);
var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
var usingBlock = SyntaxFactory.Block(statementsForUsing);
var usingStatement = CreateUsingStatement(newStatement, usingBlock)
.WithDeclaration(variableDeclaration);
var statementsToReplace = new List<StatementSyntax> { statement };
statementsToReplace.AddRange(statementsForUsing.Skip(1));
newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
}
else
{
newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
}
return newRoot;
}
示例9: BoundObjectCreationExpression
public BoundObjectCreationExpression(
IType type,
IConstructor boundConstructor,
List<BoundExpression> boundParameter,
ObjectCreationExpressionSyntax expressionSyntax)
: base(expressionSyntax, type)
{
BoundConstructor = boundConstructor;
BoundParameter = boundParameter;
}
示例10: ChangeToImmutableArrayEmpty
private static async Task<Document> ChangeToImmutableArrayEmpty(ObjectCreationExpressionSyntax objectCreation, Document document, CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var memberAccess = generator.MemberAccessExpression(objectCreation.Type, "Empty");
var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = oldRoot.ReplaceNode(objectCreation, memberAccess);
return document.WithSyntaxRoot(newRoot);
}
示例11: VisitObjectCreationExpression
public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
var symbol = (IMethodSymbol)SemanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null)
{
IsAsyncLibraryConstruct(symbol.OriginalDefinition);
}
base.VisitObjectCreationExpression(node);
}
示例12: CreateNewBlockParent
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
{
var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
var newBlockParent = SyntaxFactory.Block()
.WithLeadingTrivia(blockParent.GetLeadingTrivia())
.WithTrailingTrivia(blockParent.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var newAssignmentExpressions = new List<ExpressionStatementSyntax>();
for (int i = 0; i < blockParent.Statements.Count; i++)
{
var blockStatement = blockParent.Statements[i];
if (blockStatement.Equals(statement))
{
var initializationExpressions = new List<AssignmentExpressionSyntax>();
foreach (var expressionStatement in assignmentExpressions)
{
var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
initializationExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right));
}
var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions);
var newObjectCreationExpression = objectCreationExpression.WithInitializer(
SyntaxFactory.InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia("\n")),
initializers,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
))
.WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
.WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0)
{
newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null);
}
var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
i += initializationExpressions.Count;
}
else
{
newBlockParent = newBlockParent.AddStatements(blockStatement
.WithLeadingTrivia(blockStatement.GetLeadingTrivia())
.WithTrailingTrivia(blockStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation));
}
}
return newBlockParent;
}
示例13: VisitObjectCreationExpression
public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
var symbol = SemanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null)
{
Enums.ThreadingNamespaceDetected type = CheckThreadingUsage(symbol);
Result.StoreDetectedThreadingNamespaceUsage(type);
Result.WriteDetectedThreadingNamespaceUsage(type, Document.FilePath, symbol, node);
}
base.VisitObjectCreationExpression(node);
}
示例14: IsDefaultCtorOnStruct
private bool IsDefaultCtorOnStruct(ObjectCreationExpressionSyntax objectCreation, ITypeSymbol symbol, SemanticModel semanticModel)
{
if (symbol.IsValueType)
{
var ctor = semanticModel.GetSymbolInfo(objectCreation).Symbol as IMethodSymbol;
if (ctor != null)
{
return ctor.Parameters.Length == 0;
}
}
return false;
}
开发者ID:SergeyTeplyakov,项目名称:ErrorProne.NET,代码行数:13,代码来源:AssignmentFreePureObjectConstructionAnalyzer.cs
示例15: GetValidParameterNames
internal static List<string> GetValidParameterNames(ObjectCreationExpressionSyntax objectCreateExpression)
{
var names = new List<string>();
var node = objectCreateExpression.Parent;
while (node != null && !(node is TypeBlockSyntax) && !(node is AnonymousObjectCreationExpressionSyntax))
{
var lambda = node as LambdaExpressionSyntax;
if (lambda != null)
{
names.AddRange(lambda.SubOrFunctionHeader.ParameterList.Parameters.Select(p => p.Identifier.ToString()));
break;
}
var indexer = node as PropertyBlockSyntax;
if ((indexer != null) && (indexer.PropertyStatement.ParameterList != null))
{
names.AddRange(indexer.PropertyStatement.ParameterList.Parameters.Select(p => p.Identifier.ToString()));
break;
}
var methodDeclaration = node as MethodBlockSyntax;
if (methodDeclaration != null)
{
names.AddRange(methodDeclaration.SubOrFunctionStatement.ParameterList.Parameters.Select(p => p.Identifier.ToString()));
break;
}
var constructorDeclaration = node as ConstructorBlockSyntax;
if (constructorDeclaration != null)
{
names.AddRange(constructorDeclaration.SubNewStatement.ParameterList.Parameters.Select(p => p.Identifier.ToString()));
break;
}
var accessor = node as AccessorBlockSyntax;
if (accessor != null)
{
if (accessor.IsKind(SyntaxKind.SetAccessorBlock) ||
accessor.IsKind(SyntaxKind.AddHandlerAccessorBlock) ||
accessor.IsKind(SyntaxKind.RemoveHandlerAccessorBlock))
{
names.Add("value");
}
var propertyParent = node.Parent as PropertyBlockSyntax;
if ((propertyParent == null) || (propertyParent.PropertyStatement.ParameterList == null))
break;
}
node = node.Parent;
}
return names;
}