/// <summary> /// Create a local declaration statement which declare a local variable and assign it a complex reference. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="reference">Value of the variable.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, Type type, VariableReference reference, bool useVarKeyword = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (reference == null) { throw new ArgumentNullException(nameof(reference)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClauseFactory.GetEqualsValueClause(reference).WithEqualsToken(Token(SyntaxKind.EqualsToken))))))); }
/// <summary> /// Create the local declaration statement syntax to declare a local variable and assign it a string value. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="value">Value to assign variable.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, string value, bool useVarKeyword = true) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(typeof(string))) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClauseFactory.GetEqualsValueClause($@"""{value}""").WithEqualsToken(Token(SyntaxKind.EqualsToken))))))); }