310 likes | 458 Views
.Net Components And Types. Bassem Elkarablieh. Outline. Modules Assemblies Object Model Types Runtime Type Information Programming with Types. .net Code Journey. CLR Architecture. Modules. CLR programs reside into Modules Modules contain: Code(IL code) MetaData ( module description)
E N D
.Net Components And Types Bassem Elkarablieh
Outline • Modules • Assemblies • Object Model • Types • Runtime Type Information • Programming with Types
Modules • CLR programs reside into Modules • Modules contain: • Code(IL code) • MetaData ( module description) • Resources (any external resource) • Modules are not deployable components
Assemblies • Modules are the physical construct of the program that resides in file system • Assembly is a logical construct that interfaces with the CLR to access modules • Assemblies are references by a logical name • The CLR have a mechanism to identify assemblies
Assemblies continued • Assemblies are deployable modules • Each assembly has a manifest • Assemblies might have multiple modules • Only one manifest exists • Manifests describe the modules in the assembly • Assembly can be: • Executable application • Library
Compiling process • csc /t:module Speaker1.cs • csc /t:module Speaker2.cs • csc /t:library /addmodule:Speaker1.netmodule /addmodule:Speaker2.netmodule Speakers.cs • csc /t:exe /r:Speakers.dll Executive.cs
Manifests • Manifests contain: • Filenames of the internal modules • Type declaration of internal modules • Name of the assembly • MetaData Contain • List of all external dependencies • Type declarations of the assembly
Assembly names • Assembly names are four parts: • Logical friendly name • Version number • Culture • Public Key Token • Friendly names: simple name to reference the assembly • Culture: “en-us”,”ar-lb” Satellite
Versioning • Version number have the following structure: • Major.Minor.Build.Revision
Public Keys • Public keys resolves name confusions of assemblies • Public keys signs the module with the developer identity • Private keys can be used to digitally sign the assembly • Public key file can be generated using the sn.exe tool
Loading assemblies • If assembly is partially specified it can be loaded by friendly name • If assembly is fully specified , then the name should be resolved before the assembly is loaded
.net Types • Every class declared in .net is a type • Types can include: • Fields • Methods • Properties • Events
How to call a method • If the method is a member of the type , then 3 pointer references are used • If the method is a base-member then more references are used depending on the length of the hierarchy
Type Casting • One of the main properties of .net is type safety. • Disadvantage of type checking is performance. • Three types of castings are possible: • Upcast • Downcast • Sidecast
Type Casting • Casting is the process of a linear search and match of the handles in the interface table and the Class hierarchy. • To support casting two opcodes are used( invisible from user) • Isinsit (is keyword) • Castclass(as Keyword) • Both opcode take the object and the desired type metadata pointer as arguments.
Type checking • The System.Type class is a façade to the internal Type structure. • Provides methods to check type compatibility • IsCompatible, Issubclassof • To get the type handle of any object use the inherited object.getType()
System.Reflection • This namespace provide the Assembly object used in loading assemblies • The assembly object allows the accessing of the typemetadata. • Given the metadata we can get the type handle references. • Given the type Handel we can get member info.
System.Type • Assembly Asm = Assembly.Loadfrom(“x.dll”); • Type[] types = Asm.GetTypes(); • Foreach(Type t in types) • T.GetMembers() • T.GetMethods() • T.GetFields() • T.GetEvents()
Filtering Type selection • Using bindingflags • BindingFlags f = static|Instance|public|nonpublic|FlattenHirarchy • T.GetMembers(f); • We can also filter by name and the member type