Demonstrate dynamically invoking an object : 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# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » Reflection AssemblyScreenshots 
Demonstrate dynamically invoking an object

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Invoke.cs -- Demonstrate dynamically invoking an object
//
//              Compile this program with the following command line:
//                  C:>csc Invoke.cs
using System;
using System.Reflection;

namespace nsReflection
{
    public class Invoke
    {
        static public void Main ()
        {
            // Load the Circle assembly.
            Assembly asy = null;
            try
            {
                asy = Assembly.Load ("Circle");
            }
            catch (Exception e)
            {
                Console.WriteLine (e.Message);
                return;
            }
            
            // Parameter array for a POINT object.
            object [] parmsPoint = new object [2] {1530};
            
            // Parameter array for a clsCircle object.
            object [] parmsCircle = new object [3] {1001530};
            
            // Get the type of clsCircle and create an instance of it.
            Type circle = asy.GetType("nsCircle.clsCircle");
            object obj = Activator.CreateInstance (circle, parmsCircle);
            
            // Get the property info for the area and show the area
            PropertyInfo p = circle.GetProperty ("Area");
            Console.WriteLine ("The area of the circle is " + p.GetValue(obj, null));
            
            // Get the POINT type and create an instance of it
            Type point = asy.GetType("nsCircle.POINT");
            object pt = Activator.CreateInstance (point, parmsPoint);
            
            // Show the point using object's ToString() method
            Console.WriteLine ("The point is " + pt.ToString ());
        }
    }
}


           
       
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.Locate an assembly, determine types, and create an object using reflection
10.Utilize MyClass without assuming any prior knowledge
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.