0% found this document useful (0 votes)
41 views21 pages

Understanding Assemblies: and Reflecting On Them

This document discusses .NET assemblies and reflection. It covers: - The role and format of .NET assemblies, including that they promote code reuse, establish type boundaries, are versionable units, and are self-describing. - Managing private and public assemblies, including storing private assemblies, configuring their locations, and installing shared assemblies into the global assembly cache (GAC). - The concept of reflection, which allows accessing metadata at runtime to dynamically load and analyze assemblies and classes. Key classes for reflection like Assembly, Type, and MemberInfo are described. - An example of using reflection to load an assembly.

Uploaded by

sanjeev kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views21 pages

Understanding Assemblies: and Reflecting On Them

This document discusses .NET assemblies and reflection. It covers: - The role and format of .NET assemblies, including that they promote code reuse, establish type boundaries, are versionable units, and are self-describing. - Managing private and public assemblies, including storing private assemblies, configuring their locations, and installing shared assemblies into the global assembly cache (GAC). - The concept of reflection, which allows accessing metadata at runtime to dynamically load and analyze assemblies and classes. Key classes for reflection like Assembly, Type, and MemberInfo are described. - An example of using reflection to load an assembly.

Uploaded by

sanjeev kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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

• .Net Probing Process


– The process how the runtime resolves the location of private external
assemblies
– 1. Look for DLL in clients assembly location
– 2. Look for EXE in clients assembly location
– 3. Look for either DLL or EXE in a subdirectory with same name as the
assembly in clients assembly location
– If not found “FileNot Found Exception”
– Usually stored in the local directory
– Can also be stored in any other location using the applications config file

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

• The Global Assembly Cache (GAC)


– A safe Win\System32 repository ☺
– Can store different versions of the same assembly
– Only limited and controlled access

• 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)

• The Actual path


C:\Windows\assembly\NativeImages1_v1.1.4322\mscorlib\1
.0.5000.0__b77a5c561934e089_1dc9747c\mscorlib.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

• Removing assemblies from the GAC


> gacutil -u GlobalLib

• Only signed assemblies can be stored


– Assembly is signed with a strong name

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.*")]

csc /t:library /keyfile:mykeys.snk MyLib.cs csc /t:library MyLib.cs

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()

* Used for meta-description of all types in the run-time system


BaseType
Provides access to the meta-information about its members
Type
Interfaces
*

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 }

Type[] types = a.GetTypes();


foreach (Type t in types)
Console.WriteLine(t.FullName); Hello.HelloWorld

• Print all existing methods of a given type


Type hw = a.GetType("Hello.HelloWorld");
MethodInfo[] methods = hw.GetMethods();
foreach (MethodInfo m in methods)
Console.WriteLine(m.Name); Main
ToString

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");

• Get method ToString(), which has no parameters


• Invoke the method

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

• System.Reflection.Emit supports realization of .NET compilers und interpreters

• Important classes of Reflection.Emit are


– AssemblyBuilder to define assemblies
– ModuleBuilder to define modules
– TypeBuilder to define types
– MethodBuilder to define methods
– ILGenerator to emit IL-code

Assemblies & Reflecting Institute for Mobile and Distributed Systems, M. Kropp 21

You might also like