Invoke member method with created instance from reflection : Method « Reflection « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Reflection » Method 
19.5.10.Invoke member method with created instance from reflection
using System;
using System.Reflection;
using System.Globalization;
  class Class1
  {
    DateTime[] dateTimes = new DateTime[10];
    public DateTime this[int index]
    {
      getreturn dateTimes[index]}
      setdateTimes[index= value;}
    }
    
    
    private DateTime dateOfBirth;
    public DateTime DateOfBirth
    {
        getreturn dateOfBirth; }
        setdateOfBirth = value; }
    }
      
    public void Test()
    {
      Console.WriteLine("Test method called");
    }
    
    
    private string field;
    
    public string Property
    
      getreturn field; }
      setfield = value; }
    }
    
  }


    class MainClass{
    
    static void Main(string[] args)
    {
      Type type = Type.GetType("Class1");
      object o = Activator.CreateInstance(type);
      
      type.InvokeMember("Test", BindingFlags.InvokeMethod,null, o, new object[]{});
      
      // Using a ConstructorInfo
      ConstructorInfo constructorInfo = type.GetConstructor(new Type[]{});
        
      o = constructorInfo.Invoke(new object[]{});
      
      type.InvokeMember("Test", BindingFlags.InvokeMethod,null, o, new object[]{});
      
      // Type.InvokeMember
      o = type.InvokeMember("Class1", BindingFlags.CreateInstance, null, null, new object[]{});
      
      type.InvokeMember("Test", BindingFlags.InvokeMethod,null, o, new object[]{});

    }
    }
19.5.Method
19.5.1.Deeper Reflection: iterate through the methods of the class
19.5.2.Deeper Reflection: Invoking Functions
19.5.3.Invoke methods using reflection
19.5.4.List Methods
19.5.5.Obtain the metadata tokens
19.5.6.Inspecting a method's body
19.5.7.Dynamically Invoking A Method
19.5.8.Finding Particular Members
19.5.9.Call string method with reflection and Expression
19.5.10.Invoke member method with created instance from reflection
19.5.11.Use reflection to invoke methods
19.5.12.Use DefinePInvokeMethod method to create a PInvoke method.
19.5.13.Get the method that matches the specified binding flags.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.