本文整理汇总了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 ());
}
示例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);
}