Create an object using reflection : 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 Assembly 




Create an object using reflection
Create an object using reflection

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// Create an object using reflection. 
 
using System; 
using System.Reflection; 
 
class MyClass 
  int x; 
  int y; 
 
  public MyClass(int i) { 
    Console.WriteLine("Constructing MyClass(int, int). ")
    x = y = i;  
  
 
  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)
  
 

 
public class InvokeConsDemo 
  public static void Main() { 
    Type t = typeof(MyClass)
    int val; 
 
    // Get constructor info. 
    ConstructorInfo[] ci = t.GetConstructors()
 
    Console.WriteLine("Available constructors: ")
    foreach(ConstructorInfo c in ci) { 
      // Display return type and name. 
      Console.Write("   " + t.Name + "(")
 
      // Display parameters. 
      ParameterInfo[] pi = c.GetParameters()
 
      for(int i=0; i < pi.Length; i++) { 
        Console.Write(pi[i].ParameterType.Name + 
                      " " + pi[i].Name)
        if(i+< pi.LengthConsole.Write(", ")
      
 
      Console.WriteLine(")")
    
    Console.WriteLine()
 
    // Find matching constructor. 
    int x; 
 
    for(x=0; x < ci.Length; x++) { 
      ParameterInfo[] pi =  ci[x].GetParameters()
      if(pi.Length == 2break
    
 
    if(x == ci.Length) { 
      Console.WriteLine("No matching constructor found.")
      return
    
    else 
      Console.WriteLine("Two-parameter constructor found.\n")
 
    // Construct the object.   
    object[] consargs = new object[2]
    consargs[010
    consargs[120
    object reflectOb = ci[x].Invoke(consargs)
 
    Console.WriteLine("\nInvoking methods on reflectOb.")
    Console.WriteLine()
    MethodInfo[] mi = t.GetMethods()
 
    // Invoke each method. 
    foreach(MethodInfo m in mi) { 
      // Get the parameters 
      ParameterInfo[] pi = m.GetParameters()
 
      if(m.Name.CompareTo("set")==&& 
         pi[0].ParameterType == typeof(int)) { 
        // This is set(int, int). 
        object[] args = new object[2]
        args[09
        args[118
        m.Invoke(reflectOb, args)
      
      else if(m.Name.CompareTo("set")==&& 
              pi[0].ParameterType == typeof(double)) { 
        // This is set(double, double). 
        object[] args = new object[2]
        args[01.12
        args[123.4
        m.Invoke(reflectOb, args)
      
      else if(m.Name.CompareTo("sum")==0) { 
        val = (intm.Invoke(reflectOb, null)
        Console.WriteLine("sum is " + val)
      
      else if(m.Name.CompareTo("isBetween")==0) { 
        object[] args = new object[1]
        args[014
        if((boolm.Invoke(reflectOb, args)) 
          Console.WriteLine("14 is between x and y")
      
      else if(m.Name.CompareTo("show")==0) { 
        m.Invoke(reflectOb, null)
      
    
  




           
       














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.Deeper Reflection: Invoking Functions
15.Illustrates runtime type invocation
16.Illustrates unloading an application domain
17.Demonstrate typeofDemonstrate typeof
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.