本文整理汇总了C#中TypeReferenceExpression.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# TypeReferenceExpression.Clone方法的具体用法?C# TypeReferenceExpression.Clone怎么用?C# TypeReferenceExpression.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeReferenceExpression
的用法示例。
在下文中一共展示了TypeReferenceExpression.Clone方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run (RefactoringContext context)
{
var switchStatement = GetSwitchStatement (context);
var result = context.Resolve (switchStatement.Expression);
var type = result.Type;
var newSwitch = (SwitchStatement)switchStatement.Clone ();
var target = new TypeReferenceExpression (context.CreateShortType (result.Type.Resolve (context.TypeResolveContext)));
foreach (var field in type.GetFields (context.TypeResolveContext)) {
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);
}
}
示例2: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var switchStatement = GetSwitchStatement(context);
if (switchStatement == null) {
yield break;
}
var result = context.Resolve(switchStatement.Expression);
if (result.Type.Kind != TypeKind.Enum) {
yield break;
}
yield return new CodeAction (context.TranslateString("Create switch labels"), script => {
var type = result.Type;
var newSwitch = (SwitchStatement)switchStatement.Clone();
var target = new TypeReferenceExpression (context.CreateShortType(result.Type));
foreach (var field in type.GetFields ()) {
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")))
}
});
script.Replace(switchStatement, newSwitch);
}, switchStatement);
}