OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Collections; | |
5 using System.Collections.Generic; | |
6 using System.Diagnostics; | |
7 using System.Diagnostics.CodeAnalysis; | |
8 using System.Globalization; | |
9 using System.Reflection; | |
10 using System.Runtime.InteropServices; | |
11 | |
12 namespace Microsoft.VisualStudio.Project.Automation | |
13 { | |
14 /// <summary> | |
15 /// Contains all of the properties of a given object that are contained
in a generic collection of properties. | |
16 /// </summary> | |
17 [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrec
tSuffix")] | |
18 [CLSCompliant(false), ComVisible(true)] | |
19 public class OAProperties : EnvDTE.Properties | |
20 { | |
21 #region fields | |
22 private NodeProperties target; | |
23 private Dictionary<string, EnvDTE.Property> properties = new Dic
tionary<string, EnvDTE.Property>(); | |
24 #endregion | |
25 | |
26 #region properties | |
27 /// <summary> | |
28 /// Defines the NodeProperties object that contains the defines
the properties. | |
29 /// </summary> | |
30 public NodeProperties Target | |
31 { | |
32 get | |
33 { | |
34 return this.target; | |
35 } | |
36 } | |
37 | |
38 /// <summary> | |
39 /// The hierarchy node for the object which properties this item
represent | |
40 /// </summary> | |
41 public HierarchyNode Node | |
42 { | |
43 get | |
44 { | |
45 return this.Target.Node; | |
46 } | |
47 } | |
48 | |
49 /// <summary> | |
50 /// Defines a dictionary of the properties contained. | |
51 /// </summary> | |
52 public Dictionary<string, EnvDTE.Property> Properties | |
53 { | |
54 get | |
55 { | |
56 return this.properties; | |
57 } | |
58 } | |
59 #endregion | |
60 | |
61 #region ctor | |
62 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usag
e", "CA2214:DoNotCallOverridableMethodsInConstructors")] | |
63 public OAProperties(NodeProperties target) | |
64 { | |
65 System.Diagnostics.Debug.Assert(target != null); | |
66 | |
67 this.target = target; | |
68 this.AddPropertiesFromType(target.GetType()); | |
69 } | |
70 #endregion | |
71 | |
72 #region EnvDTE.Properties | |
73 /// <summary> | |
74 /// Microsoft Internal Use Only. | |
75 /// </summary> | |
76 public virtual object Application | |
77 { | |
78 get { return null; } | |
79 } | |
80 | |
81 /// <summary> | |
82 /// Gets a value indicating the number of objects in the collect
ion. | |
83 /// </summary> | |
84 public int Count | |
85 { | |
86 get { return properties.Count; } | |
87 } | |
88 | |
89 /// <summary> | |
90 /// Gets the top-level extensibility object. | |
91 /// </summary> | |
92 public virtual EnvDTE.DTE DTE | |
93 { | |
94 get | |
95 { | |
96 if(this.target == null || this.target.Node == nu
ll || this.target.Node.ProjectMgr == null || this.target.Node.ProjectMgr.IsClose
d || | |
97 this.target.Node.ProjectMgr.Site == null
) | |
98 { | |
99 throw new InvalidOperationException(); | |
100 } | |
101 return this.target.Node.ProjectMgr.Site.GetServi
ce(typeof(EnvDTE.DTE)) as EnvDTE.DTE; | |
102 } | |
103 } | |
104 | |
105 /// <summary> | |
106 /// Gets an enumeration for items in a collection. | |
107 /// </summary> | |
108 /// <returns>An enumerator. </returns> | |
109 public IEnumerator GetEnumerator() | |
110 { | |
111 if(this.properties == null) | |
112 { | |
113 yield return null; | |
114 } | |
115 | |
116 if(this.properties.Count == 0) | |
117 { | |
118 yield return new OANullProperty(this); | |
119 } | |
120 | |
121 IEnumerator enumerator = this.properties.Values.GetEnume
rator(); | |
122 | |
123 while(enumerator.MoveNext()) | |
124 { | |
125 yield return enumerator.Current; | |
126 } | |
127 } | |
128 | |
129 /// <summary> | |
130 /// Returns an indexed member of a Properties collection. | |
131 /// </summary> | |
132 /// <param name="index">The index at which to return a mamber.</
param> | |
133 /// <returns>A Property object.</returns> | |
134 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Perf
ormance", "CA1800:DoNotCastUnnecessarily")] | |
135 public virtual EnvDTE.Property Item(object index) | |
136 { | |
137 if(index is string) | |
138 { | |
139 string indexAsString = (string)index; | |
140 if(this.properties.ContainsKey(indexAsString)) | |
141 { | |
142 return (EnvDTE.Property)this.properties[
indexAsString]; | |
143 } | |
144 else | |
145 { | |
146 Trace.WriteLine("OAProperties.cs -- Item method -- did NOT find index
'" + indexAsString + "' returning null EnvDTE.Property"); | |
147 return null; | |
148 } | |
149 } | |
150 else if(index is int) | |
151 { | |
152 int realIndex = (int)index - 1; | |
153 if(realIndex >= 0 && realIndex < this.properties
.Count) | |
154 { | |
155 IEnumerator enumerator = this.properties
.Values.GetEnumerator(); | |
156 | |
157 int i = 0; | |
158 while(enumerator.MoveNext()) | |
159 { | |
160 if(i++ == realIndex) | |
161 { | |
162 return (EnvDTE.Property)
enumerator.Current; | |
163 } | |
164 } | |
165 } | |
166 } | |
167 | |
168 throw new ArgumentException(SR.GetString(SR.InvalidParam
eter, CultureInfo.CurrentUICulture), "index"); | |
169 } | |
170 /// <summary> | |
171 /// Gets the immediate parent object of a Properties collection. | |
172 /// </summary> | |
173 public virtual object Parent | |
174 { | |
175 get { return null; } | |
176 } | |
177 #endregion | |
178 | |
179 #region methods | |
180 /// <summary> | |
181 /// Add properties to the collection of properties filtering onl
y those properties which are com-visible and AutomationBrowsable | |
182 /// </summary> | |
183 /// <param name="targetType">The type of NodeProperties the we s
hould filter on</param> | |
184 protected void AddPropertiesFromType(Type targetType) | |
185 { | |
186 Debug.Assert(targetType != null); | |
187 | |
188 // If the type is not COM visible, we do not expose any
of the properties | |
189 if(!IsComVisible(targetType)) | |
190 return; | |
191 | |
192 // Add all properties being ComVisible and AutomationVis
ible | |
193 PropertyInfo[] propertyInfos = targetType.GetProperties(
); | |
194 foreach(PropertyInfo propertyInfo in propertyInfos) | |
195 { | |
196 if(!IsInMap(propertyInfo) && IsComVisible(proper
tyInfo) && IsAutomationVisible(propertyInfo)) | |
197 { | |
198 AddProperty(propertyInfo); | |
199 } | |
200 } | |
201 } | |
202 #endregion | |
203 | |
204 #region virtual methods | |
205 /// <summary> | |
206 /// Creates a new OAProperty object and adds it to the current l
ist of properties | |
207 /// </summary> | |
208 /// <param name="propertyInfo">The property to be associated wit
h an OAProperty object</param> | |
209 protected virtual void AddProperty(PropertyInfo propertyInfo) | |
210 { | |
211 this.properties.Add(propertyInfo.Name, new OAProperty(th
is, propertyInfo)); | |
212 } | |
213 #endregion | |
214 | |
215 #region helper methods | |
216 | |
217 private bool IsInMap(PropertyInfo propertyInfo) | |
218 { | |
219 return this.properties.ContainsKey(propertyInfo.Name); | |
220 } | |
221 | |
222 private static bool IsAutomationVisible(PropertyInfo propertyInf
o) | |
223 { | |
224 object[] customAttributesOnProperty = propertyInfo.GetCu
stomAttributes(typeof(AutomationBrowsableAttribute), true); | |
225 | |
226 foreach(AutomationBrowsableAttribute attr in customAttri
butesOnProperty) | |
227 { | |
228 if(!attr.Browsable) | |
229 { | |
230 return false; | |
231 } | |
232 } | |
233 return true; | |
234 } | |
235 | |
236 private static bool IsComVisible(Type targetType) | |
237 { | |
238 object[] customAttributesOnProperty = targetType.GetCust
omAttributes(typeof(ComVisibleAttribute), true); | |
239 | |
240 foreach(ComVisibleAttribute attr in customAttributesOnPr
operty) | |
241 { | |
242 if(!attr.Value) | |
243 { | |
244 return false; | |
245 } | |
246 } | |
247 return true; | |
248 } | |
249 | |
250 private static bool IsComVisible(PropertyInfo propertyInfo) | |
251 { | |
252 object[] customAttributesOnProperty = propertyInfo.GetCu
stomAttributes(typeof(ComVisibleAttribute), true); | |
253 | |
254 foreach(ComVisibleAttribute attr in customAttributesOnPr
operty) | |
255 { | |
256 if(!attr.Value) | |
257 { | |
258 return false; | |
259 } | |
260 } | |
261 return true; | |
262 } | |
263 #endregion | |
264 } | |
265 } | |
OLD | NEW |