2 // System.ComponentModel.Design.ReferenceService
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2007 Ivan N. Zlatev
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
34 using System.Globalization;
35 using System.ComponentModel;
37 namespace System.ComponentModel.Design
40 internal class ReferenceService : IReferenceService, IDisposable
43 private List<IComponent> _references;
45 internal ReferenceService (IServiceProvider provider)
48 throw new ArgumentNullException ("provider");
50 _references = new List<IComponent>();
51 IComponentChangeService serv = provider.GetService (typeof (IComponentChangeService)) as IComponentChangeService;
53 serv.ComponentAdded += OnComponentAdded;
54 serv.ComponentRemoved += OnComponentRemoved;
58 private void OnComponentAdded (object sender, ComponentEventArgs args)
60 _references.Add (args.Component);
63 private void OnComponentRemoved (object sender, ComponentEventArgs args)
65 _references.Remove (args.Component);
68 public IComponent GetComponent (object reference)
70 return reference as IComponent;
73 public string GetName (object reference)
75 IComponent comp = reference as IComponent;
76 if (comp != null && comp.Site != null)
77 return comp.Site.Name;
81 public object GetReference (string name)
83 foreach (IComponent component in _references)
84 if (component.Site != null && component.Site.Name == name)
89 public object[] GetReferences ()
91 IComponent[] references = new IComponent[_references.Count];
92 _references.CopyTo (references);
96 public object[] GetReferences (Type baseType)
98 List<IComponent> references = new List<IComponent>();
100 foreach (IComponent component in _references)
101 if (baseType.IsAssignableFrom ((component.GetType ())))
102 references.Add (component);
104 IComponent[] refArray = new IComponent[references.Count];
105 references.CopyTo (refArray);
109 public void Dispose ()
111 _references.Clear ();