2 using System
.Reflection
;
3 using System
.Collections
.Generic
;
6 using System
.Windows
.Media
;
7 using System
.Windows
.Shapes
;
8 using System
.Windows
.Media
.Animation
;
9 using System
.Windows
.Controls
;
10 using System
.Windows
.Input
;
14 static int Main (string [] args
)
16 Type DO
= typeof (System
.Windows
.DependencyObject
);
17 List
<Type
> types
= new List
<Type
> (DO
.Assembly
.GetTypes ());
19 for (int i
= types
.Count
- 1; i
>= 0; i
--) {
22 else if (!types
[i
].IsSubclassOf (DO
))
24 else if (types
[i
].IsNotPublic
)
28 if (args
.Length
>= 1 && args
[0] == "generate-test-code") {
29 Console
.WriteLine (GenerateTestCode (types
));
31 return DoChecks (types
);
37 static int DoChecks (List
<Type
> types
)
40 int normal_ctor_size
= 12;
41 int normal_ctor_intptr_size
= 8;
43 foreach (Type tp
in types
) {
44 //Console.WriteLine ("Checking: " + tp.FullName);
46 mi
= tp
.GetMethod ("GetKind", BindingFlags
.NonPublic
| BindingFlags
.Instance
);
47 if (mi
== null || mi
.DeclaringType
!= tp
) {
48 Console
.WriteLine ("Error: 1. The class '{0}' does not implement 'GetKind'.", tp
.FullName
);
52 ConstructorInfo ci
= tp
.GetConstructor (Type
.EmptyTypes
);
55 if (tp
.FullName
.IndexOf ("Internal") < 0) {
56 Console
.WriteLine ("Error: 2a. The class '{0}' does not have an empty constructor.", tp
.FullName
);
59 } else if (!ci
.IsPublic
) {
60 Console
.WriteLine ("Error: 2b. The class' '{0}' empty constructor is not public.", tp
.FullName
);
62 } else if (result
== 0) {
63 MethodBody body
= ci
.GetMethodBody ();
64 byte [] il
= body
.GetILAsByteArray ();
65 if (il
.Length
!= normal_ctor_size
)
66 Console
.WriteLine ("Warning: 2c. {1,3} bytes is the size of the empty constructor of the class '{0}' (normal size = {2}).", tp
.FullName
, il
.Length
, normal_ctor_size
);
70 foreach (Type tp
in types
) {
71 ConstructorInfo ci
= tp
.GetConstructor (BindingFlags
.Instance
| BindingFlags
.NonPublic
, null, new Type
[] {typeof (IntPtr)}
, null);
74 Console
.WriteLine ("Error: 3a. The class '{0}' does not have an IntPtr constructor.", tp
.FullName
);
76 } else if (!ci
.IsAssembly
|| ci
.IsPublic
|| ci
.IsFamily
) {
77 Console
.WriteLine ("Error: 3b. The class' '{0}' IntPtr constructor is not internal.", tp
.FullName
);
79 } else if (result
== 0) {
80 MethodBody body
= ci
.GetMethodBody ();
81 byte [] il
= body
.GetILAsByteArray ();
82 if (il
.Length
!= normal_ctor_intptr_size
)
83 Console
.WriteLine ("Warning: 3c. {1,3} bytes is the size of the IntPtr constructor of the class '{0}' (normal size = {2}).", tp
.FullName
, il
.Length
, normal_ctor_intptr_size
);
89 static string GenerateTestCode (List
<Type
> types
)
91 StringBuilder result
= new StringBuilder ();
95 using System.Reflection;
96 using System.Collections.Generic;
99 using System.Windows.Media;
100 using System.Windows.Shapes;
101 using System.Windows.Media.Animation;
102 using System.Windows.Controls;
103 using System.Windows.Input;
104 using System.Windows.Ink;
106 result
.AppendLine ("class tester {");
107 result
.AppendLine ("static void Main () {");
108 result
.AppendLine ("string a,b;");
109 foreach (Type tp
in types
) {
114 if (tp
.GetConstructor (Type
.EmptyTypes
) == null) {
115 result
.AppendLine ("// Don't know how to create a " + tp
.FullName
);
119 result
.AppendLine ("try {");
120 //result.AppendLine (string.Format (" Console.WriteLine (\"Testing {0}\");", tp.FullName));
121 result
.AppendLine (string.Format (" {1} var_{0} = new {1} ();", tp
.Name
, tp
.FullName
));
122 foreach (PropertyInfo field
in tp
.GetProperties ()) {
123 Type ftp
= field
.PropertyType
;
124 string var = string.Empty
;
125 string nullvar
= string.Empty
;
126 string vartp
= ftp
.FullName
;
128 if ((field
.CanRead
&& field
.GetGetMethod (true).IsStatic
) || (field
.CanWrite
&& field
.GetSetMethod (true).IsStatic
))
131 if (ftp
== typeof(int)) {
133 } else if (ftp
== typeof(double)) {
135 } else if (ftp
== typeof (string)) {
137 } else if (ftp
== typeof (bool)) {
139 } else if (ftp
== typeof (object)) {
141 } else if (ftp
== typeof (Uri
)) {
142 var = "new Uri (\"http://www.mono-project.com\");";
143 } else if (ftp
== typeof (Point
)) {
144 var = "new Point (2, 3)";
145 } else if (ftp
== typeof (Transform
)) {
146 var = "new TranslateTransform ()";
147 } else if (ftp
.IsSubclassOf (typeof (DependencyObject
)) && !ftp
.IsAbstract
) {
148 var = "new " + ftp
.Name
+ "()";
149 } else if (ftp
.IsEnum
) {
150 var = "(" + ftp
.Name
+ ") 1";
151 } else if (ftp
== typeof (double[])) {
152 var = "new double [] {1, 2, 3}";
153 } else if (ftp
== typeof (Point
[])) {
154 var = "new Point [] {new Point (1, 2), new Point (3, 4)}";
155 } else if (ftp
== typeof (Color
)) {
156 var = "Color.FromArgb (0, 25, 25, 25)";
157 } else if (ftp
== typeof (Geometry
)) {
158 var = "new PathGeometry ()";
159 } else if (ftp
== typeof (Rect
)) {
160 var = "new Rect (1, 2, 3, 4)";
161 } else if (ftp
== typeof (Matrix
)) {
162 var = "new Matrix (1, 2, 3, 4, 5, 6)";
163 } else if (ftp
== typeof (Timeline
)) {
164 var = "new Storyboard ()";
165 } else if (ftp
== typeof (Duration
)) {
166 var = "new Duration (new TimeSpan ())";
167 } else if (ftp
== typeof (RepeatBehavior
)) {
168 var = "new RepeatBehavior (12.34)";
169 } else if (ftp
== typeof (Nullable
<TimeSpan
>)) {
171 var = "new Nullable<TimeSpan> (new TimeSpan ())";
172 vartp
= "Nullable<TimeSpan>";
173 } else if (ftp
== typeof (Nullable
<double>)) {
175 var = "new Nullable<double> (1.2)";
176 vartp
= "Nullable<double>";
177 } else if (ftp
== typeof (Nullable
<Color
>)) {
179 var = "new Nullable<Color> (Color.FromArgb (0, 25, 50, 75))";
180 vartp
= "Nullable<Color>";
181 } else if (ftp
== typeof (Nullable
<Point
>)) {
183 var = "new Nullable<Point> (new Point (25, 75))";
184 vartp
= "Nullable<Point>";
185 } else if (ftp
== typeof (Brush
)) {
186 var = "new SolidColorBrush (Color.FromArgb (0, 25, 50, 75))";
187 } else if (ftp
== typeof (KeyTime
)) {
188 var = "KeyTime.Uniform";
189 } else if (ftp
== typeof (TimeSpan
)) {
190 var = "new TimeSpan (123456)";
192 Console
.WriteLine ("//Don't know how to test a '" + ftp
.FullName
+ "'.");
195 if (var == string.Empty
)
198 if (field
.Name
== "Item")
201 if (!field
.CanWrite
&& !field
.CanRead
) {
202 Console
.WriteLine ("Can't read nor write to: " + field
.Name
);
206 if (field
.CanWrite
|| field
.CanRead
) {
207 //result.AppendLine (string.Format (" Console.WriteLine (\"Testing {0}.{1} <{2}>\");", tp.FullName, field.Name, ftp.FullName));
211 result
.AppendLine (string.Format (" {2} field_{0}_{1}_a;", tp
.Name
, field
.Name
, vartp
));
214 result
.AppendLine (string.Format (" {2} field_{0}_{1}_b;", tp
.Name
, field
.Name
, vartp
));
217 foreach (string code
in new string [] {var, nullvar}
) {
218 if (code
== string.Empty
|| code
== null)
220 bool cw
= field
.CanWrite
&& field
.GetSetMethod (true).IsPublic
; //!field.GetSetMethod (true).IsPrivate && !field.GetSetMethod (true).IsAssembly && !field.;
221 bool cr
= field
.CanRead
&& field
.GetGetMethod (true).IsPublic
; //!field.GetGetMethod (true).IsPrivate && !field.GetGetMethod (true).IsAssembly;
224 result
.AppendLine (string.Format (" field_{0}_{1}_a = {3};", tp
.Name
, field
.Name
, vartp
, code
));
225 result
.AppendLine (string.Format (" var_{0}.{1} = field_{0}_{1}_a;", tp
.Name
, field
.Name
));
228 result
.AppendLine (string.Format (" field_{0}_{1}_b = var_{0}.{1};", tp
.Name
, field
.Name
, vartp
));
232 result
.AppendLine (string.Format (" a = field_{0}_{1}_a == null ? \"<null>\" : field_{0}_{1}_a.ToString ();", tp
.Name
, field
.Name
));
233 result
.AppendLine (string.Format (" b = field_{0}_{1}_b == null ? \"<null>\" : field_{0}_{1}_b.ToString ();", tp
.Name
, field
.Name
));
235 result
.AppendLine (string.Format (" a = field_{0}_{1}_a.ToString ();", tp
.Name
, field
.Name
));
236 result
.AppendLine (string.Format (" b = field_{0}_{1}_b.ToString ();", tp
.Name
, field
.Name
));
238 result
.AppendLine (string.Format (" if (string.CompareOrdinal (a, b) != 0) {{"));
239 result
.AppendLine (string.Format (" Console.WriteLine (\"While testing {0}.{1} got:\");", tp
.FullName
, field
.Name
));
240 result
.AppendLine (string.Format (" Console.WriteLine (\" Wrote '{{0}}' and got '{{1}}'\", a, b);", tp
.Name
, field
.Name
));
241 result
.AppendLine (" }");
245 result
.AppendLine ("} catch (TypeInitializationException ex) {");
246 result
.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got:\");", tp
.FullName
));
247 result
.AppendLine (" Console.WriteLine (\" \" + ex.InnerException.Message);");
248 result
.AppendLine ("} catch (EntryPointNotFoundException ex) {");
249 result
.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got EntryPointNotFoundException:\");", tp
.FullName
));
250 result
.AppendLine (" Console.WriteLine (\" \" + ex.Message);");
251 result
.AppendLine ("} catch (Exception ex) {");
252 result
.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got:\");", tp
.FullName
));
253 result
.AppendLine (" Console.WriteLine (\" \" + ex.Message);");
254 result
.AppendLine (" Console.WriteLine (ex.StackTrace);");
255 result
.AppendLine ("}");
256 result
.AppendLine ("");
258 result
.AppendLine ("}");
259 result
.AppendLine ("}");
260 return result
.ToString ();