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 } | |
145 else if(index is int) | |
146 { | |
147 int realIndex = (int)index - 1; | |
148 if(realIndex >= 0 && realIndex < this.properties
.Count) | |
149 { | |
150 IEnumerator enumerator = this.properties
.Values.GetEnumerator(); | |
151 | |
152 int i = 0; | |
153 while(enumerator.MoveNext()) | |
154 { | |
155 if(i++ == realIndex) | |
156 { | |
157 return (EnvDTE.Property)
enumerator.Current; | |
158 } | |
159 } | |
160 } | |
161 } | |
162 | |
163 throw new ArgumentException(SR.GetString(SR.InvalidParam
eter, CultureInfo.CurrentUICulture), "index"); | |
164 } | |
165 /// <summary> | |
166 /// Gets the immediate parent object of a Properties collection. | |
167 /// </summary> | |
168 public virtual object Parent | |
169 { | |
170 get { return null; } | |
171 } | |
172 #endregion | |
173 | |
174 #region methods | |
175 /// <summary> | |
176 /// Add properties to the collection of properties filtering onl
y those properties which are com-visible and AutomationBrowsable | |
177 /// </summary> | |
178 /// <param name="targetType">The type of NodeProperties the we s
hould filter on</param> | |
179 protected void AddPropertiesFromType(Type targetType) | |
180 { | |
181 Debug.Assert(targetType != null); | |
182 | |
183 // If the type is not COM visible, we do not expose any
of the properties | |
184 if(!IsComVisible(targetType)) | |
185 return; | |
186 | |
187 // Add all properties being ComVisible and AutomationVis
ible | |
188 PropertyInfo[] propertyInfos = targetType.GetProperties(
); | |
189 foreach(PropertyInfo propertyInfo in propertyInfos) | |
190 { | |
191 if(!IsInMap(propertyInfo) && IsComVisible(proper
tyInfo) && IsAutomationVisible(propertyInfo)) | |
192 { | |
193 AddProperty(propertyInfo); | |
194 } | |
195 } | |
196 } | |
197 #endregion | |
198 | |
199 #region virtual methods | |
200 /// <summary> | |
201 /// Creates a new OAProperty object and adds it to the current l
ist of properties | |
202 /// </summary> | |
203 /// <param name="propertyInfo">The property to be associated wit
h an OAProperty object</param> | |
204 protected virtual void AddProperty(PropertyInfo propertyInfo) | |
205 { | |
206 this.properties.Add(propertyInfo.Name, new OAProperty(th
is, propertyInfo)); | |
207 } | |
208 #endregion | |
209 | |
210 #region helper methods | |
211 | |
212 private bool IsInMap(PropertyInfo propertyInfo) | |
213 { | |
214 return this.properties.ContainsKey(propertyInfo.Name); | |
215 } | |
216 | |
217 private static bool IsAutomationVisible(PropertyInfo propertyInf
o) | |
218 { | |
219 object[] customAttributesOnProperty = propertyInfo.GetCu
stomAttributes(typeof(AutomationBrowsableAttribute), true); | |
220 | |
221 foreach(AutomationBrowsableAttribute attr in customAttri
butesOnProperty) | |
222 { | |
223 if(!attr.Browsable) | |
224 { | |
225 return false; | |
226 } | |
227 } | |
228 return true; | |
229 } | |
230 | |
231 private static bool IsComVisible(Type targetType) | |
232 { | |
233 object[] customAttributesOnProperty = targetType.GetCust
omAttributes(typeof(ComVisibleAttribute), true); | |
234 | |
235 foreach(ComVisibleAttribute attr in customAttributesOnPr
operty) | |
236 { | |
237 if(!attr.Value) | |
238 { | |
239 return false; | |
240 } | |
241 } | |
242 return true; | |
243 } | |
244 | |
245 private static bool IsComVisible(PropertyInfo propertyInfo) | |
246 { | |
247 object[] customAttributesOnProperty = propertyInfo.GetCu
stomAttributes(typeof(ComVisibleAttribute), true); | |
248 | |
249 foreach(ComVisibleAttribute attr in customAttributesOnPr
operty) | |
250 { | |
251 if(!attr.Value) | |
252 { | |
253 return false; | |
254 } | |
255 } | |
256 return true; | |
257 } | |
258 #endregion | |
259 } | |
260 } | |
OLD | NEW |