/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 36 - Deeper into C#\Deeper Reflection\Invoking Functions
// copyright 2000 Eric Gunnerson
// file=driver.cs
// compile with: csc driver.cs iprocess.cs
using System;
using System.Reflection;
using MamaSoft;
public class Deeper ReflectionInvokingFunctions
{
public static void ProcessAssembly(string aname)
{
Console.WriteLine("Loading: {0}", aname);
Assembly a = Assembly.LoadFrom (aname);
// walk through each type in the assembly
foreach (Type t in a.GetTypes())
{
// if it's a class, it might be one that we want.
if (t.IsClass)
{
Console.WriteLine(" Found Class: {0}", t.FullName);
// check to see if it implements IProcess
if (t.GetInterface("IProcess") == null)
continue;
// it implements IProcess. Create an instance
// of the object.
object o = Activator.CreateInstance(t);
// create the parameter list, call it,
// and print out the return value.
Console.WriteLine(" Calling Process() on {0}",
t.FullName);
object[] args = new object[] {55};
object result;
result = t.InvokeMember("Process",
BindingFlags.Default |
BindingFlags.InvokeMethod,
null, o, args);
Console.WriteLine(" Result: {0}", result);
}
}
}
public static void Main(String[] args)
{
foreach (string arg in args)
ProcessAssembly(arg);
}
}
//=======================================================
// 36 - Deeper into C#\Deeper Reflection\Invoking Functions
// copyright 2000 Eric Gunnerson
// file=IProcess.cs
namespace MamaSoft
{
interface IProcess
{
string Process(int param);
}
}
//=======================================================
// 36 - Deeper into C#\Deeper Reflection\Invoking Functions
// copyright 2000 Eric Gunnerson
// file=process2.cs
// compile with: csc /target:library process2.cs iprocess.cs
using System;
namespace MamaSoft
{
class Processor2: IProcess
{
Processor2() {}
public string Process(int param)
{
Console.WriteLine("In Processor2.Process(): {0}", param);
return("Shiver me timbers! ");
}
}
class Unrelated
{
}
}
//========================================================
// 36 - Deeper into C#\Deeper Reflection\Invoking Functions
// copyright 2000 Eric Gunnerson
// file=process1.cs
// compile with: csc /target:library process1.cs iprocess.cs
using System;
namespace MamaSoft
{
class Processor1: IProcess
{
Processor1() {}
public string Process(int param)
{
Console.WriteLine("In Processor1.Process(): {0}", param);
return("Raise the mainsail! ");
}
}
}