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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Development Class » Reflection AssemblyScreenshots 
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[n10 + 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 = (intm.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[014
            if((boolm.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[09
            args[118
            m.Invoke(reflectOb, args)
          
          else if((pi[0].ParameterType == typeof(double)) && 
                  (pi[1].ParameterType == typeof(double))) { 
            object[] args = new object[2]
            args[01.12
            args[123.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 = (inta; 
    y = (intb; 
    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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.