当前位置: 首页>>代码示例>>C#>>正文


C# PrimitiveType.Invoke方法代码示例

本文整理汇总了C#中PrimitiveType.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# PrimitiveType.Invoke方法的具体用法?C# PrimitiveType.Invoke怎么用?C# PrimitiveType.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PrimitiveType的用法示例。


在下文中一共展示了PrimitiveType.Invoke方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			// NOTE: @, multiple occurance
			var node = context.GetNode();
			while (node != null && !IsStringConcatenation(context, node as BinaryOperatorExpression))
				node = node.Parent;

			if (node == null)
				yield break;

			var expr = (BinaryOperatorExpression)node;
			var parent = expr.Parent as BinaryOperatorExpression;
			while (parent != null && parent.Operator == BinaryOperatorType.Add) {
				expr = parent;
				parent = expr.Parent as BinaryOperatorExpression;
			}

			yield return new CodeAction(context.TranslateString("Use 'string.Format()'"),
			                             script => {
				var stringType = new PrimitiveType("string");
				var formatInvocation = stringType.Invoke("Format");
				var formatLiteral = new PrimitiveExpression("");
				var counter = 0;
				var arguments = new List<Expression>();

				formatInvocation.Arguments.Add(formatLiteral);
				var concatItems = GetConcatItems(context, expr);
				bool hasVerbatimStrings = false;
				bool hasNonVerbatimStrings = false;

				foreach (var item in concatItems) {
					if (IsStringLiteral(item)) {
						var stringLiteral = (PrimitiveExpression)item;
						if (stringLiteral.LiteralValue[0] == '@') {
							hasVerbatimStrings = true;
						} else {
							hasNonVerbatimStrings = true;
						}
					}
				}
					
				var format = new StringBuilder();
				var verbatim = hasVerbatimStrings && hasNonVerbatimStrings;
				format.Append('"');
				foreach (var item in concatItems) {
					if (IsStringLiteral(item)) {
						var stringLiteral = (PrimitiveExpression)item;

						string rawLiteral;
						if (hasVerbatimStrings && hasNonVerbatimStrings) {
							rawLiteral = stringLiteral.Value.ToString().Replace("\"", "\"\"");
						} else {
							if (stringLiteral.LiteralValue[0] == '@') {
								verbatim = true;
								rawLiteral = stringLiteral.LiteralValue.Substring(2, stringLiteral.LiteralValue.Length - 3);
							} else {
								rawLiteral = stringLiteral.LiteralValue.Substring(1, stringLiteral.LiteralValue.Length - 2);
							}
						}
						format.Append(QuoteBraces(rawLiteral));
					} else {
						Expression myItem = RemoveUnnecessaryToString(item);
						string itemFormatStr = DetermineItemFormatString(ref myItem);
							
						var index = IndexOf(arguments, myItem);
						if (index == -1) {
							// new item
							formatInvocation.Arguments.Add(myItem.Clone());
							arguments.Add(item);
							format.Append("{" + counter++ + itemFormatStr + "}");
						} else {
							// existing item
							format.Append("{" + index + itemFormatStr + "}");
						}
					}
				}
				format.Append('"');
				if (verbatim)
					format.Insert(0, '@');
				formatLiteral.SetValue(format.ToString(), format.ToString());
				if (arguments.Count > 0)
					script.Replace(expr, formatInvocation);
				else
					script.Replace(expr, formatLiteral);
			}, node);
		}
开发者ID:sphynx79,项目名称:dotfiles,代码行数:86,代码来源:UseStringFormatAction.cs


注:本文中的PrimitiveType.Invoke方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。