Kalpesh Kinariwala: Pantheon Development installs boilers ...
Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.
-
Upload
eunice-turner -
Category
Documents
-
view
225 -
download
4
Transcript of Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.
![Page 1: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/1.jpg)
Kalpesh Padia
Reflection in .Net
![Page 2: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/2.jpg)
OVERVIEW
04/21/23 2
![Page 3: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/3.jpg)
INTRODUCTION• Reflection – What it is?
• Why reflection?
04/21/23 3
![Page 4: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/4.jpg)
LOOKING BACK: AUTOMATION AND COM TYPE INFO
04/21/23 4
• TypeLibraries contain type information• Exploration through ITypeLib/ITypeInfo
• Attributes describe behavior of elements • e.g. Directionality of parameters• e.g. Method role (instance method, property)
• Dynamic Invocation using IDispatch
• Type system supports most popular simple types
• Dynamic types using VARIANT
• Arrays passed using SAFEARRAY
• Strings expressed as BSTR
![Page 5: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/5.jpg)
WHAT'S WRONG WITH THAT?
04/21/23 5
• Automation client model favors VisualBasic
• Automation server model favors C/C++
• ITypeInfo and IDispatch are way too complicated
• Information model is limited
• No sufficient support for complex types
• SAFEARRAYs are a pain from C/C++
• BSTRs are difficult to manage from C/C++
• Type information separate from code
![Page 6: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/6.jpg)
.NET REFLECTION CORE CONCEPTS
04/21/23 6
• Metadata• Single location for type information and code• Code is literally contained within type information• Every .NET object can be queried for its type• Types' metadata can be explored with Reflection
• Dynamic Type System• Highly dynamic and language independent• Types may be extended and built at run-time• Allows on-the-fly creation of assemblies• .NET Compilers use .NET to emit .NET code
![Page 7: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/7.jpg)
EXPLORING METADATA
04/21/23 7
![Page 8: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/8.jpg)
METADATA: TYPE INFO AT RUNTIME
04/21/23 8
GetType()
![Page 9: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/9.jpg)
WHO ARE YOU?
04/21/23 9
• Accessing meta-data: System.Object.GetType()• All .NET classes (implicitly) inherit System.Object• Available on every .NET class; simple types too
• Explicit language support for type meta-data• C#, JScript.NET: typeof(…)• VB.NET: If TypeOf … Is … Then …
• Determining Type Identity• Types have unique identity across any assembly• Types can be compared for identity• if ( a.GetType() == b.GetType() ) { … };
![Page 10: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/10.jpg)
SYSTEM.TYPE
04/21/23 10
• Access to meta-data for any .NET type
• Returned by System.Object.GetType()
• Allows drilling down into all facets of a type• Category: Simple, Enum, Struct or Class• Methods and Constructors, Parameters and Return• Fields and Properties, Arguments and Attributes• Events, Delegates and Namespaces
![Page 11: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/11.jpg)
WHAT ARE YOU?
04/21/23 11
• Value, Interface or Class?
• IsValueType, IsInterface, IsClass
• Public, Private or Sealed ?
• IsNotPublic, IsSealed
• Abstract or Implementation?
• IsAbstract
• Covers all possible properties of a managed type
• Very intuitive API, no "Parameter Hell"
![Page 12: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/12.jpg)
ANYTHING SPECIAL ABOUT YOU?
04/21/23 12
• Special Memory Layout?
• IsAutoLayout
• IsExplicitLayout
• IsLayoutSequential
• COM Objects?
• IsCOMObject
• More…
• IsUnicodeClass
• IsSpecialName, etc.
![Page 13: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/13.jpg)
NOW TELL ME WHAT YOU HAVE!
04/21/23 13
• Finding and Exploring Members
• MemberInfo: GetMembers(), FindMembers()
• Exploring Fields and Properties
• FieldInfo: GetFields(), PropertyInfo: GetProperties()
• Exploring Constructors, Methods and Events
• GetConstructors(), GetMethods(), GetEvents()
• Exploring attributes, determining implemented interfaces, enumerating nested types, …
• Summary: Everything you may ever want to know
![Page 14: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/14.jpg)
TYPE AND INSTANCES
04/21/23 14
• Type Safety First! Type checking at runtime• C#: if ( o is Customer ) { … }• VB: If TypeOf o Is Customer Then … End If
• Dynamic Invocation through Reflection• Support for late binding• MethodInfo.Invoke()• FieldInfo.SetValue()• PropertyInfo.SetValue()
![Page 15: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/15.jpg)
DETAIL INFORMATION
04/21/23 15
![Page 16: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/16.jpg)
MEMBERINFO
04/21/23 16
• Base class for all "member" element descriptions
• Fields, Properties, Methods, etc.
• Provides member kind, name and declaring class
MemberInfo
MethodBase ParameterInfo FieldInfo EventInfo PropertyInfo
MethodInfo ConstructorInfo
![Page 17: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/17.jpg)
FIELDINFO, PROPERTYINFO
04/21/23 17
• FieldInfo• Field data type and attributes• Static or Instance field, protection level• Can set value through reflection • Provides low-level, direct access with SetValueDirect()
• PropertyInfo• Property type and attributes• Test methods for readability, writeability• "get" and "set" MethodInfo• Indexer ParameterInfo• Can invoke "set" and "get" through Reflection
![Page 18: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/18.jpg)
METHODINFO, CONSTRUCTORINFO
04/21/23 18
• MethodInfo• Return type and attributes• List of all parameters as ParameterInfo array• Detail implementation information through flag-field• Can invoke method through Reflection
• ConstructorInfo• Same features as MethodInfo, just for constructors
![Page 19: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/19.jpg)
ATTRIBUTES
04/21/23 19
• Custom attributes are the killer-app for Reflection!
• Attributes enable declarative behavior
• Attributes allow data augmentation
![Page 20: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/20.jpg)
THE BIGGER PICTURE
04/21/23 20
• Types know their Module, Modules know their types
• Modules know their Assembly and vice versa
• Code can browse and search its entire context
![Page 21: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/21.jpg)
BUILDING TYPES AT RUNTIME
04/21/23 21
![Page 22: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/22.jpg)
INTRODUCING SYSTEM.REFLECTION.EMIT
04/21/23 22
• Full representation of physical structure
• Allows building modules and assemblies at runtime
• Transient code only used at runtime
• Persistent code for reuse
• Create classes, types and emit IL
• Used by .NET compilers to build .NET apps
![Page 23: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/23.jpg)
WHY? SOME SCENARIOS…
04/21/23 23
• Build classes dynamically from script-like code• ASP.NET, Regular Expressions do just that !
• Generate code from visual development tools• e.g. build interfaces, base classes from UML
• Create dynamic wrappers for existing code
• Transfer code-chunks to remote machines• Distributed processing scenarios (like SETI@home)
![Page 24: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/24.jpg)
BUILDING ASSEMBLIES
04/21/23 24
• System.Reflection.Emit.AssemblyBuilder
• Dynamically create new assemblies• Create manifest and resources• Add modules• Specify security requirements
• Can be persisted as files or held in memory
• Act and behave like any other assembly
![Page 25: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/25.jpg)
BUILDING MODULES
04/21/23 25
• System.Reflection.Emit.ModuleBuilder
• Modules contain types, classes and code
• Allows creating fully functional modules with code
• Can associate source code
• Emit debug symbols
• Acts like any module created by any .NET compiler
![Page 26: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/26.jpg)
WRITING IL
04/21/23 26
• Emit accepts IL (Intermediate Language) code
• Same code as emitted by C#, VB.NET, Eiffel#
• IL is optimized and translated into machine code
• Reflection.Emit puts you "on-par"
• Same backend as .NET compilers
• Same access to code-generation
• Languages are mostly scanners and parsers
• For more info see System.CodeDOM
![Page 27: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/27.jpg)
PUTTING IT TOGETHER
04/21/23 27
![Page 28: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/28.jpg)
VISUALSTUDIO.NET AND REFLECTION
04/21/23 28
![Page 29: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/29.jpg)
WHAT ASP.NET DOES WITH A PAGE
04/21/23 29
![Page 30: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/30.jpg)
QUESTIONS
04/21/23 30
![Page 31: Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/2015 2.](https://reader036.fdocuments.net/reader036/viewer/2022062423/56649e615503460f94b5d19a/html5/thumbnails/31.jpg)
THANK YOU
04/21/23 31