Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IFaceDisco.cs « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f8a06225e924535e1c11857c3b0b4e7e8b4a767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// IFaceDisco.cs
//
// Nick Drochak (ndrochak@gol.com)
//
// (C) 2001 Nick Drochak

using System;
using System.Reflection;
using System.Collections;
using System.IO;

namespace Mono.Util
{
	class IFaceDisco {
		public static void Main(string[] args) {
			Assembly					asm;
			Type[]						asmTypes;
			InterfaceMapping		map;
			Type[]						interfaces;
			ArrayList					TypesList					= new ArrayList();
			ArrayList					implementingTypes	= new ArrayList();
			string						asmFullPath				= null;
			string						ifaceToDiscover		= null;
			
			if (args.Length < 1 || args.Length > 3) {
				Usage();
				return;
			}

			for (int i = 0; i < args.Length; i++) {
				string arg = args[i];

				if (arg.StartsWith("-") && ((i + 1) < args.Length)) {
					if (arg == "--asm") {
						asmFullPath = args[++i];
					} else {
						Usage();
						return;
					}
				} else {
					// allow only one interface to discover
					if (ifaceToDiscover != null){
						Usage();
						return;
					}
					ifaceToDiscover = arg;
				}
			}

			// find the assembly
			if (null == asmFullPath){
				asm = Assembly.GetAssembly(typeof (System.Object));
			}
			else {
				try{
					asm = Assembly.LoadFrom(asmFullPath);
				}
				catch(Exception e){
					Console.WriteLine("Could not open assembly '{0}' for discovery. Error is: "+e.Message, asmFullPath);
					return;
				}
			}
			asmTypes = asm.GetTypes();

			// examine all the public types
			foreach(Type t in asmTypes) {
				if (t.IsPublic) {
					// find out which, if any, interfaces are "in" the type
					interfaces= t.GetInterfaces();
					if (null != interfaces){
						// look for the interface we want to discover
						foreach (Type iface in interfaces) {
							// this area seems to throw an exception sometimes, just ignore it
							try{
								if (iface.FullName.ToLower() == args[0].ToLower()) {
									// find out if this type is the one which "declares" the interface
									map = t.GetInterfaceMap(iface);
									if (map.TargetMethods[0].DeclaringType.FullName == t.FullName){
										// if so, then we found a class to report
										implementingTypes.Add(t.FullName);
									} // if
								}  // if
							}catch{}
						} // foreach
					} // if
				} // if
			} // foreach

			// sort the list to make it easier to find what you are looking for
			implementingTypes.Sort();
			Console.WriteLine(XMLUtil.ToXML(implementingTypes, "Type", "ImplementingTypes"));
		} // Main()

		private static void Usage() {
			Console.WriteLine (
				"Mono Interface Discovery Tool\n" +
				"usage: ifacedisco [--asm assembly] interface\n\n" +
				"  The full path to 'assembly' should be specified when using --asm.\n" +
				"  If 'assembly' is not specified, the assembly that contains System.Object will be used.\n" +
				"  Use the fully qualified form for 'interface', e.g. System.Runtime.Serialization.ISerializable\n"
				);
		} // Usage()

	} // class IFaceDisco
}  // namespace Mono.Util