**** Merged from MCS ****
[mono-project.git] / mcs / tools / IFaceDisco.cs
blob0f8a06225e924535e1c11857c3b0b4e7e8b4a767
1 // IFaceDisco.cs
2 //
3 // Nick Drochak (ndrochak@gol.com)
4 //
5 // (C) 2001 Nick Drochak
7 using System;
8 using System.Reflection;
9 using System.Collections;
10 using System.IO;
12 namespace Mono.Util
14 class IFaceDisco {
15 public static void Main(string[] args) {
16 Assembly asm;
17 Type[] asmTypes;
18 InterfaceMapping map;
19 Type[] interfaces;
20 ArrayList TypesList = new ArrayList();
21 ArrayList implementingTypes = new ArrayList();
22 string asmFullPath = null;
23 string ifaceToDiscover = null;
25 if (args.Length < 1 || args.Length > 3) {
26 Usage();
27 return;
30 for (int i = 0; i < args.Length; i++) {
31 string arg = args[i];
33 if (arg.StartsWith("-") && ((i + 1) < args.Length)) {
34 if (arg == "--asm") {
35 asmFullPath = args[++i];
36 } else {
37 Usage();
38 return;
40 } else {
41 // allow only one interface to discover
42 if (ifaceToDiscover != null){
43 Usage();
44 return;
46 ifaceToDiscover = arg;
50 // find the assembly
51 if (null == asmFullPath){
52 asm = Assembly.GetAssembly(typeof (System.Object));
54 else {
55 try{
56 asm = Assembly.LoadFrom(asmFullPath);
58 catch(Exception e){
59 Console.WriteLine("Could not open assembly '{0}' for discovery. Error is: "+e.Message, asmFullPath);
60 return;
63 asmTypes = asm.GetTypes();
65 // examine all the public types
66 foreach(Type t in asmTypes) {
67 if (t.IsPublic) {
68 // find out which, if any, interfaces are "in" the type
69 interfaces= t.GetInterfaces();
70 if (null != interfaces){
71 // look for the interface we want to discover
72 foreach (Type iface in interfaces) {
73 // this area seems to throw an exception sometimes, just ignore it
74 try{
75 if (iface.FullName.ToLower() == args[0].ToLower()) {
76 // find out if this type is the one which "declares" the interface
77 map = t.GetInterfaceMap(iface);
78 if (map.TargetMethods[0].DeclaringType.FullName == t.FullName){
79 // if so, then we found a class to report
80 implementingTypes.Add(t.FullName);
81 } // if
82 } // if
83 }catch{}
84 } // foreach
85 } // if
86 } // if
87 } // foreach
89 // sort the list to make it easier to find what you are looking for
90 implementingTypes.Sort();
91 Console.WriteLine(XMLUtil.ToXML(implementingTypes, "Type", "ImplementingTypes"));
92 } // Main()
94 private static void Usage() {
95 Console.WriteLine (
96 "Mono Interface Discovery Tool\n" +
97 "usage: ifacedisco [--asm assembly] interface\n\n" +
98 " The full path to 'assembly' should be specified when using --asm.\n" +
99 " If 'assembly' is not specified, the assembly that contains System.Object will be used.\n" +
100 " Use the fully qualified form for 'interface', e.g. System.Runtime.Serialization.ISerializable\n"
102 } // Usage()
104 } // class IFaceDisco
105 } // namespace Mono.Util