Finding assembly entry points

Some assemblies contain entry points. Think of entry points as the "starting method" for an assembly. The most obvious example of an entry point for an assembly is the Main() method found in C#-based executables. The CLR loads an executable, searches for the entry point for the assembly, and begins executing with that entry point method.

DLL-based assemblies, by contrast, do not typically have entry points. These assemblies generally hold resources or types that are used by other pieces of code, and they are passive in that they wait to be called before any of the code in the assembly is executed.

The Assembly class contains a property called EntryPoint. The EntryPoint property is a value of a type called MethodInfo, which is found in the .NET System.Reflection namespace. The MethodInfo class describes the specifics of a method, and calling ToString() on an object of type MethodInfo returns a string that describes the method's return type, name, and parameters. The EntryPoint property is null if the assembly reference does not have an entry point, or a valid MethodInfo object if the assembly reference does have an entry point, as shown in Listing 31-3.

Listing 31-3: Working with an Assembly Entry Point using System;

using System.Reflection;

public class MainClass {

static void Main(string[] args) {

Assembly EntryAssembly;

EntryAssembly = Assembly.GetEntryAssembly(); if(EntryAssembly.EntryPoint == null)

Console.WriteLine("The assembly has no entry point.");

else

Console.WriteLine(EntryAssembly.EntryPoint.ToString());

Compiling and executing Listing 31-3 sends the following information to the console:

Void Main(System.String[])

In the simple example of Listing 31-3, the EntryPoint property is never null, but it is always good practice to check for the possibility of a null value, especially with more complicated pieces of code.

0 0

Post a comment