Reflection is about runtime access to type metadata
read-only information
once defined, type information is immutable
type information
fields, methods, properties, etc
instance information
values of fields
method invocation and results
CLR Reflection can also interact with instances
obtain/set properties
invoke methods
construct objects (via constructors)
get/set fields
... even through language access barriers
Reflection API (System.Reflection
)
System.Type
: Type metadata
base class, interfaces, fields, methods, constructors, events, ...
pretty much everything but comments
see also System.Reflection.TypeInfo
Assembly
Module
Reflection API (System.Reflection
)
FieldInfo
MethodInfo
MethodBase
ConstructorInfo
PropertyInfo
EventInfo
System.Type is information about a type
think of it as the runtime representation of code
we deliberately use the more generic term "Type", not "class"
System.Type provides metadata for all types in the CLR, including value types, arrays, enums, everything
provides access to metadata
that is, information about the code for the type: methods, fields, etc
Obtaining Type instances
language operators
C# typeof
, C++ typeid
, VisualBasic TypeOf
, etc
System.Object.GetType()
(instance method)
System.Type.GetType("name")
(static method)
Module APIs
Assembly APIs
GetTypeFromHandle()
construct one at runtime!
System.Type API: Overview
FullName
AssemblyQualifiedName
the assembly-qualified name for this type
BaseType
from what does this type inherit?
DeclaringType
in what other type does this type nest?
Assembly
get the assembly in which this type is defined
Module
get the module in which this type is defined
System.Type API: Interrogatory
vast collection of "Is..." properties
some are to determine what this type is
IsClass, IsArray, IsEnum, IsGenericType, ...
some determine the modifiers on this type
IsAbstract, IsSealed, ...
some provide information about its surroundings
IsNestedPublic, IsNestedPrivate, IsNotPublic, ...
see API docs for full details
System.Type API: Components
Attributes
return all the attributes declared on this type
GetField()/GetFields()
return all or named fields on this type
GetProperty()/GetProperties()
return all or named properties on this type
GetInterfaces()
return all the interfaces implemented or inherited
GetMethod()/GetMethods()
return all or named methods on this type
GetConstructor/GetConstructors()
return all or typed constructors on this type
System.Reflection.PropertyInfo objects
represent a property on a type
are NOT tied to a particular object instance
contain the type of the property
properties on the PropertyInfo describe the property's modifiers
static, public/private/etc
CanRead
/ CanWrite
: read-only or write-only (or both)
GetMethod
: obtain the MethodInfo for the property accessor
SetMethod
: obtain the MethodInfo for the property mutator
Obtaining a PropertyInfo
Type.GetProperty()
takes a name of the property
Type.GetProperties()
obtains some/all the Properties in the type (including inherited)
Note that by default non-public properties will not be visible
use a BindingFlags
enumeration to specify the scope to search
BindingFlags.Public
: all public properties
BindingFlags.NonPublic
: all non-public properties
BindingFlags.Instance
: all instance properties
BindingFlags.Static
: all static properties
System.Reflection.MethodInfo
objects
represent a method on a type
are NOT tied to a particular object instance
contain the type of the method's parameters and return type
properties on the MethodInfo describe the method's modifiers
static, public/private/etc, abstract, final (sealed), virtual, etc
return type
surprisingly, parameter types is a method (GetParameters())
Obtaining a MethodInfo
Type.GetMethod()
takes a name and signature of a method
Type.GetMethods()
obtains some/all the methods in the type (including inherited)
Note that by default non-public methods will not be visible
use a BindingFlags enumeration to specify the scope to search
BindingFlags.Public: all public methods
BindingFlags.NonPublic: all non-public methods
BindingFlags.Instance: all instance methods
BindingFlags.Static: all static methods
System.Reflection.ConstructorInfo objects
represent a constructor on a type
contain the type of the constructor's parameters
properties on the ConstructorInfo describe the constructor's modifiers
public/private/etc, etc
surprisingly, parameter types is a method (GetParameters())
Obtaining a ConstructorInfo
Type.GetConstructor()
takes a signature of a constructor
Type.GetConstructors()
obtains some/all the constructors in the type
Note that by default non-public methods will not be visible
use a BindingFlags enumeration to specify the scope to search
BindingFlags.Public: all public constructors
BindingFlags.NonPublic: all non-public constructors
System.Reflection.FieldInfo
objects
represent a field on a type
are NOT tied to a particular object instance
contain the type of the field
properties on the FieldInfo describe the field's modifiers
static, public/private/etc, "init-only" (can it be initialized in the constructor), etc
Obtaining a FieldInfo
Type.GetField()
takes a name of the field
Type.GetFields()
obtains some/all the fields in the type (including inherited)
Note that by default non-public fields will not be visible
use a BindingFlags enumeration to specify the scope to search
BindingFlags.Public: all public fields
BindingFlags.NonPublic: all non-public fields
BindingFlags.Instance: all instance fields
BindingFlags.Static: all static fields
.NET Reflection is a powerful way to access class metadata at runtime
to discover types and their shape at runtime
to manipulate instances of types without prior knowledge
Who is this guy?
Architect, Engineering Manager/Leader, "force multiplier"
Principal -- Neward & Associates
http://www.newardassociates.com
Educative (http://educative.io) Author
Performance Management for Engineering Managers
Author
Professional F# 2.0 (w/Erickson, et al; Wrox, 2010)
Effective Enterprise Java (Addison-Wesley, 2004)
SSCLI Essentials (w/Stutz, et al; OReilly, 2003)
Server-Based Java Programming (Manning, 2000)