Game Development Reference
In-Depth Information
12 foreach(Type t in Assembly.GetExecutingAssembly().GetTypes())
13 {
14 Debug.Log (t.Name);
15 }
16 }
17 }
The following are the comments for code sample 8-2:
Lines 03-04 : Both the namespaces System and System.Reflection should
be included as they feature all classes and objects necessary for performing
reflection in .NET.
Line 12 : This foreach loop cycles through all classes (types) in the active
assembly (that is, the compiled code, including all your custom made
script files).
You can take the concept of reflection even further. For example, having listed all
types from code sample 8-2, you can even list the methods, properties, and variables
( Fields ) for a type. Refer to the following code sample 8-3 that, given a specific type
as an argument, will list all its public member variables:
//Function to list all public variables for class t
public void ListAllPublicVariables(Type t)
{
//Loop through all public variables
foreach(FieldInfo FI in t.GetFields(BindingFlags.Public |
BindingFlags.Instance)
{
//Print name of variable
Debug.Log (FI.Name);
}
}
More information on bitwise operations, as used in this code
sample, can be found online at http://www.blackwasp.co.uk/
CSharpLogicalBitwiseOps.aspx .
Most crucially, however, you can list the attributes assigned to a type too. This lets
you query a type for its metadata and examine its properties at runtime as shown in
the following code sample 8-4:
 
Search WWH ::




Custom Search