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


C# RefactoringContext.GetDefinition方法代码示例

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


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

示例1: GetAnonymousMethodExpression

		static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out ITypeDefinition delegateType)
		{
			delegateType = null;
			
			var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
			if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
				return null;
			
			AstType resolvedType = null;
			var parent = anonymousMethodExpression.Parent;
			if (parent is AssignmentExpression) {
				resolvedType = context.ResolveType (((AssignmentExpression)parent).Left);
			} else if (parent is VariableDeclarationStatement) {
				resolvedType = context.ResolveType (((VariableDeclarationStatement)parent).Type);
			} else if (parent is InvocationExpression) {
				// TODO: handle invocations
			}
			
			if (resolvedType == null)
				return null;
			delegateType = context.GetDefinition (resolvedType);
			if (delegateType == null || delegateType.ClassType != ClassType.Delegate) 
				return null;
			
			return anonymousMethodExpression;
		}
开发者ID:hduregger,项目名称:monodevelop,代码行数:26,代码来源:InsertAnonymousMethodSignature.cs

示例2: IsValid

		public bool IsValid (RefactoringContext context)
		{
			var switchStatement = GetSwitchStatement (context);
			if (switchStatement == null)
				return false;
			var result = context.ResolveType (switchStatement.Expression);
			if (result == null)
				return false;
			var type = context.GetDefinition (result);
			return type != null && type.ClassType == ClassType.Enum;
		}
开发者ID:hduregger,项目名称:monodevelop,代码行数:11,代码来源:GenerateSwitchLabels.cs

示例3: Run

		public void Run (RefactoringContext context)
		{
			VariableInitializer initializer;
			var eventDeclaration = GetEventDeclaration (context, out initializer);
			var type = context.GetDefinition (context.ResolveType (eventDeclaration.ReturnType));
			if (type == null)
				return;
			var invokeMethod = type.Methods.Where (m => m.Name == "Invoke").FirstOrDefault ();
			if (invokeMethod == null)
				return;
			
			bool hasSenderParam = false;
			IEnumerable<IParameter> pars = invokeMethod.Parameters;
			if (invokeMethod.Parameters.Any ()) {
				var first = invokeMethod.Parameters [0];
				if (first.Name == "sender" /*&& first.Type == "System.Object"*/) {
					hasSenderParam = true;
					pars = invokeMethod.Parameters.Skip (1);
				}
			}
			const string handlerName = "handler";
					
			var arguments = new List<Expression> ();
			if (hasSenderParam)
				arguments.Add (new ThisReferenceExpression ());
			foreach (var par in pars)
				arguments.Add (new IdentifierExpression (par.Name));
			
			var methodDeclaration = new MethodDeclaration () {
				Name = "On" + initializer.Name,
				ReturnType = context.CreateShortType (eventDeclaration.ReturnType),
				Modifiers = ICSharpCode.NRefactory.CSharp.Modifiers.Protected | ICSharpCode.NRefactory.CSharp.Modifiers.Virtual,
				Body = new BlockStatement () {
					new VariableDeclarationStatement (context.CreateShortType (eventDeclaration.ReturnType), handlerName, new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name)),
					new IfElseStatement () {
						Condition = new BinaryOperatorExpression (new IdentifierExpression (handlerName), BinaryOperatorType.InEquality, new PrimitiveExpression (null)),
						TrueStatement = new ExpressionStatement (new InvocationExpression (new IdentifierExpression (handlerName), arguments))
					}
				}
			};
			
			foreach (var par in pars) {
				var typeName = context.CreateShortType (par.Type.Resolve (context.TypeResolveContext));
				var decl = new ParameterDeclaration (typeName, par.Name);
				methodDeclaration.Parameters.Add (decl);
			}
			
			using (var script = context.StartScript ()) {
				script.InsertWithCursor ("Create event invocator", methodDeclaration, Script.InsertPosition.After);
			}
		}
开发者ID:hduregger,项目名称:monodevelop,代码行数:51,代码来源:CreateEventInvocator.cs

示例4: Run

		public void Run (RefactoringContext context)
		{
			var switchStatement = GetSwitchStatement (context);
			
			var result = context.ResolveType (switchStatement.Expression);
			var type = context.GetDefinition (result);
			
			var newSwitch = (SwitchStatement)switchStatement.Clone ();
			
			var target = new TypeReferenceExpression (context.CreateShortType (result));
			foreach (var field in type.Fields) {
				if (field.IsSynthetic || !field.IsConst)
					continue;
				newSwitch.SwitchSections.Add (new SwitchSection () {
					CaseLabels = {
						new CaseLabel (new MemberReferenceExpression (target.Clone (), field.Name))
					},
					Statements = {
						new BreakStatement ()
					}
				});
			}
			
			newSwitch.SwitchSections.Add (new SwitchSection () {
				CaseLabels = {
					new CaseLabel ()
				},
				Statements = {
					new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System.ArgumentOutOfRangeException")))
				}
			});
			
			using (var script = context.StartScript ()) {
				script.Replace (switchStatement, newSwitch);
			}
		}
开发者ID:hduregger,项目名称:monodevelop,代码行数:36,代码来源:GenerateSwitchLabels.cs


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