Utilize MyClass without assuming any prior knowledge : Reflection Assembly « Development Class « C# / C Sharp






Utilize MyClass without assuming any prior knowledge

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Utilize MyClass without assuming any prior knowledge. 
 
using System; 
using System.Reflection; 
 
public class ReflectAssemblyDemo1 { 
  public static void Main() { 
    int val; 
    Assembly asm = Assembly.LoadFrom("MyClasses.exe"); 
 
    Type[] alltypes = asm.GetTypes(); 
 
    Type t = alltypes[0]; // use first class found 
 
    Console.WriteLine("Using: " + t.Name); 
 
    ConstructorInfo[] ci = t.GetConstructors(); 
 
    // Use first constructor found. 
    ParameterInfo[] cpi = ci[0].GetParameters(); 
    object reflectOb; 
 
    if(cpi.Length > 0) { 
      object[] consargs = new object[cpi.Length]; 
 
      // initialize args 
      for(int n=0; n < cpi.Length; n++) 
        consargs[n] = 10 + n * 20; 
 
      // construct the object 
      reflectOb = ci[0].Invoke(consargs); 
    } else 
      reflectOb = ci[0].Invoke(null); 
     
 
    Console.WriteLine("\nInvoking methods on reflectOb."); 
    Console.WriteLine(); 
 
    // Ignore inherited methods. 
    MethodInfo[] mi = t.GetMethods(BindingFlags.DeclaredOnly | 
                                   BindingFlags.Instance | 
                                   BindingFlags.Public) ; 
 
    // Invoke each method. 
    foreach(MethodInfo m in mi) { 
      Console.WriteLine("Calling {0} ", m.Name); 
 
      // Get the parameters 
      ParameterInfo[] pi = m.GetParameters(); 
 
      // Execute methods. 
      switch(pi.Length) { 
        case 0: // no args 
          if(m.ReturnType == typeof(int)) { 
            val = (int) m.Invoke(reflectOb, null); 
            Console.WriteLine("Result is " + val); 
          } 
          else if(m.ReturnType == typeof(void)) { 
            m.Invoke(reflectOb, null); 
          } 
          break; 
        case 1: // one arg 
          if(pi[0].ParameterType == typeof(int)) { 
            object[] args = new object[1]; 
            args[0] = 14; 
            if((bool) m.Invoke(reflectOb, args)) 
              Console.WriteLine("14 is between x and y"); 
            else 
              Console.WriteLine("14 is not between x and y"); 
          } 
          break; 
        case 2: // two args 
          if((pi[0].ParameterType == typeof(int)) && 
             (pi[1].ParameterType == typeof(int))) { 
            object[] args = new object[2]; 
            args[0] = 9; 
            args[1] = 18; 
            m.Invoke(reflectOb, args); 
          } 
          else if((pi[0].ParameterType == typeof(double)) && 
                  (pi[1].ParameterType == typeof(double))) { 
            object[] args = new object[2]; 
            args[0] = 1.12; 
            args[1] = 23.4; 
            m.Invoke(reflectOb, args); 
          } 
          break; 
      }      
      Console.WriteLine(); 
    } 
 
  } 
}
//==============================================================
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A file that contains three classes.  Call this file MyClasses.cs. 
 
using System; 
 
class MyClass { 
  int x; 
  int y; 
 
  public MyClass(int i) { 
    Console.WriteLine("Constructing MyClass(int). "); 
    x = y = i;  
    show(); 
  } 
 
  public MyClass(int i, int j) { 
    Console.WriteLine("Constructing MyClass(int, int). "); 
    x = i; 
    y = j; 
    show(); 
  } 
 
  public int sum() { 
    return x+y; 
  } 
 
  public bool isBetween(int i) { 
    if((x < i) && (i < y)) return true; 
    else return false; 
  } 
 
  public void set(int a, int b) { 
    Console.Write("Inside set(int, int). "); 
    x = a; 
    y = b; 
    show(); 
  } 
 
  // Overload set. 
  public void set(double a, double b) { 
    Console.Write("Inside set(double, double). "); 
    x = (int) a; 
    y = (int) b; 
    show(); 
  } 
 
  public void show() { 
    Console.WriteLine("Values are x: {0}, y: {1}", x, y); 
  } 
 
} 
 
class AnotherClass { 
  string remark; 
 
  public AnotherClass(string str) { 
    remark = str; 
  } 
 
  public void show() { 
    Console.WriteLine(remark); 
  } 
} 
 
public class Demo12 { 
  public static void Main() { 
    Console.WriteLine("This is a placeholder."); 
  } 
}


           
       








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.Uses reflection to show the inherited members of a classUses reflection to show the inherited members of a class
12.Uses reflection to execute a class method indirectlyUses reflection to execute a class method indirectly
13.Deeper Reflection: Invoking Functions
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