~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/Reports/ICSharpCode.Reports.Addin/Project/Services/TypeDiscoveryService.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections;
 
6
using System.Collections.Generic;
 
7
using System.ComponentModel.Design;
 
8
using System.Reflection;
 
9
 
 
10
using ICSharpCode.Core;
 
11
using ICSharpCode.SharpDevelop.Dom;
 
12
 
 
13
namespace ICSharpCode.Reports.Addin
 
14
{
 
15
        public class TypeDiscoveryService : ITypeDiscoveryService
 
16
        {
 
17
                public TypeDiscoveryService()
 
18
                {
 
19
                }
 
20
                
 
21
                /// <summary>
 
22
                /// Returns the list of available types.
 
23
                /// </summary>
 
24
                /// <param name="baseType">The base type to match.  Can be null.</param>
 
25
                /// <param name="excludeGlobalTypes">Determines whether types
 
26
                /// from all referenced assemblies should be checked.</param>
 
27
                public ICollection GetTypes(Type baseType, bool excludeGlobalTypes)
 
28
                {
 
29
                        List<Type> types = new List<Type>();
 
30
                        
 
31
                        if (baseType == null) {
 
32
                                baseType = typeof(object);
 
33
                        }
 
34
                        
 
35
                        LoggingService.Debug("TypeDiscoveryService.GetTypes for " + baseType.FullName
 
36
                                             + "excludeGlobalTypes=" + excludeGlobalTypes.ToString());
 
37
                        //seek in all assemblies
 
38
                        //allow to work designers like columns editor in datagridview
 
39
                        // Searching types can cause additional assemblies to be loaded, so we need to use
 
40
                        // ToArray to prevent an exception if the collection changes.
 
41
                        foreach (Assembly asm in TypeResolutionService.DesignerAssemblies.ToArray()) {
 
42
                                if (excludeGlobalTypes) {
 
43
                                        if (GacInterop.IsWithinGac(asm.Location)) {
 
44
                                                continue;
 
45
                                        }
 
46
                                }
 
47
                                AddDerivedTypes(baseType, asm, types);
 
48
                        }
 
49
                        LoggingService.Debug("TypeDiscoveryService returns " + types.Count + " types");
 
50
                        
 
51
                        // TODO - Don't look in all assemblies.
 
52
                        // Should use the current project and its referenced assemblies
 
53
                        // as well as System.Windows.Forms.
 
54
                        
 
55
                        return types;
 
56
                }
 
57
                
 
58
                /// <summary>
 
59
                /// Gets the types derived from baseType from the assembly and adds them to the list.
 
60
                /// </summary>
 
61
                void AddDerivedTypes(Type baseType, Assembly assembly, IList<Type> list)
 
62
                {
 
63
                        foreach (Type t in assembly.GetExportedTypes()) {
 
64
                                if (t.IsSubclassOf(baseType)) {
 
65
                                        //LoggingService.Debug("TypeDiscoveryService.  Adding type=" + t.FullName);
 
66
                                        list.Add(t);
 
67
                                }
 
68
                        }
 
69
                }
 
70
        }
 
71
}