Implemented better attribute support for C# output.
[mono.git] / mcs / tools / type-reflector / TypeDisplayerFactory.cs
1 //
2 // TypeDisplayerFactory.cs: Factory for TypeDisplayer objects
3 //
4 // Author: Jonathan Pryor (jonpryor@vt.edu)
5 //
6 // (C) 2002 Jonathan Pryor
7 //
8 // Permission is hereby granted, free of charge, to any           
9 // person obtaining a copy of this software and associated        
10 // documentation files (the "Software"), to deal in the           
11 // Software without restriction, including without limitation     
12 // the rights to use, copy, modify, merge, publish,               
13 // distribute, sublicense, and/or sell copies of the Software,    
14 // and to permit persons to whom the Software is furnished to     
15 // do so, subject to the following conditions:                    
16 //                                                                 
17 // The above copyright notice and this permission notice          
18 // shall be included in all copies or substantial portions        
19 // of the Software.                                               
20 //                                                                 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY      
22 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO         
23 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A               
24 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL      
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,      
26 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  
27 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION       
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Diagnostics;
35 using System.Reflection;
36 using System.Text;
37 using System.Text.RegularExpressions;
38
39 namespace Mono.TypeReflector
40 {
41         public sealed class TypeDisplayerFactory
42         {
43                 private static IDictionary entries = new Hashtable ();
44
45                 public static void Add (string key, Type value)
46                 {
47                         lock (entries) {
48                                 entries.Add (key, value);
49                         }
50                 }
51
52                 private static TypeDisplayer CreateInstance (Type type, TextWriter writer)
53                 {
54                         // Will work when Activator.CreateInstance works properly.
55 #if HAVE_WORKING_ACTIVATOR_CREATE_INSTANCE
56                         return (TypeDisplayer) Activator.CreateInstance (type, 
57                                         new object[]{writer});
58 #else
59                         // Look up constructor and invoke ourselves...
60                         ConstructorInfo ctor = 
61                                 type.GetConstructor(new Type[]{typeof(TextWriter)});
62                         if (ctor == null)
63                                 return null;
64                         return (TypeDisplayer) ctor.Invoke(new object[]{writer});
65 #endif
66                 }
67
68                 public static TypeDisplayer Create (string key, TextWriter writer)
69                 {
70                         try {
71                                 Type type = null;
72                                 lock (entries) {
73                                         type = (Type) entries[key];
74                                 }
75                                 if (type == null)
76                                         type = typeof (ExplicitTypeDisplayer);
77                                 return CreateInstance (type, writer);
78                         }
79                         catch {
80                         }
81                         return  null;
82                 }
83
84                 public static void Remove (string key)
85                 {
86                         lock (entries) {
87                                 entries.Remove (key);
88                         }
89                 }
90
91                 public static ICollection Keys {
92                         get {
93                                 return entries.Keys;
94                         }
95                 }
96         }
97 }
98