本文整理汇总了C#中SyntaxGenerator.ExpressionStatement方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxGenerator.ExpressionStatement方法的具体用法?C# SyntaxGenerator.ExpressionStatement怎么用?C# SyntaxGenerator.ExpressionStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxGenerator
的用法示例。
在下文中一共展示了SyntaxGenerator.ExpressionStatement方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDiagnosticReport
internal static SyntaxNode CreateDiagnosticReport(SyntaxGenerator generator, string argumentName, string contextName)
{
var argumentExpression = generator.IdentifierName(argumentName);
var argument = generator.Argument(argumentExpression);
var identifier = generator.IdentifierName(contextName);
var memberExpression = generator.MemberAccessExpression(identifier, "ReportDiagnostic");
var expression = generator.InvocationExpression(memberExpression, argument);
SyntaxNode expressionStatement = generator.ExpressionStatement(expression);
return expressionStatement;
}
示例2: CreateEnsureProxyMethod
//.........这里部分代码省略.........
),
g.DottedName("System.ServiceModel.CommunicationState.Faulted")
),
// ((System.ServiceModel.ICommunicationObject)m_cachedProxy).State == System.ServiceModel.CommunicationState.Faulted
g.ValueEqualsExpression(
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
)
),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Closed")
)
)
),
new SyntaxNode[]
{
// await CloseProxyAsync(false).ConfigureAwait(false);
// or
// CloseProxy(false);
AwaitExpressionIfAsync(g, asAsync,
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod]
),
g.FalseLiteralExpression()
)
)
}
),
g.IfStatement(
g.ReferenceEqualsExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
g.NullLiteralExpression()
),
new SyntaxNode[]
{
g.LocalDeclarationStatement(
"proxy",
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.ProxyFactoryField]
)
)
),
g.ExpressionStatement(
asAsync ?
AwaitExpression(g,
g.InvocationExpression(
g.DottedName("System.Threading.Tasks.Task.Factory.FromAsync"),
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.IdentifierName("proxy")
),
"BeginOpen"
),
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.IdentifierName("proxy")
),
"EndOpen"
),
g.NullLiteralExpression()
)
)
:
g.InvocationExpression(
g.MemberAccessExpression(
g.CastExpression(
compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"),
g.IdentifierName("proxy")
),
"Open"
)
)
),
g.AssignmentStatement(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
),
g.IdentifierName("proxy")
)
}
)
}
);
}
示例3: CreateDisposeMethods
private IEnumerable<SyntaxNode> CreateDisposeMethods(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool suppressWarningComments)
{
yield return g.AddWarningCommentIf(!suppressWarningComments,
g.MethodDeclaration(
"Dispose",
accessibility: Accessibility.Public,
statements: new SyntaxNode[]
{
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
"Dispose"
),
g.TrueLiteralExpression()
),
g.InvocationExpression(
g.MemberAccessExpression(
g.TypeExpression(compilation.RequireTypeByMetadataName("System.GC")),
"SuppressFinalize"
),
g.ThisExpression()
)
}
))
.PrependLeadingTrivia(g.CreateRegionTrivia("IDisposable"));
yield return g.AddWarningCommentIf(!suppressWarningComments, g.MethodDeclaration(
"Dispose",
parameters: new SyntaxNode[] { g.ParameterDeclaration("disposing", g.TypeExpression(SpecialType.System_Boolean)) },
accessibility: Accessibility.Private,
statements: new SyntaxNode[]
{
g.IfStatement(
g.ValueEqualsExpression(
g.IdentifierName("disposing"),
g.TrueLiteralExpression()
),
new SyntaxNode[]
{
g.TryCatchStatement(
new SyntaxNode[]
{
g.ExpressionStatement(
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CloseProxyMethod]
),
g.FalseLiteralExpression()
)
)
},
new SyntaxNode[]
{
g.CatchClause(new SyntaxNode [0])
}
)
}
)
}
)).AddTrailingTrivia(g.CreateEndRegionTrivia());
}
示例4: CreateStaticCloseProxyMethod
private SyntaxNode CreateStaticCloseProxyMethod(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, bool asAsync)
{
//private static void CloseProxy(System.ServiceModel.ICommunicationObject proxy, bool alwaysAbort)
//{
// try
// {
// if (proxy != null && proxy.State != System.ServiceModel.CommunicationState.Closed)
// {
// if (!alwaysAbort && proxy.State != System.ServiceModel.CommunicationState.Faulted)
// {
// proxy.Close();
// }
// else
// {
// proxy.Abort();
// }
// }
// }
// catch (System.ServiceModel.CommunicationException)
// {
// proxy.Abort();
// }
// catch (System.TimeoutException)
// {
// proxy.Abort();
// }
// catch
// {
// proxy.Abort();
// throw;
// }
//}
return g.MethodDeclaration(
asAsync ? nameTable[MemberNames.CloseProxyAsyncMethod] : nameTable[MemberNames.CloseProxyMethod],
accessibility: Accessibility.Private,
returnType: asAsync ? g.TypeExpression(compilation.RequireType<Task>()) : null,
modifiers: (asAsync ? DeclarationModifiers.Async : DeclarationModifiers.None) | DeclarationModifiers.Static,
parameters: new SyntaxNode[]
{
g.ParameterDeclaration("proxy", g.TypeExpression(compilation.RequireTypeByMetadataName("System.ServiceModel.ICommunicationObject"))),
g.ParameterDeclaration("alwaysAbort", g.TypeExpression(SpecialType.System_Boolean))
},
statements: new SyntaxNode[]
{
g.TryCatchStatement(
new SyntaxNode[]
{
// if (proxy != null && proxy.State != System.ServiceModel.CommunicationState.Closed)
g.IfStatement(
g.LogicalAndExpression(
g.ReferenceNotEqualsExpression(
g.IdentifierName("proxy"),
g.NullLiteralExpression()
),
g.ValueNotEqualsExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Closed")
)
),
new SyntaxNode[]
{
// if (!alwaysAbort && proxy.State != System.ServiceModel.CommunicationState.Faulted)
g.IfStatement(
g.LogicalAndExpression(
g.LogicalNotExpression(
g.IdentifierName("alwaysAbort")
),
g.ValueNotEqualsExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"State"
),
g.DottedName("System.ServiceModel.CommunicationState.Faulted")
)
),
new SyntaxNode[]
{
g.ExpressionStatement(
asAsync ?
// await System.Threading.Tasks.Task.Factory.FromAsync(proxy.BeginClose, proxy.EndClose, null).ConfigureAwait(false);
AwaitExpression(g,
g.InvocationExpression(
g.DottedName("System.Threading.Tasks.Task.Factory.FromAsync"),
g.DottedName("proxy.BeginClose"),
g.DottedName("proxy.EndClose"),
g.NullLiteralExpression()
)
)
:
// proxy.Close();
g.InvocationExpression(
g.MemberAccessExpression(
g.IdentifierName("proxy"),
"Close"
)
//.........这里部分代码省略.........
示例5: GenerateClientClass
//.........这里部分代码省略.........
var lambda = gen.ValueReturningLambdaExpression(
gen.ObjectCreationExpression(gen.IdentifierName(gen.GetName(proxyClass)), ctor.Parameters.Select(p => gen.IdentifierName(p.Name)))
);
targetCtor = gen.WithThisConstructorInitializer(targetCtor, new[] { lambda });
targetCtor = gen.AddWarningCommentIf(!suppressWarningComments, targetCtor);
targetCtor = gen.WithAccessibility(targetCtor, ToAccessibility(constructorAccessibility));
if (ctorEntry.IsLast)
{
targetCtor = targetCtor.AddTrailingTrivia(gen.CreateEndRegionTrivia()).AddNewLineTrivia();
}
targetClass = gen.AddMembers(targetClass, targetCtor.AddNewLineTrivia());
}
}
#endregion
#region Operation Contract Methods
// ==> catch
// ==> {
// ==> this.CloseProxy(false);
// ==> throw;
// ==> }
var catchAndCloseProxyStatement = gen.CatchClause(new SyntaxNode[]
{
// ==> this.CloseProxy(false);
gen.ExpressionStatement(
gen.InvocationExpression(
gen.MemberAccessExpression(
gen.ThisExpression(),
nameTable[MemberNames.CloseProxyMethod]
),
gen.FalseLiteralExpression()
)
),
// throw;
gen.ThrowStatement()
});
foreach (var sourceMethodEntry in methods.AsSmartEnumerable())
{
var sourceMethod = sourceMethodEntry.Value;
using (nameTable.PushScope(sourceMethod.Parameters.Select(p => p.Name)))
{
bool isAsync = ReturnsTask(semanticModel.Compilation, sourceMethod);
bool isVoid = sourceMethod.ReturnType.SpecialType == SpecialType.System_Void || sourceMethod.ReturnType.Equals(semanticModel.Compilation.RequireType<Task>());
SyntaxNode targetMethod = gen.MethodDeclaration(sourceMethod);
if (sourceMethodEntry.IsFirst)
targetMethod = targetMethod.PrependLeadingTrivia(gen.CreateRegionTrivia("Contract Methods")).AddLeadingTrivia(gen.NewLine());
targetMethod = gen.AddWarningCommentIf(!suppressWarningComments, targetMethod);
targetMethod = gen.WithModifiers(targetMethod, isAsync ? DeclarationModifiers.Async : DeclarationModifiers.None);
示例6: CreateGetProxyMethod
private SyntaxNode CreateGetProxyMethod(Compilation compilation, SyntaxGenerator g, INamedTypeSymbol proxyInterface, GenerationNameTable nameTable, bool isAsync)
{
//private IProxy GetProxy()
//{
// EnsureProxy();
// return m_cachedProxy;
//}
return g.MethodDeclaration(
isAsync ? nameTable[MemberNames.GetProxyAsyncMethod] : nameTable[MemberNames.GetProxyMethod],
returnType: g.TypeExpression(isAsync ? compilation.RequireType(typeof(Task<>)).Construct(proxyInterface) : proxyInterface),
accessibility: Accessibility.Private,
modifiers: isAsync ? DeclarationModifiers.Async : DeclarationModifiers.None,
statements: new SyntaxNode[]
{
g.ExpressionStatement(
AwaitExpressionIfAsync(g, isAsync,
g.InvocationExpression(
g.MemberAccessExpression(
g.ThisExpression(),
isAsync ? nameTable[MemberNames.EnsureProxyAsyncMethod] : nameTable[MemberNames.EnsureProxyMethod]
)
)
)
),
g.ReturnStatement(
g.MemberAccessExpression(
g.ThisExpression(),
nameTable[MemberNames.CachedProxyField]
)
)
}
);
}
示例7: CreateProxyInvocationStatement
private SyntaxNode CreateProxyInvocationStatement(Compilation compilation, SyntaxGenerator g, GenerationNameTable nameTable, IMethodSymbol sourceMethod)
{
bool isAsync = ReturnsTask(compilation, sourceMethod);
bool isVoid = IsVoid(compilation, sourceMethod);
// proxy.Method(arg1, arg2, ...);
SyntaxNode invocation = g.InvocationExpression(
g.MemberAccessExpression(
g.IdentifierName(nameTable[MemberNames.ProxyVariable]),
sourceMethod.Name
),
sourceMethod.Parameters.Select(p => g.IdentifierName(p.Name))
);
if (isAsync)
{
// await proxy.Method(arg1, arg2, ...).ConfigureAwait(false);
invocation =
g.AwaitExpression(
g.InvocationExpression(
g.MemberAccessExpression(
invocation,
"ConfigureAwait"
),
g.FalseLiteralExpression()
)
);
}
if (isVoid)
{
invocation = g.ExpressionStatement(invocation);
}
else
{
invocation = g.ReturnStatement(invocation);
}
return invocation;
}
示例8: GenerateProxyClass
public ClassDeclarationSyntax GenerateProxyClass(SemanticModel semanticModel, SyntaxGenerator generator, INamedTypeSymbol sourceProxyInterface, string name, Accessibility accessibility, bool suppressWarningComments, MemberAccessibility constructorAccessibility, out IEnumerable<IMethodSymbol> sourceConstructors)
{
if (name == null)
{
if (sourceProxyInterface.Name.StartsWith("I"))
name = sourceProxyInterface.Name.Substring(1) + "Proxy";
else
name = sourceProxyInterface.Name + "Proxy";
}
var compilation = semanticModel.Compilation;
// Resolve the callback contract if any
ITypeSymbol serviceContractAttributeType = compilation.RequireTypeByMetadataName("System.ServiceModel.ServiceContractAttribute");
AttributeData serviceContractAttribute = sourceProxyInterface.GetAttributes().FirstOrDefault(attr => attr.AttributeClass.Equals(serviceContractAttributeType));
if (serviceContractAttribute == null)
throw new CodeGeneratorException(sourceProxyInterface, $"The interface {sourceProxyInterface.Name} is not decorated with ServiceContractAttribute.");
ITypeSymbol callbackContractType;
var callbackContractArg = serviceContractAttribute.NamedArguments.FirstOrDefault(arg => arg.Key.Equals("CallbackContract"));
if (callbackContractArg.Key != null)
callbackContractType = callbackContractArg.Value.Value as ITypeSymbol;
else
callbackContractType = null;
// Resolve the base type (ClientBase or DuplexClientBase depending on whether a CallbackContract exists or not)
INamedTypeSymbol baseType;
if (callbackContractType != null)
{
baseType = compilation.RequireTypeByMetadataName("System.ServiceModel.DuplexClientBase`1").Construct(sourceProxyInterface);
}
else
{
baseType = compilation.RequireTypeByMetadataName("System.ServiceModel.ClientBase`1").Construct(sourceProxyInterface);
}
// Create class declaration
SyntaxNode targetClass = generator.ClassDeclaration(name, accessibility: accessibility, baseType: generator.TypeExpression(baseType), interfaceTypes: new[] { generator.TypeExpression(sourceProxyInterface) });
targetClass = generator.AddWarningCommentIf(!suppressWarningComments, targetClass);
// Copy constructors from base class.
sourceConstructors = baseType.Constructors.Where(ctor => ctor.DeclaredAccessibility != Accessibility.Private).ToImmutableArray();
foreach (var baseCtor in sourceConstructors)
{
var targetCtor = generator.ConstructorDeclaration(baseCtor, baseCtor.Parameters.Select(p => generator.Argument(generator.IdentifierName(p.Name))));
targetCtor = generator.AddWarningCommentIf(!suppressWarningComments, targetCtor);
targetCtor = generator.WithAccessibility(targetCtor, ToAccessibility(constructorAccessibility));
targetClass = generator.AddMembers(targetClass, targetCtor.AddNewLineTrivia());
}
foreach (IMethodSymbol sourceMethod in GetOperationContractMethods(semanticModel.Compilation, sourceProxyInterface))
{
SyntaxNode targetMethod = generator.MethodDeclaration(sourceMethod);
targetMethod = generator.AddWarningCommentIf(!suppressWarningComments, targetMethod);
targetMethod = generator.WithModifiers(targetMethod, DeclarationModifiers.None);
bool isVoid = sourceMethod.ReturnType.SpecialType == SpecialType.System_Void;
targetMethod = targetMethod.AddNewLineTrivia().AddNewLineTrivia();
var expression = generator.InvocationExpression(
generator.MemberAccessExpression(
generator.MemberAccessExpression(
generator.BaseExpression(),
"Channel"
),
sourceMethod.Name
),
sourceMethod.Parameters.Select(p => generator.IdentifierName(p.Name)).ToArray()
);
SyntaxNode statement;
if (!isVoid)
statement = generator.ReturnStatement(expression);
else
statement = generator.ExpressionStatement(expression);
targetMethod = generator.WithStatements(targetMethod,
new[]
{
statement
}
);
targetClass = generator.AddMembers(targetClass, targetMethod.AddNewLineTrivia());
}
return (ClassDeclarationSyntax)targetClass;
}