本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.CSharpOutputVisitor.VisitParameterDeclaration方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpOutputVisitor.VisitParameterDeclaration方法的具体用法?C# CSharpOutputVisitor.VisitParameterDeclaration怎么用?C# CSharpOutputVisitor.VisitParameterDeclaration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.CSharp.CSharpOutputVisitor
的用法示例。
在下文中一共展示了CSharpOutputVisitor.VisitParameterDeclaration方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateCode
protected override string GenerateCode(ITypeDefinition currentClass)
{
List<PropertyOrFieldWrapper> filtered = parameterList
.Where(p => p.IsIncluded)
.OrderBy(p => p.Index)
.ToList();
var test = refactoringContext.GetNode();
var insertedConstructor = refactoringContext.GetNode().PrevSibling as ConstructorDeclaration;
if (insertedConstructor == null)
{
// We are not inside of a constructor declaration
return null;
}
using (Script script = refactoringContext.StartScript()) {
BlockStatement originalCtorBody = insertedConstructor.Body;
foreach (PropertyOrFieldWrapper w in filtered) {
if (w.AddCheckForNull) {
// true = reference, null = generic or unknown
if (w.Type.IsReferenceType != false)
script.AddTo(originalCtorBody,
new IfElseStatement(
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)),
new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
)
);
else
script.AddTo(originalCtorBody,
new IfElseStatement(
new UnaryOperatorExpression(UnaryOperatorType.Not, new MemberReferenceExpression(new IdentifierExpression(w.MemberName), "HasValue")),
new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
)
);
}
if (w.AddRangeCheck) {
script.AddTo(originalCtorBody,
new IfElseStatement(
new BinaryOperatorExpression(
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.LessThan, new IdentifierExpression("lower")),
BinaryOperatorType.ConditionalOr,
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper"))
),
new ThrowStatement(
new ObjectCreateExpression(
new SimpleType("ArgumentOutOfRangeException"),
new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"'), new IdentifierExpression(w.ParameterName), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Add, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Add, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Add, new IdentifierExpression("upper")))) }
)
)
)
);
}
}
foreach (PropertyOrFieldWrapper w in filtered) {
script.AddTo(originalCtorBody,
new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName)))
);
}
}
AnchorElement parameterListElement = insertionContext.ActiveElements
.OfType<AnchorElement>()
.FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));
if (parameterListElement != null) {
StringBuilder pList = new StringBuilder();
var parameters = filtered
.Select(p => new ParameterDeclaration(refactoringContext.CreateShortType(p.Type), p.ParameterName))
.ToList();
using (StringWriter textWriter = new StringWriter(pList)) {
// Output parameter list as string
var formattingOptions = FormattingOptionsFactory.CreateMono();
CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(textWriter, formattingOptions);
for (int i = 0; i < parameters.Count; i++) {
if (i > 0)
textWriter.Write(",");
outputVisitor.VisitParameterDeclaration(parameters[i]);
}
}
parameterListElement.Text = pList.ToString();
}
return null;
}