Deeper Reflection: Invoking Functions : Reflection Assembly « Development Class « C# / C Sharp






Deeper Reflection: Invoking Functions

/*
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! ");
        }
    }
}
           
       








Related examples in the same category

1.Get Method ParamemtersGet Method Paramemters
2.Get type infomation: base type, is abstract, is com object, is sealed, is classGet type infomation: base type, is abstract, is com object, is sealed, is class
3.Get all methods from a classGet all methods from a class
4.Get all fields from a classGet all fields from a class
5.Get all properties from a classGet all properties from a class
6.Get all implemented interface from a classGet all implemented interface from a class
7.Using reflection to get information about an assembly
8.Demonstrate using Type class to discover information about a classDemonstrate using Type class to discover information about a class
9.Demonstrate dynamically invoking an object
10.Locate an assembly, determine types, and create an object using reflection
11.Utilize MyClass without assuming any prior knowledge
12.Uses reflection to show the inherited members of a classUses reflection to show the inherited members of a class
13.Uses reflection to execute a class method indirectlyUses reflection to execute a class method indirectly
14.Illustrates runtime type invocation
15.Illustrates unloading an application domain
16.Demonstrate typeofDemonstrate typeof
17.Create an object using reflectionCreate an object using reflection
18.Uses an object in another application domain
19.Illustrates creation of an application domainIllustrates creation of an application domain