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


C# MethodDeclaration.ReplaceWith方法代码示例

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


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

示例1: VisitMethodDeclaration

			public override void VisitMethodDeclaration (MethodDeclaration methodDeclaration)
			{
				base.VisitMethodDeclaration (methodDeclaration);

				//
				// Try to force the method into a form we can handle
				//
				var m = methodDeclaration;

				var gotos = m.Body.Descendants.OfType<GotoStatement> ().ToList ();
				if (gotos.Count == 0)
					return;

				if (HasBadLabels (m)) {
					foreach (var ts in transforms) {
						m = (MethodDeclaration)methodDeclaration.Clone ();

						m.AcceptVisitor (new LabelLoops ());
						m.AcceptVisitor (new SwitchSectionBlocksToStatements ());
						foreach (var t in ts) {
							m.AcceptVisitor (t);
							m.AcceptVisitor (new RemoveRedundantGotos ());
							m.AcceptVisitor (new RemoveUnreachableStatements ());
						}
						m.AcceptVisitor (new RemoveLabelsWithoutGotos ());

						if (!HasBadLabels (m)) {
							break;
						}
					}

					if (HasBadLabels (m)) {
						App.Warning ("! GOTO labels at different levels in {0}.{1}(). This is not supported.", 
							methodDeclaration.GetParent<TypeDeclaration> ().Name, 
							methodDeclaration.Name);
						return;
					} else {
						methodDeclaration.ReplaceWith (m);
					}
				}

				//
				// Handle it
				//
				m.AcceptVisitor (new CreateGotoLoop ());
				m.AcceptVisitor (new RewriteGotos ());
			}
开发者ID:RReverser,项目名称:Netjs,代码行数:47,代码来源:CsToTs.cs

示例2: TryConverterMethodToProperty

        private void TryConverterMethodToProperty(MethodDeclaration currGetterOrSetter, MethodDefinition baseGetterOrSetter)
        {
            if (baseGetterOrSetter == null) return;
            if (!baseGetterOrSetter.IsSpecialName) return;
            if (!baseGetterOrSetter.IsGetter && !baseGetterOrSetter.IsSetter) return;

            var propName = baseGetterOrSetter.WellName;
            if (propName.StartsWith("get_") || propName.StartsWith("set_")) propName = propName.Substring(4);

            var baseProperty = baseGetterOrSetter.DeclaringType.Properties.FirstOrDefault(x => x.WellName == propName);
            if (baseProperty == null) return;

            // 将方法转换为属性:解决将属性实现为读取方法(和设置方法)的问题:
            PropertyDeclaration pdNew = new PropertyDeclaration() { Name = propName };
            if (currGetterOrSetter.PrivateImplementationType.IsNull)
            {
                // 属性重载
                pdNew.Modifiers = currGetterOrSetter.Modifiers;
            }
            else
            {
                // 属性的显示接口实现
                pdNew.PrivateImplementationType = currGetterOrSetter.PrivateImplementationType.Detach();
            }

            // 返回值类型
            var pt = baseProperty.PropertyType;
            var rt = AstBuilder.ConvertType(pt);
            rt.WithAnnotation(pt);
            pdNew.ReturnType = rt;

            // 读写方法
            var myBody = currGetterOrSetter.Body.Detach();
            if (baseGetterOrSetter.IsGetter)
                pdNew.Getter = new Accessor() { Body = myBody };
            else
                pdNew.Setter = new Accessor() { Body = myBody };

            // “姊妹”方法
            MethodDeclaration mdCompanion = null;
            var td = currGetterOrSetter.Parent as TypeDeclaration;
            if (td != null)
            {
                var companionName = (baseGetterOrSetter.IsGetter)
                    ? (baseProperty.SetMethod == null ? null : "set_" + propName)
                    : (baseProperty.GetMethod == null ? null : "get_" + propName);
                if (companionName != null)
                {
                    foreach (var x in td.DescendantNodes().OfType<MethodDeclaration>())
                    {
                        if (x.Name == companionName)
                        {
                            mdCompanion = x;
                            break;
                        }

                        var xmd = x.Annotation<MethodDefinition>();
                        if (xmd != null && xmd.HasOverrides)
                        {
                            var omd = xmd.Overrides.First();
                            if (omd.WellName == companionName)
                            {
                                mdCompanion = x;
                                break;
                            }
                        }
                    }
                }
            }
            if (mdCompanion != null)
            {
                var companionBody = mdCompanion.Body.Detach();
                if (mdCompanion.Parameters.Count == 1 && mdCompanion.Parameters.First().Name != "value")
                {
                    // 修正设置方法参数名称
                    var pn = mdCompanion.Parameters.First().Name;
                    foreach (var x in companionBody.Descendants.OfType<Identifier>().Where(x => x.Name == pn))
                    {
                        x.Name = "value";
                    }
                }
                if (baseGetterOrSetter.IsGetter)
                    pdNew.Setter = new Accessor() { Body = companionBody };
                else
                    pdNew.Getter = new Accessor() { Body = companionBody };

                DelayRemove(mdCompanion);
            }
            currGetterOrSetter.ReplaceWith(pdNew);
        }
开发者ID:DKeeper1523,项目名称:ilspy_yh,代码行数:90,代码来源:YuehanTransform.cs


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