-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeStatementExtensions.cs
105 lines (96 loc) · 5.18 KB
/
CodeStatementExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#region License
// Copyright 2004-2010 Castle Project - http:www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
namespace Castle.ActiveWriter.CodeDomExtensions
{
using System;
using System.CodeDom;
public static class CodeStatementExtensions
{
public static CodeStatement Clone(this CodeStatement statement)
{
if (statement == null) return null;
if (statement is CodeAssignStatement)
return (statement as CodeAssignStatement).Clone();
if (statement is CodeAttachEventStatement)
return (statement as CodeAttachEventStatement).Clone();
if (statement is CodeConditionStatement)
return (statement as CodeConditionStatement).Clone();
if (statement is CodeExpressionStatement)
return (statement as CodeExpressionStatement).Clone();
if (statement is CodeIterationStatement)
return (statement as CodeIterationStatement).Clone();
if (statement is CodeLabeledStatement)
return (statement as CodeLabeledStatement).Clone();
if (statement is CodeMethodReturnStatement)
return (statement as CodeMethodReturnStatement).Clone();
if (statement is CodeRemoveEventStatement)
return (statement as CodeRemoveEventStatement).Clone();
if (statement is CodeThrowExceptionStatement)
return (statement as CodeThrowExceptionStatement).Clone();
if (statement is CodeTryCatchFinallyStatement)
return (statement as CodeTryCatchFinallyStatement).Clone();
if (statement is CodeVariableDeclarationStatement)
return (statement as CodeVariableDeclarationStatement).Clone();
// The following statements don't have any types to replace, so we don't need to
// change anything about them right now. If we were doing something more
// advanced in the future, we might need to do more work here.
if (statement is CodeCommentStatement)
return statement;
if (statement is CodeGotoStatement)
return statement;
if (statement is CodeSnippetStatement)
return statement;
throw new NotImplementedException("Clone has not been implemented for statement of type: " + statement.GetType().FullName);
}
public static void ReplaceType(this CodeStatement statement, string oldType, string newType)
{
if (statement == null) return;
if (statement is CodeAssignStatement)
(statement as CodeAssignStatement).ReplaceType(oldType, newType);
else if (statement is CodeAttachEventStatement)
(statement as CodeAttachEventStatement).ReplaceType(oldType, newType);
else if (statement is CodeConditionStatement)
(statement as CodeConditionStatement).ReplaceType(oldType, newType);
else if (statement is CodeExpressionStatement)
(statement as CodeExpressionStatement).ReplaceType(oldType, newType);
else if (statement is CodeIterationStatement)
(statement as CodeIterationStatement).ReplaceType(oldType, newType);
else if (statement is CodeLabeledStatement)
(statement as CodeLabeledStatement).ReplaceType(oldType, newType);
else if (statement is CodeMethodReturnStatement)
(statement as CodeMethodReturnStatement).ReplaceType(oldType, newType);
else if (statement is CodeRemoveEventStatement)
(statement as CodeRemoveEventStatement).ReplaceType(oldType, newType);
else if (statement is CodeThrowExceptionStatement)
(statement as CodeThrowExceptionStatement).ReplaceType(oldType, newType);
else if (statement is CodeTryCatchFinallyStatement)
(statement as CodeTryCatchFinallyStatement).ReplaceType(oldType, newType);
else if (statement is CodeVariableDeclarationStatement)
(statement as CodeVariableDeclarationStatement).ReplaceType(oldType, newType);
// The following statements don't have any types to replace.
else if (statement is CodeCommentStatement)
return;
else if (statement is CodeGotoStatement)
return;
else if (statement is CodeSnippetStatement)
return;
// Unknown statement types.
else
throw new NotImplementedException("ReplaceType has not been implemented for statement of type: " + statement.GetType().FullName);
}
}
}