Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Dynamically Invoking an Object
Author: phanindra yerra
Published: 6/29/2006 6:35 AM
Category: C#
Summary: This article explains how to invoke object dynamically using system.Reflections

 

Dynamically Invoking an Object

 

In previous article <a href= “http://www.dotnetspider.com/kb/Article2055.aspx”> http://www.dotnetspider.com/kb/Article2055.aspx  </a> discovered information about an assembly and the types it contains, the next step is to create an object and invoke its properties or methods from that information. The .NET Framework provides the means to create an object and to invoke its members dynamically.

The key to doing this is the Activator class, which is a member of the System.Reflection namespace.

 

Activator class

 

The Activator class contains only static methods, so you never have to declare an instance of the class. Using the Activator class involves late binding.

<b>late binding</b>

 

 In which the exact type of the object is not known until runtime.

 

<b>early binding</b>

 

which you use when you create an object using the new keyword. Early binding is always preferable to late binding, because early binding ensures that errors will be determined when you compile the program. Early binding usually results in faster program load times as well. Late binding, however, allows you to create an object without knowing its type.

 

The following program, Invoke.cs, extends the Asy.cs program and creates objects of the

ASM structure and the clsASM class in the ASMNAME assembly:

 

 Invoke.cs -- Demonstrates dynamically invoking an object

 

[code]

 

using System;

using System.Reflection;

 

namespace nsReflection

{

class clsMain

{

static public void Main ()

{

 

// Load the ASMNAME assembly.

 

Assembly asy = null;

 

try

{

asy = Assembly.Load ("ASMNAME");

}

 

catch (Exception e)

{

Console.WriteLine (e.Message);

return;

}

 

// Parameter array for a ASM object.

 

object [] parmsSUC = new object [2] {1, 1};

 

// Parameter array for a clsASM object.

 

object [] parmsASM = new object [3] {‘v’, ‘z’, ‘g’};

 

// Get the type of clsASM and create an instance of it.

 

Type asm1 = asy.GetType("nsASM.ASMNAME");

 

object obj = Activator.CreateInstance (asm1, parmsASM);

 

// Get the property info for the area and show the area.

 

PropertyInfo p = asm1.GetProperty ("Name");

 

Console.WriteLine ("The Name of the city is " +p.GetValue(obj, null));

 

// Get the POINT type and create an instance of it.

Type asm2 = asy.GetType("nsASM.ASM");

 

object pt = Activator.CreateInstance (asm2, parmsSUC);

 

// Show the asm2 using object's ToString() method.

Console.WriteLine ("The ASM is " + pt.ToString ());

}

}

}

 

[/code]

 

Compiling and running Invoke.cs results in the following output:

 

The name of the city is vizag.

The ASM is success

 

 

Assuming that you do not have the source code for the ASMNAME assembly, you now have discovered information about the assembly and its types, and you’ve created objects indirectly using theActivator class.

 

This article explains how to invoke object dynamically using system.Reflections  

see also <a href= “http://www.dotnetspider.com/kb/Article2055.aspx”> http://www.dotnetspider.com/kb/Article2055.aspx  </a>


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
online secure backup
Color Laser Printer
Giorgio Armani sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139