5
5
#if FEATURE_COM
6
6
7
7
using System ;
8
- using System . Collections ;
8
+ using System . Collections . Concurrent ;
9
9
using System . Collections . Generic ;
10
+ using System . Globalization ;
10
11
using System . Runtime . InteropServices . ComTypes ;
11
12
using System . Threading ;
12
13
13
14
namespace Microsoft . Scripting . ComInterop {
14
15
15
16
public class ComTypeDesc : ComTypeLibMemberDesc {
16
- private string _typeName ;
17
- private string _documentation ;
18
- //Hashtable is threadsafe for multiple readers single writer.
19
- //Enumerating and writing is mutually exclusive so require locking.
20
- private Hashtable _funcs ;
21
- private Hashtable _puts ;
22
- private Hashtable _putRefs ;
17
+ private readonly string _typeName ;
18
+ private readonly string _documentation ;
23
19
private ComMethodDesc _getItem ;
24
20
private ComMethodDesc _setItem ;
25
- private Dictionary < string , ComEventDesc > _events ;
26
21
private static readonly Dictionary < string , ComEventDesc > _EmptyEventsDict = new Dictionary < string , ComEventDesc > ( ) ;
27
22
28
23
internal ComTypeDesc ( ITypeInfo typeInfo , ComType memberType , ComTypeLibDesc typeLibDesc ) : base ( memberType ) {
@@ -51,122 +46,101 @@ internal static ComTypeDesc FromITypeInfo(ITypeInfo typeInfo, TYPEATTR typeAttr)
51
46
52
47
internal static ComTypeDesc CreateEmptyTypeDesc ( ) {
53
48
ComTypeDesc typeDesc = new ComTypeDesc ( null , ComType . Interface , null ) ;
54
- typeDesc . _funcs = new Hashtable ( ) ;
55
- typeDesc . _puts = new Hashtable ( ) ;
56
- typeDesc . _putRefs = new Hashtable ( ) ;
57
- typeDesc . _events = _EmptyEventsDict ;
49
+ typeDesc . Funcs = new ConcurrentDictionary < string , ComMethodDesc > ( ) ;
50
+ typeDesc . Puts = new ConcurrentDictionary < string , ComMethodDesc > ( ) ;
51
+ typeDesc . PutRefs = new ConcurrentDictionary < string , ComMethodDesc > ( ) ;
52
+ typeDesc . Events = _EmptyEventsDict ;
58
53
59
54
return typeDesc ;
60
55
}
61
56
62
- internal static Dictionary < string , ComEventDesc > EmptyEvents {
63
- get { return _EmptyEventsDict ; }
64
- }
57
+ internal static Dictionary < string , ComEventDesc > EmptyEvents => _EmptyEventsDict ;
65
58
66
- internal Hashtable Funcs {
67
- get { return _funcs ; }
68
- set { _funcs = value ; }
69
- }
59
+ internal ConcurrentDictionary < string , ComMethodDesc > Funcs { get ; set ; }
70
60
71
- internal Hashtable Puts {
72
- get { return _puts ; }
73
- set { _puts = value ; }
74
- }
61
+ internal ConcurrentDictionary < string , ComMethodDesc > Puts { get ; set ; }
75
62
76
- internal Hashtable PutRefs {
77
- set { _putRefs = value ; }
78
- }
63
+ internal ConcurrentDictionary < string , ComMethodDesc > PutRefs { get ; set ; }
79
64
80
- internal Dictionary < string , ComEventDesc > Events {
81
- get { return _events ; }
82
- set { _events = value ; }
83
- }
65
+ internal Dictionary < string , ComEventDesc > Events { get ; set ; }
84
66
85
67
internal bool TryGetFunc ( string name , out ComMethodDesc method ) {
86
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
87
- if ( _funcs . ContainsKey ( name ) ) {
88
- method = _funcs [ name ] as ComMethodDesc ;
68
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
69
+ if ( Funcs . TryGetValue ( name , out method ) ) {
89
70
return true ;
90
71
}
91
- method = null ;
72
+
92
73
return false ;
93
74
}
94
75
95
76
internal void AddFunc ( string name , ComMethodDesc method ) {
96
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
97
- lock ( _funcs ) {
98
- _funcs [ name ] = method ;
99
- }
77
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
78
+ Funcs [ name ] = method ;
100
79
}
101
80
102
81
internal bool TryGetPut ( string name , out ComMethodDesc method ) {
103
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
104
- if ( _puts . ContainsKey ( name ) ) {
105
- method = _puts [ name ] as ComMethodDesc ;
82
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
83
+ if ( Puts . TryGetValue ( name , out method ) ) {
106
84
return true ;
107
85
}
108
- method = null ;
86
+
109
87
return false ;
110
88
}
111
89
112
90
internal void AddPut ( string name , ComMethodDesc method ) {
113
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
114
- lock ( _puts ) {
115
- _puts [ name ] = method ;
116
- }
91
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
92
+ Puts [ name ] = method ;
117
93
}
118
94
119
95
internal bool TryGetPutRef ( string name , out ComMethodDesc method ) {
120
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
121
- if ( _putRefs . ContainsKey ( name ) ) {
122
- method = _putRefs [ name ] as ComMethodDesc ;
96
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
97
+ if ( PutRefs . TryGetValue ( name , out method ) ) {
123
98
return true ;
124
99
}
125
- method = null ;
100
+
126
101
return false ;
127
102
}
128
103
internal void AddPutRef ( string name , ComMethodDesc method ) {
129
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
130
- lock ( _putRefs ) {
131
- _putRefs [ name ] = method ;
132
- }
104
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
105
+ PutRefs [ name ] = method ;
106
+
133
107
}
134
108
135
109
internal bool TryGetEvent ( string name , out ComEventDesc @event ) {
136
- name = name . ToUpper ( System . Globalization . CultureInfo . InvariantCulture ) ;
137
- return _events . TryGetValue ( name , out @event ) ;
110
+ name = name . ToUpper ( CultureInfo . InvariantCulture ) ;
111
+ return Events . TryGetValue ( name , out @event ) ;
138
112
}
139
113
140
114
internal string [ ] GetMemberNames ( bool dataOnly ) {
141
115
var names = new Dictionary < string , object > ( ) ;
142
116
143
- lock ( _funcs ) {
144
- foreach ( ComMethodDesc func in _funcs . Values ) {
117
+ lock ( Funcs ) {
118
+ foreach ( ComMethodDesc func in Funcs . Values ) {
145
119
if ( ! dataOnly || func . IsDataMember ) {
146
120
names . Add ( func . Name , null ) ;
147
121
}
148
122
}
149
123
}
150
124
151
125
if ( ! dataOnly ) {
152
- lock ( _puts ) {
153
- foreach ( ComMethodDesc func in _puts . Values ) {
126
+ lock ( Puts ) {
127
+ foreach ( ComMethodDesc func in Puts . Values ) {
154
128
if ( ! names . ContainsKey ( func . Name ) ) {
155
129
names . Add ( func . Name , null ) ;
156
130
}
157
131
}
158
132
}
159
133
160
- lock ( _putRefs ) {
161
- foreach ( ComMethodDesc func in _putRefs . Values ) {
134
+ lock ( PutRefs ) {
135
+ foreach ( ComMethodDesc func in PutRefs . Values ) {
162
136
if ( ! names . ContainsKey ( func . Name ) ) {
163
137
names . Add ( func . Name , null ) ;
164
138
}
165
139
}
166
140
}
167
141
168
- if ( _events != null && _events . Count > 0 ) {
169
- foreach ( string name in _events . Keys ) {
142
+ if ( Events != null && Events . Count > 0 ) {
143
+ foreach ( string name in Events . Keys ) {
170
144
if ( ! names . ContainsKey ( name ) ) {
171
145
names . Add ( name , null ) ;
172
146
}
@@ -180,30 +154,22 @@ internal string[] GetMemberNames(bool dataOnly) {
180
154
}
181
155
182
156
// this property is public - accessed by an AST
183
- public string TypeName {
184
- get { return _typeName ; }
185
- }
157
+ public string TypeName => _typeName ;
186
158
187
- internal string Documentation {
188
- get { return _documentation ; }
189
- }
159
+ internal string Documentation => _documentation ;
190
160
191
161
// this property is public - accessed by an AST
192
162
public ComTypeLibDesc TypeLib { get ; }
193
163
194
164
internal Guid Guid { get ; set ; }
195
165
196
- internal ComMethodDesc GetItem {
197
- get { return _getItem ; }
198
- }
166
+ internal ComMethodDesc GetItem => _getItem ;
199
167
200
168
internal void EnsureGetItem ( ComMethodDesc candidate ) {
201
169
Interlocked . CompareExchange ( ref _getItem , candidate , null ) ;
202
170
}
203
171
204
- internal ComMethodDesc SetItem {
205
- get { return _setItem ; }
206
- }
172
+ internal ComMethodDesc SetItem => _setItem ;
207
173
208
174
internal void EnsureSetItem ( ComMethodDesc candidate ) {
209
175
Interlocked . CompareExchange ( ref _setItem , candidate , null ) ;
0 commit comments