Understanding Assemblies: and Reflecting On Them
Understanding Assemblies: and Reflecting On Them
Martin Kropp
University of Applied Sciences Northwestern Switzerland
Learning Target
• You
– can explain the concept and format of .Net assemblies
– can explain the content of a .Net assembly
– can explain the concept of the global assembly cache
– can develop a public assembly
– understand and can explain the .Net Reflection concept
– can apply the .Net reflection API to analyze assemblies
– can apply the .Net reflection API to dynamically load assemblies and
classes
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 2
Content
Assemblies
• Role and Format of .Net Assemblies
• The Global Assembly Cache
• Managing Private and Public Assemlies
Reflection
• Type Information in .Net Assemblies
• Loading and Analyzing .Net Assembly
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 3
Role of .Net Assemblies
• Assemblies Promote Code Reuse
– Are the smallest executable unit
• Assemblies Establish a Type Boundary
– Types in a .Net assembly are considered to be unique
• Assemblies Are Versionable Units
– Contain exact version info
• Assemblies Are Self-Describing
– Contain all type info
• Assemblies Are Configurable
– Can be stored anywhere
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 4
The Format of a .Net Assembly
Windows File
Win 32 Header PE/COFF Header
Info for CLR Runtime CLR Header
for execution
version number
Manifest public key
interface description
of
Detailed type info
Type metadata - classes
about assembly types
- methods
- variables
The complete IL code CIL code - parameters
- types
Optional
Resources
Icons, Graphics,
Sounds, …
• Ildasm.exe
• dumpbin.exe [/headers | /clrheader]
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 5
Format of .Net Assembly – ILDASM - View
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 6
Assembly Manifest
External References
Assembly Information
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 7
… and the IL Code
• HotelBroker.AddHotel
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 8
Managing Private Assemblies
• Usually located in the executable’s directory
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 9
Configuring Private Assemblies
• Can also be stored in any other location using the
applications config file (AppName.exe.config)
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="MyLibraries"/>
</assemblyBinding>
</runtime>
</configuration>
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 10
Managing Shared Assemblies
• Share the Assembly with other applications
– Can be used several applications
– Only one copy stored
• Various GACs
– GAC – .net 1.x
– GAC_32 / GAC_64
– GAC_MSIL
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 11
The Global Assembly Cache
• A View on the GAC
– (using Explorer plugin “Assembly Cache Viewer“ (shfushion.dll)
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 12
Installing Assemblies into the GAC
• No direct Access
• Installing with the GACUtil (gacutil.exe)
> gacutil -i GlobalLib.dll
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 13
Strong Name for Assemblies
• "strong" assembly name consists of:
– name
– version number (major.minor.build.revision)
– culture attribute (usually neutral)
– hash value of a public key
e.g. MyLib,
Version=1.2.745.18000,
Culture=en-US,
PublicKeyToken=13a3f300fff94cef
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 14
Strong Name Tool (sn.exe)
Command line tool for
• generating pairs of private & public keys
> sn -k mykeys.snk
• extracting the public key from a key pair
> sn -p mykeys.snk mypubkey.snk
• delayed signing of assemblies
> sn -R MyLib.dll mykeys.snk
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 15
Signing an Assembly
sn -k mykeys.snk
Sign Directly Delay-Signing
sn -p mykeys.snk mypubkeys.snk
MyLib.cs MyLib.cs
[assembly:AssemblyVersionAttribute("4.3.2.*")] [assembly: AssemblyDelaySign(true)]
[assembly:AssemblyVersionAttribute("4.3.2.*")]
sn -R MyLib.dll mykeys.snk
MyLib.dll (signed)
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 16
Reflection
• Permits access to meta-information of types at run-time
• System.Reflection allows:
– Getting meta-information about assemblies, modules and types
– Getting meta-information about the members of a type
– Dynamic creation of instances of a type at run-time
– Search for methods and their dynamic invocation at run-time
– Accessing values of properties and fields of an object
– Design of new types at run time
namespace System.Reflection.Emit
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 17
Reflection Class Hierarchy
Loads assemblies and their meta-data Provides access to its
Assembly meta-data
GetTypes()
GetFields() * FieldInfo
GetMethods() * MethodInfo
MethodBase
GetConstructors() * ConstructorInfo MemberInfo
GetProperties() * PropertyInfo
GetEvents() * EventInfo
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 18
Example Reflection
namespace Hello {
• Loading the assembly "HelloWorld.exe": public class HelloWorld {
public static void Main(string[] args)
{ ... }
Assembly a = Assembly.Load("HelloWorld"); public override string ToString() {
… }
}
• Print all existing types in a given assembly }
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 19
Example Reflection (2)
• Create a new instance of a given type
Assembly a = Assembly.Load("HelloWorld");
object o = a.CreateInstance("Hello.HelloWorld");
Type hw = a.GetType("Hello.HelloWorld");
MethodInfo mi = hw.GetMethod("ToString");
object retVal = mi.Invoke(o, null);
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 20
Reflection.Emit
• Reflection.Emit allows creation of assemblies and types at run-time
– creation of assemblies
– creation of new modules
– creation of new types
– creation of symbolic meta-information of existing modules
Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 21