1: // This is the program which will load the plugins and test them.
2: // Compile with the following command line:
3: // csc PlugInTester.cs /r:MyPlugIn.dll
4: using System;
5: using System.Reflection;
6: using System.IO;
7:
8: class PlugInTester
9: {
10: public static void Main()
11: {
12: //Get a list of all the files in the current directory
13: // who's names match the plugin mask.
14: string[] plugInFileNames = Directory.GetFiles(
15: Environment.CurrentDirectory,
16: PlugInForm.FileNameMask);
17: //Iterate through them all.
18: foreach(string fileName in plugInFileNames)
19: {
20: //Split off the file name and display it.
21: Console.WriteLine("Loading DLL '{0}'",
22: Path.GetFileName(fileName));
23:
24: //Load the assembly so that we can manipulate it and
25: // instantiate the concrete class contained within it.
26: Assembly assCurrent = Assembly.LoadFrom(fileName);
27:
28: //Get a list of all types contained within the object
29: Type[] typPlugInTypes = assCurrent.GetTypes();
30:
31: //Iterate through all the types in the assembly
32: foreach(Type typCurrent in typPlugInTypes)
33: {
34: //If the type is one we know how to handle
35: if (typCurrent.IsSubclassOf(typeof(PlugInForm)))
36: {
37: //Create an instance of the type
38: object objCurrent = Activator.CreateInstance(
39: typCurrent);
40:
41: //Get a handle to the method we know it has
42: MethodInfo miCurrent = typCurrent.GetMethod(
43: "WhatIsPI");
44:
45: //Write out the type of the object
46: Console.Write(" {0} says: ",
47: typCurrent.ToString());
48:
49: // followed by the output from the method we know.
50: miCurrent.Invoke(objCurrent,null);
51: }
52: else
53: {
54: //Otherwise, spit out a message saying the name of
55: // the type.
56: Console.WriteLine(" {0} is not derived from "+
57: "PlugInForm",typCurrent.ToString());
58:
59: //For a more advanced look at what you can do with an
60: // unknown type, uncomment the following code and run with
61: // the MPIPhilosopher plugin:
62: // MethodInfo[] miAll = typCurrent.GetMethods();
63: // foreach(MethodInfo miThisOne in miAll)
64: // {
65: // ParameterInfo[] piThisOne = miThisOne.GetParameters();
66: // if (piThisOne.Length == 0)
67: // {
68: // Console.WriteLine(" {0} has no parameters "+
69: // "so I will run it.",miThisOne.ToString());
70: //
71: // //Create an instance of the type
72: // object objThisOne = Activator.CreateInstance(
73: // typCurrent);
74: //
75: // if(miThisOne.ReturnType == typeof(void))
76: // {
77: // //Run the method
78: // Console.Write(" ");
79: // miThisOne.Invoke(objThisOne,null);
80: // }
81: // else
82: // {
83: // //Run the method and print the output
84: // Console.WriteLine(" {0}",
85: // miThisOne.Invoke(objThisOne,null));
86: // }
87: // }
88: // else
89: // {
90: // //If the method had parameters then you would go
91: // // on and find out what type they
92: // // were and pass in appropriate values.
93: // Console.WriteLine(" {0} has parameters so "+
94: // "I will NOT run it.",miThisOne.ToString());
95: // }
96: // }
97: }
98: }
99: }
100: }
101: }
102: