13.9 Inspect an Assembly s Evidence


13.9 Inspect an Assembly's Evidence

Problem

You need to inspect the evidence that the runtime assigned to an assembly.

Solution

Obtain a System.Reflection.Assembly object that represents the assembly in which you are interested. Get the System.Security.Policy.Evidence collection from the Evidence property of the Assembly object, and access the contained evidence objects using the GetEnumerator , GetHostEnumerator , or GetAssemblyEnumerator methods of the Evidence class.

Discussion

The Evidence class represents a collection of evidence objects. The read-only Evidence property of the Assembly class returns an Evidence collection object that contains all of the evidence objects that the runtime assigned to the assembly as the assembly was loaded.

The Evidence class actually contains two collections, representing different types of evidence: host evidence and assembly evidence. Host evidence includes those evidence objects assigned to the assembly by the runtime or the trusted code that loaded the assembly. Assembly evidence represents custom evidence objects embedded into the assembly at build time. The Evidence class implements the following three methods for enumerating the evidence objects it contains:

  • GetEnumerator

  • GetHostEnumerator

  • GetAssemblyEnumerator

The GetEnumerator method returns a System.Collections.IEnumerator that enumerates all of the evidence objects contained in the Evidence collection. The GetHostEnumerator and GetAssemblyEnumerator methods return an IEnumerator instance that enumerates only those evidence objects from the appropriate collection.

The ViewEvidenceExample listed here demonstrates how to display the host and assembly evidence of an assembly to the console. The example relies on the fact that all standard evidence classes override the Object.ToString method to display a useful representation of the evidence object's state. Although interesting, this example doesn't always show the evidence that an assembly would have when loaded from within your program. The runtime host (such as the Microsoft ASP.NET or Microsoft Internet Explorer runtime hosts ) is free to assign additional host evidence as it loads an assembly.

 using System; using System.Reflection; using System.Collections; using System.Security.Policy; public class ViewEvidenceExample {     public static void Main(string[] args) {         // Load the specified assembly.         Assembly a = Assembly.LoadFrom(args[0]);         // Get the Evidence collection from the          // loaded assembly.         Evidence e = a.Evidence;         // Display the Host Evidence.         IEnumerator x = e.GetHostEnumerator();         Console.WriteLine("HOST EVIDENCE COLLECTION:");         while(x.MoveNext()) {             Console.WriteLine(x.Current.ToString());         }         // Display the Assembly Evidence.         x = e.GetAssemblyEnumerator();         Console.WriteLine("ASSEMBLY EVIDENCE COLLECTION:");         while(x.MoveNext()) {             Console.WriteLine(x.Current.ToString());         }     } } 

All of the standard evidence classes provided by the .NET Framework are immutable, ensuring that you can't change their values after the runtime has created them and assigned them to the assembly. In addition, you can't add or remove items while you are enumerating across the contents of a collection using an IEnumerator , otherwise , the MoveNext method throws a System.InvalidOperationException .




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net