Exploring .NET with reflection
You do not have to guess what a class can do. A program can ask: this lesson lists the properties, methods and parameters of a class, and shows where to find the same information in the manual.
What you will learn#
- what reflection is,
- how to get a
Typefrom a name or from an object, - listing properties, methods and their parameters,
- enums and their values,
- where the manual gets its class reference from.
Reflection#
Reflection means that a program looks at its own types: names, members, parameters. .NET offers it in the class Type and the namespace System.Reflection. It is how the class reference of this manual was made: a Ring program asked every wrapped class what it contains.
The program#
# exploring .NET from Ring - ask a class what it can do
load "dotnet.ring"
using {
"System"
"System.Text"
"System.Reflection"
}
func main
# Reflection means: a program looks at its own types. Start from a type name.
oType = new Type().GetType([ :typeName = "System.Text.StringBuilder" ])
? "Type : " + oType.FullName
? "Is a class : " + oType.IsClass
? "Base type : " + oType.BaseType.FullName
? "Assembly : " + oType.Assembly.GetName(NULL).Name
# Its public properties
? nl + "Properties:"
aProperties = oType.GetProperties(NULL).toList()
for oProperty in aProperties
? " " + oProperty.Name + " : " + oProperty.PropertyType.Name + iif(oProperty.CanWrite," (read/write)"," (read only)")
next
# Its methods: many names appear several times (overloads), so list each name once
aNames = []
aMethods = oType.GetMethods(NULL).toList()
for oMethod in aMethods
if find(aNames,oMethod.Name) = 0
aNames + oMethod.Name
ok
next
aNames = sort(aNames)
? nl + "The class has " + len(aNames) + " method names, for example:"
for nIndex = 1 to 8
? " " + aNames[nIndex]
next
# The overloads of one method, with their parameters
aVersions = []
aMethods = oType.GetMethods(NULL).toList()
for oMethod in aMethods
if oMethod.Name = "Append"
aParameters = oMethod.GetParameters().toList()
if len(aParameters) = 1
aVersions + ("Append(" + aParameters[1].ParameterType.Name + " " + aParameters[1].Name + ")")
ok
ok
next
? nl + "Append has " + len(aVersions) + " versions with 1 parameter, the first ones are:"
for nIndex = 1 to 4
? " " + aVersions[nIndex]
next
# An object knows its own type
oSB = new StringBuilder("hi")
? nl + "An object knows its type: " + oSB.GetType().Name
# An enum: its names are the texts that Ring uses
oDays = new Type().GetType([ :typeName = "System.DayOfWeek" ])
? "Values of DayOfWeek: " + joinList(new Enum().GetNames(oDays).toList())
# Your own tools: dnDescribe returns the public API of a type as lists
aApi = dnDescribe("System.Math")
? nl + "dnDescribe('System.Math') returned a list with " + len(aApi) + " parts"
func iif lCondition, xYes, xNo
if lCondition
return xYes
ok
return xNo
func joinList aList
cText = ""
for xItem in aList
if cText != ""
cText += ", "
ok
cText += xItem
next
return cTextType : System.Text.StringBuilder
Is a class : 1
Base type : System.Object
Assembly : System.Private.CoreLib
Properties:
Capacity : Int32 (read/write)
MaxCapacity : Int32 (read only)
Length : Int32 (read/write)
Chars : Char (read/write)
The class has 22 method names, for example:
Append
AppendFormat
AppendJoin
AppendLine
Clear
CopyTo
EnsureCapacity
Equals
Append has 20 versions with 1 parameter, the first ones are:
Append(String value)
Append(StringBuilder value)
Append(Boolean value)
Append(Char value)
An object knows its type: StringBuilder
Values of DayOfWeek: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
dnDescribe('System.Math') returned a list with 14 partsHow it works#
A Type#
A Type describes a class. Two ways to get one:
oType = new Type().GetType([ :typeName = "System.Text.StringBuilder" ]) # from the full name
oType = oObject.GetType() # from an object that existsIts properties say what it is: FullName, Namespace, IsClass, BaseType, Assembly.
Properties and methods#
GetProperties(NULL) and GetMethods(NULL) return arrays of PropertyInfo and MethodInfo; toList() turns an array into a Ring list. Each has a Name. A PropertyInfo has PropertyType and CanWrite; a MethodInfo has GetParameters(), whose items have ParameterType and Name.
A method name appears once per overload, so the program collects distinct names. StringBuilder.Append has 20 versions with one parameter alone: one per type that can be appended. This is why Ring offers one Append(value): the bridge looks at your value and calls the right version.
Enums#
Enum.GetNames(oType) lists the names of an enum type. These are exactly the texts that you use in Ring: "Monday", "Sunday".
dnDescribe#
dnDescribe("System.Math") is what the class generator uses. It returns lists: the constructors, methods, members, the indexer and events. You rarely need it; the reflection classes above are friendlier.
Using the manual instead#
You often need only to look. The class reference shows, for every wrapped class, its constructors, properties, methods with the Ring calls, and events — produced by exactly this kind of reflection. Use search (/) to jump to a class by name.
What you learned#
- reflection lets a program inspect types,
GetProperties,GetMethods,GetParameters,- overloads show up as repeated names,
- the class reference is made from reflection.
Exercises#
- List the public properties of
System.IO.FileInfoand which of them can be written. - Print every overload of
Math.Roundwith its parameter types and names. How many are there? - List the values of
System.IO.FileAttributes. - Write a function
describe(cTypeName)that prints the name, the base type and the number of methods and properties of any type.