ScriptStack 1.0.5
Loading...
Searching...
No Matches
Routine.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
7
9{
10
69 public class Routine
70 {
71
72 #region Private Variables
73
74 private string name;
75 private List<Type> parameters;
76 private Type result;
77 private string description;
78 private Host host;
79
80 #endregion
81
82 #region Private Methods
83
84 private void Validate(Type type)
85 {
86
87 if (type == null || type == typeof(void))
88 return;
89
90 if (type != typeof(int)
91 && type != typeof(float)
92 && type != typeof(bool)
93 && type != typeof(double)
94 && type != typeof(decimal)
95 && type != typeof(string)
96 && type != typeof(char)
97 && type != typeof(ArrayList)
98 )
99 throw new ExecutionException("Der Typ '" + type.Name + "' ist kein generischer Datentyp und es wurden keine Erweiterungen registriert.");
100
101 }
102
103 private string ToString(Type type)
104 {
105
106 string tmp = "";
107
108 if (type == null)
109 tmp = "null";
110 else if (type == typeof(void))
111 tmp = "void";
112 else if (type == typeof(int))
113 tmp = "int";
114 else if (type == typeof(float))
115 tmp = "float";
116 else if (type == typeof(double))
117 tmp = "double";
118 else if (type == typeof(bool))
119 tmp = "bool";
120 else if (type == typeof(char))
121 tmp = "char";
122 else if (type == typeof(string))
123 tmp = "string";
124 else if (type == typeof(ArrayList))
125 tmp = "array";
126 else
127 tmp = type.ToString().Replace("System.", "").ToLower();
128
129 return tmp;
130
131 }
132
133 #endregion
134
135 #region Public Methods
136
137 public Routine(Type result, string name, List<Type> parameters) {
138
140
141 foreach (Type parameter in parameters)
142 Validate(parameter);
143
144 this.result = result;
145 this.name = name;
146 this.parameters = parameters;
147
148 host = null;
149
150 }
151
152 public Routine(Type result, string name, List<Type> parameterTypes, string description)
153 {
154
156
157 foreach (Type parameter in parameterTypes)
158 Validate(parameter);
159
160 this.result = result;
161 this.name = name;
162 this.parameters = parameterTypes;
163 this.description = description;
164 host = null;
165
166 }
167
168 public Routine(string name) : this(null, name, new List<Type>())
169 {
170 }
171
172 public Routine(string name, string description) : this(null, name, new List<Type>(), description)
173 {
174 }
175
176 public Routine(Type result, string name) : this(result, name, new List<Type>())
177 {
178 }
179
180 public Routine(Type result, string name, string description) : this(result, name, new List<Type>(), description)
181 {
182 }
183
184 public Routine(Type result, string name, Type parameter) : this(result, name, new List<Type>())
185 {
186 parameters.Add(parameter);
187 }
188
189 public Routine(Type result, string name, Type parameter, string description) : this(result, name, new List<Type>(), description)
190 {
191 parameters.Add(parameter);
192 }
193
194 public Routine(Type result, string name, Type parameter0, Type parameter1) : this(result, name, new List<Type>())
195 {
196 parameters.Add(parameter0);
197 parameters.Add(parameter1);
198 }
199
200 public Routine(Type result, string name, Type parameter0, Type parameter1, string description) : this(result, name, new List<Type>(), description)
201 {
202 parameters.Add(parameter0);
203 parameters.Add(parameter1);
204 }
205
206 public Routine(Type result, string name, Type parameter0, Type parameter1, Type parameter2) : this(result, name, new List<Type>())
207 {
208 parameters.Add(parameter0);
209 parameters.Add(parameter1);
210 parameters.Add(parameter2);
211 }
212
213 public Routine(Type result, string name, Type parameter0, Type parameter1, Type parameter2, string description) : this(result, name, new List<Type>(), description)
214 {
215 parameters.Add(parameter0);
216 parameters.Add(parameter1);
217 parameters.Add(parameter2);
218 }
219
224 public void Verify(List<object> parameters)
225 {
226
227 if (parameters.Count != this.parameters.Count)
228 throw new ExecutionException("Die Routine '" + name + "' wurde mit " + parameters.Count + " statt erwarteten " + this.parameters.Count + " Parametern aufgerufen.");
229
230 for (int i = 0; i < parameters.Count; i++)
231 {
232
233 if (null == this.parameters[i] || null == parameters[i])
234 continue;
235
236 if (typeof(void) == this.parameters[i] || typeof(void) == parameters[i])
237 continue;
238
239 Type expected = this.parameters[i];
240
241 Type specified = parameters[i].GetType();
242
243 if (expected != specified)
244 throw new ExecutionException("Typ '" + specified.Name + "' statt erwartetem Typ '" + expected.Name + "' als " + (i + 1) + " Parameter von '" + name +"' angegeben.");
245
246 }
247
248 }
249
254 public void Verify(object result)
255 {
256
257 if (null == this.result || null == result)
258 return;
259
260 if (typeof(void) == this.result || typeof(void) == result)
261 return;
262
263 if (result.GetType() != this.result)
264 throw new ExecutionException("Typ '" + result.GetType().Name + "' statt erwartetem Typ '" + this.result.Name + "' als Ergebnis von '" + name + "' erhalten.");
265
266 }
267
268 public override string ToString()
269 {
270
271 StringBuilder sb = new StringBuilder();
272
273 //if(result != (Type)null)
274 sb.Append(ToString(result) + " ");
275
276 sb.Append(name);
277
278 sb.Append("(");
279
280 int i = 0;
281
282 for (i = 0; i < parameters.Count; i++)
283 {
284
285 if (i > 0)
286 sb.Append(", ");
287
288 sb.Append(ToString(parameters[i]));
289
290 }
291
292 sb.Append(")");
293
294 return sb.ToString();
295
296 }
297
298 public string Description()
299 {
300 return description;
301 }
302
303 #endregion
304
305 #region Public Properties
306
307 public string Name
308 {
309 get { return name; }
310 }
311
312 public List<Type> ParameterTypes
313 {
314 get { return parameters; }
315 }
316
317 public Type Result
318 {
319 get { return result; }
320 }
321
323 {
324 get { return host; }
325 internal set { host = value; }
326 }
327
328 #endregion
329
330 }
331
332}
333
string ToString(Type type)
Definition Routine.cs:103
void Verify(List< object > parameters)
Verify the parameter types of a Routine. If null or void was specified values arent verified.
Definition Routine.cs:224
Routine(Type result, string name, Type parameter)
Definition Routine.cs:184
Routine(Type result, string name, Type parameter0, Type parameter1, string description)
Definition Routine.cs:200
Routine(Type result, string name, List< Type > parameterTypes, string description)
Definition Routine.cs:152
void Verify(object result)
Verify the result of a Routine. If null or void was specified values arent verified.
Definition Routine.cs:254
void Validate(Type type)
Definition Routine.cs:84
Routine(Type result, string name, Type parameter0, Type parameter1)
Definition Routine.cs:194
Routine(Type result, string name)
Definition Routine.cs:176
Routine(string name, string description)
Definition Routine.cs:172
Routine(Type result, string name, Type parameter0, Type parameter1, Type parameter2)
Definition Routine.cs:206
Routine(Type result, string name, Type parameter0, Type parameter1, Type parameter2, string description)
Definition Routine.cs:213
List< Type > parameters
Definition Routine.cs:75
List< Type > ParameterTypes
Definition Routine.cs:313
Routine(Type result, string name, Type parameter, string description)
Definition Routine.cs:189
Routine(Type result, string name, string description)
Definition Routine.cs:180
Routine(Type result, string name, List< Type > parameters)
Definition Routine.cs:137
override string ToString()
Definition Routine.cs:268
The main interface to create a Host. A Host can implement Routine's to extend its functionality.
Definition Host.cs:80