Instantiating Classes

All managed code in a .NET application is compiled into assemblies, be they EXEs or DLLs. The key difference is that an EXE has an entry point from the operating system and a DLL doesn't. In all other respects, they're the same. You may need to access an assembly's internal structures at runtime, having had no knowledge of the names of the cases, properties, or methods available to you at compile time. Examine the Invoice class shown in Listing 2-1. This class would obviously have no real business value and is constructed solely to illustrate the various parts of Reflection.

Listing 2-1. Invoice Class public interface IInvoice {

DateTime SaleDate {get; set;} double GetTotal();

public interface IMoreInvoice {

int SalesRepID { get; set;}

public delegate void UpdateEventHandler(object sender, EventArgs e);

public class Invoice : IInvoice, IMoreInvoice {

public int iInvoiceNumber; public string szCustomerCode;

private DateTime _dSaleDate; private int _iSalesRepID;

public DateTime SaleDate {

get { return _dSaleDate; } set { _dSaleDate = value; }

public int SalesRepID {

get { return _iSalesRepID; } set { _iSalesRepID = value; }

public Invoice() {

public Invoice(DateTime dSaleDate, int iSalesRepID) {

dSaleDate = _dSaleDate; iSalesRepID = _iSalesRepID;

public event UpdateEventHandler Updated;

protected virtual void OnUpdated(EventArgs e) {

public double GetTotal() {

return 150;

public double GetSalesTax() {

public double ApplyDiscount(double dblDiscount)

return 150 * dblDiscount;

If you had this class compiled in a .NET assembly and wanted to instantiate it at runtime, you could do so by loading the assembly and instantiating the string name of the class, as shown in Listing 2-2. Note that you can't strongly type the class as Invoice because you won't have access to this type definition at the time you write the code. The assumption is that this code will be compiled at runtime.

Listing 2-2 Instantiating the Invoice Class at Runtime

Assembly oAssembly; Type oType; object oInvoice;

oAssembly = Assembly.LoadFrom(@"C:\temp\Reflection.exe"); oInvoice= oAssembly.CreateInstance("ReflectionDemo.Invoice"); oType = oObject.GetType();

Here, you can use the LoadFrom() method of the Assembly class to load the assembly. The Assembly class, as its name suggests, represents a .NET assembly. The CreateInstance() method creates an instance of the class and returns it as type object. At this point, you can retrieve a Type object from this object instance; through this Type object, you can drill down into the class, as you see later in this chapter.

If the assembly were already loaded into your project, you could instantiate the Invoice object as follows:

Type oType; object oInvoice;

oType = Type.GetType("ReflectionDemo.Invoice"); oInvoice = Activator.CreateInstance(oType);

The Activator class allows you to instantiate objects at runtime through its CreateInstance() method. Under normal circumstances, you strongly type an object reference as Invoice like this:

Invoice oInvoice = new Invoice();

But because you're building a data-driven application, you don't know the name of this object at compile time.

Loading Shared Assemblies

A shared assemblyis one residing in the General Assembly Cache (GAC). You can locate these assemblies by browsing to c: \windows\assembly. There, you can see all the information needed to load any assembly you wish. Figure 2-1 shows some of the registered assemblies.

fe» ASSEMBLY

JnJxJ

File Edit View Favorites Tools Help

1 »

Q Back • Q - S Search

Folders [v] X □

?]• Itgj, Folder Sync

Address | D:\WINDOWS\ASSEMBLY

| Version_| Culture | Public Key Token | Processor Architecture |

Assembly Name

| Version_| Culture | Public Key Token | Processor Architecture |

El Ö 0 ö I 0 ö Ö 0 ö 0 ö Ö Ö 0 Ö Ö 0 ö Ö 0 Ö 0 Ö 0 Ö Ö Q

$NtUninstallKB924496$

$NtUninstallKB925398_WMP64$

$NtUninstallKB925454$

$NtUninstallKB925486$

$NtUninstallKB926255$

$NtUninstallKB929969$

ADDINS

AppPatch

ASSEMBLY

Config

Connection Wizard Cursors

Downloaded Installations

Downloaded Program Files

Driver Cache

Fonts

Help

Intuit

JAVA

Microsoft.NET

Minidump

Motive

MSAGENT

MSAPPS

Mill occache

Offline Web Pages PCHEALTH PeerNet Prefetch

Assembly Name

IS Syncfusion.Pdf. Windows lib Syncf usion. Pdf. WPF

Syncf usion. Shared. Base 5b Syncf usion .Shared. Base ¡¿I Syncf usion. Shared. Web l3t| Syncf usion. Shared. Web ¿1 Syncf usion. Shared. Windows Sb Syncf usion. Shared. Windows

Syncf usion .Shared. WPF ¡¿I Syncf usion. Shared. WPF. dll. Design ¿1 Syncf usion .Shared. WPF. VisualStudio. Design 3b Syncf usion. Tools .Windows (¿1 Syncf usion. XlsIO, Base 5b Syncf usion. XlsIO. Base lijSyncfusion.XlsIO.Web ¿1 Syncf usion .XlsIO. Web ilil Syncf usion. XlsIO. Windows (¡b Syncf usion, XlsIO, Windows lIlSyncfusion.XlsIO.WPF CJsysglobl & System lib System. Addln System. Addln 5b System. Addln. Contract ¿J System. ComponentModel. DataAnnotations l3b System. Configuration System. Configuration. Install System. Core 3b System. Data

(¿1 System. Data. DataSetExtensions ■¿(System. Data. Entity

System. Data. Entity. Design System. Data. Linq 5b System. Data. OradeClient (¿1 System. Data. Services l3b System. Data. Services .Client i J System. Data. Services. Design

7.102.0.30 7.103.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.103.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.102.0.30 7.103.0.30 7.102.0.30 7.103.0.30 2.0.0.0 2.0.0.0 3.5.0.0 2.0.0.0 2.0.0.0 3,5.0.0 2.0.0.0 2.0.0.0 3.5.0.0 2.0.0.0 3,5.0.0 3.5.0.0 3.5.0.0 3.5.0.0 2.0.0.0 3,5.0.0 3.5.0.0 3.5.0.0

3d67edlf87d44c89 3d67edlf87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67edlf87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67edlf87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 3d67edlf87d44c89 3d67ed1f87d44c89 3d67ed1f87d44c89 b03f5f7flld50a3a b77a5c561934e089 b77a5c561934e089 b03f5f7flld50a3a b03f5f7flld50a3a 31 bf3856ad364e35 b03f5f7flld50a3a b03f5f7flld50a3a b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089 b77a5c561934e089

MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL MSIL x86 MSIL MSIL MSIL MSIL x86 MSIL MSIL MSIL

Figure 2-1. Shared assemblies in the General Assembly Cache

You can instantiate any one of these assemblies by using the Load() method of the Assembly object. In Listing 2-3, you obtain a Type reference to the Infragistics UltraListBar control.

Listing 2-3. Referencing an Assembly in the GAC

Assembly oAssembly =

Assembly.Load(@"Infragistics2.Win.UltraWinListBar.v9.1, Version=9.1.20091.1000, PublicKeyToken=7dd5c3163f2cd0cb, Culture = );

Type oType = oAssembly.GetType("Infragistics.Win.UltraWinListBar.UltraListBar");

Examining Classes

When you have a Type object reference to your class, you can access its various components via the GetMethods(),GetFields(), GetProperties(), GetInterfaces(), GetConstructors(), and GetEvents() methods. Each of these methods is examined individually in the following sections.

Because the Reflection classes offer a rather labyrinthine layout of an assembly, this chapter focuses on the properties and methods you most use in dynamic programming.

0 0

Post a comment