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


C# RefactoringContext.GetNameProposal方法代码示例

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


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

示例1: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var initializer = context.GetNode<VariableInitializer>();
			if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) {
				yield break;
			}
			var type = initializer.Parent.Parent as TypeDeclaration;
			if (type == null) {
				yield break;
			}
			foreach (var member in type.Members) {
				if (member is PropertyDeclaration && ContainsGetter((PropertyDeclaration)member, initializer)) {
					yield break;
				}
			}
			var field = initializer.Parent as FieldDeclaration;
			if (field == null || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) {
				yield break;
			}
			var resolveResult = context.Resolve(initializer) as MemberResolveResult;
			if (resolveResult == null)
				yield break;
			yield return new CodeAction(context.TranslateString("Create property"), script => {
				var fieldName = context.GetNameProposal(initializer.Name, true);
				if (initializer.Name == context.GetNameProposal(initializer.Name, false)) {
					script.Rename(resolveResult.Member, fieldName);
				}
				script.InsertWithCursor(
					context.TranslateString("Create property"),
					Script.InsertPosition.After, GeneratePropertyDeclaration(context, field, fieldName));
			});
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:32,代码来源:GeneratePropertyAction.cs

示例2: Run

		public void Run (RefactoringContext context)
		{
			var property = context.GetNode<PropertyDeclaration> ();
			
			string backingStoreName = context.GetNameProposal (property.Name);
			
			// create field
			var backingStore = new FieldDeclaration ();
			backingStore.ReturnType = property.ReturnType.Clone ();
			
			var initializer = new VariableInitializer (backingStoreName);
			backingStore.Variables.Add (initializer);
			
			// create new property & implement the get/set bodies
			var newProperty = (PropertyDeclaration)property.Clone ();
			var id1 = new IdentifierExpression (backingStoreName);
			var id2 = new IdentifierExpression (backingStoreName);
			newProperty.Getter.Body = new BlockStatement () {
				new ReturnStatement (id1)
			};
			newProperty.Setter.Body = new BlockStatement () {
				new ExpressionStatement (new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value")))
			};
			
			using (var script = context.StartScript ()) {
				script.Replace (property, newProperty);
				script.InsertBefore (property, backingStore);
				script.Link (initializer, id1, id2);
			}
		}
开发者ID:yayanyang,项目名称:monodevelop,代码行数:30,代码来源:CreateBackingStore.cs

示例3: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var property = context.GetNode<PropertyDeclaration> ();
			if (property == null || !property.NameToken.Contains(context.Location))
				yield break;

			if (!IsNotImplemented (context, property.Getter.Body) ||
			    !IsNotImplemented (context, property.Setter.Body)) {
				yield break;
			}
			
			yield return new CodeAction(context.TranslateString("Implement property"), script => {
				string backingStoreName = context.GetNameProposal (property.Name);
				
				// create field
				var backingStore = new FieldDeclaration ();
				if (property.Modifiers.HasFlag (Modifiers.Static))
					backingStore.Modifiers |= Modifiers.Static;

				if (property.Setter.IsNull)
					backingStore.Modifiers |= Modifiers.Readonly;
				
				backingStore.ReturnType = property.ReturnType.Clone ();
				
				var initializer = new VariableInitializer (backingStoreName);
				backingStore.Variables.Add (initializer);
				
				// create new property & implement the get/set bodies
				var newProperty = (PropertyDeclaration)property.Clone ();
				Expression id1;
				if (backingStoreName == "value")
					id1 = new ThisReferenceExpression().Member("value");
				else
					id1 = new IdentifierExpression (backingStoreName);
				Expression id2 = id1.Clone();
				newProperty.Getter.Body = new BlockStatement () {
					new ReturnStatement (id1)
				};
				if (!property.Setter.IsNull) {
					newProperty.Setter.Body = new BlockStatement () {
						new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value"))
					};
				}
				
				script.Replace (property, newProperty);
				script.InsertBefore (property, backingStore);
				if (!property.Setter.IsNull)
					script.Link (initializer, id1, id2);
				else
					script.Link (initializer, id1);
			}, property.NameToken);
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:52,代码来源:ImplementNotImplementedProperty.cs

示例4: GeneratePropertyDeclaration

		static PropertyDeclaration GeneratePropertyDeclaration (RefactoringContext context, FieldDeclaration field, VariableInitializer initializer)
		{
			var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
			if (field.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Static))
				mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static;
			
			return new PropertyDeclaration () {
				Modifiers = mod,
				Name = context.GetNameProposal (initializer.Name, false),
				ReturnType = field.ReturnType.Clone (),
				Getter = new Accessor () {
					Body = new BlockStatement () {
						new ReturnStatement (new IdentifierExpression (initializer.Name))
					}
				}
			};
		}
开发者ID:tapenjoyGame,项目名称:ILSpy,代码行数:17,代码来源:GenerateGetter.cs

示例5: GetActions

		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var property = context.GetNode<PropertyDeclaration>();
			if (!(property != null &&
				!property.Getter.IsNull && !property.Setter.IsNull && // automatic properties always need getter & setter
				property.Getter.Body.IsNull &&
				property.Setter.Body.IsNull)) {
				yield break;
			}
			
			yield return new CodeAction(context.TranslateString("Create backing store"), script => {
				string backingStoreName = context.GetNameProposal (property.Name);
				
				// create field
				var backingStore = new FieldDeclaration ();
				if (property.Modifiers.HasFlag (Modifiers.Static))
					backingStore.Modifiers |= Modifiers.Static;
				backingStore.ReturnType = property.ReturnType.Clone ();
				
				var initializer = new VariableInitializer (backingStoreName);
				backingStore.Variables.Add (initializer);
				
				// create new property & implement the get/set bodies
				var newProperty = (PropertyDeclaration)property.Clone ();
				Expression id1;
				if (backingStoreName == "value")
					id1 = new ThisReferenceExpression().Member("value");
				else
					id1 = new IdentifierExpression (backingStoreName);
				Expression id2 = id1.Clone();
				newProperty.Getter.Body = new BlockStatement () {
					new ReturnStatement (id1)
				};
				newProperty.Setter.Body = new BlockStatement () {
					new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value"))
				};
				
				script.Replace (property, newProperty);
				script.InsertBefore (property, backingStore);
				script.Link (initializer, id1, id2);
			});
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:42,代码来源:CreateBackingStoreAction.cs

示例6: GeneratePropertyDeclaration

		static PropertyDeclaration GeneratePropertyDeclaration (RefactoringContext context, FieldDeclaration field, string fieldName)
		{
			var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
			if (field.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Static))
				mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static;
			
			return new PropertyDeclaration () {
				Modifiers = mod,
				Name = context.GetNameProposal (fieldName, false),
				ReturnType = field.ReturnType.Clone (),
				Getter = new Accessor () {
					Body = new BlockStatement () {
						new ReturnStatement (new IdentifierExpression (fieldName))
					}
				},
				Setter = new Accessor () {
					Body = new BlockStatement () {
						new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (fieldName), new IdentifierExpression ("value")))
					}
				}
			};
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:22,代码来源:GeneratePropertyAction.cs

示例7: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			Expression node = context.GetNode<IdentifierExpression>();
			if (node == null) {
				var mr = context.GetNode<MemberReferenceExpression>();
				if (mr == null || !mr.MemberNameToken.IsInside(context.Location))
					yield break;
				node = mr;
			}
			if (node == null)
				yield break;
			var rr = context.Resolve(node) as MethodGroupResolveResult;
			if (rr == null || rr.IsError)
				yield break;
			var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate);
			if (type == null)
				yield break;
			var invocationMethod = type.GetDelegateInvokeMethod();
			if (invocationMethod == null)
				yield break;

			yield return new CodeAction(
				context.TranslateString("Convert to lambda expression"), 
				script => {
					var invocation = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name))));
					var lambda = new LambdaExpression {
						Body = invocation
					};
					lambda.Parameters.AddRange(
						invocation.Arguments
							.Cast<IdentifierExpression>()
							.Select(p => new ParameterDeclaration { Name = p.Identifier })
					);
					script.Replace(node, lambda);
				},
				node
			);
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:38,代码来源:ConvertMethodGroupToLambdaAction.cs


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