Line 1: |
Line 1: |
| {{../../header}} | | {{../../header}} |
| | | |
− | The reflection API lets you access fields, properties, or methods you otherwise couldn't access. You can use it from <tt>helper.Reflection</tt> in your entry method, or <tt>this.Helper.Reflection</tt> elsewhere in your entry class. | + | The reflection API lets you access fields, properties, or methods you otherwise couldn't access. You can use it from <samp>helper.Reflection</samp> in your entry method, or <samp>this.Helper.Reflection</samp> elsewhere in your entry class. |
| | | |
| ==Intro== | | ==Intro== |
Line 8: |
Line 8: |
| ==Basic reflection== | | ==Basic reflection== |
| ===Overview=== | | ===Overview=== |
− | SMAPI provides three overloaded methods to access code: <tt>GetField</tt>, <tt>GetProperty</tt>, and <tt>GetMethod</tt>. Each one takes three arguments: | + | SMAPI provides three overloaded methods to access code: <samp>GetField</samp>, <samp>GetProperty</samp>, and <samp>GetMethod</samp>. Each one takes three arguments: |
| {| class="wikitable" | | {| class="wikitable" |
| |- | | |- |
Line 14: |
Line 14: |
| ! purpose | | ! purpose |
| |- | | |- |
− | | <tt>obj</tt> or <tt>type</tt> | + | | <samp>obj</samp> or <samp>type</samp> |
| | The instance (or type if static) which has the private field/property/method you want to access. | | | The instance (or type if static) which has the private field/property/method you want to access. |
| |- | | |- |
− | | <tt>name</tt> | + | | <samp>name</samp> |
| | The name of the private field/property/method. | | | The name of the private field/property/method. |
| |- | | |- |
− | | <tt>required</tt> | + | | <samp>required</samp> |
− | | Whether to throw a descriptive exception if the field/property/method isn't found. Default true. If set to false, it will return <tt>null</tt> instead and you should validate it yourself. | + | | Whether to throw a descriptive exception if the field/property/method isn't found. Default true. If set to false, it will return <samp>null</samp> instead and you should validate it yourself. |
| |} | | |} |
| | | |
Line 27: |
Line 27: |
| | | |
| ===Fields and properties=== | | ===Fields and properties=== |
− | <tt>GetField</tt> and <tt>GetProperty</tt> are used the same way. Both return an object with two methods: <tt>GetValue</tt> returns the current field/property value, and <tt>SetValue</tt> overrides their value. | + | <samp>GetField</samp> and <samp>GetProperty</samp> are used the same way. Both return an object with two methods: <samp>GetValue</samp> returns the current field/property value, and <samp>SetValue</samp> overrides their value. |
| | | |
| You can get the value directly: | | You can get the value directly: |
Line 45: |
Line 45: |
| | | |
| ===Methods=== | | ===Methods=== |
− | <tt>GetMethod</tt> returns an object with one overloaded method. The <tt>Invoke</tt> comes in two forms: | + | <samp>GetMethod</samp> returns an object with one overloaded method. The <samp>Invoke</samp> comes in two forms: |
− | * <tt>Invoke()</tt> calls a method with no return value. | + | * <samp>Invoke()</samp> calls a method with no return value. |
− | * <tt>Invoke<T>()</tt> calls a method which returns a value, where <tt>T</tt> is the expected return type. | + | * <samp>Invoke<T>()</samp> calls a method which returns a value, where <samp>T</samp> is the expected return type. |
| | | |
− | For example, this calls the private <tt>TV.getFortuneForecast</tt> method and stores the value it returns: | + | For example, this calls the private <samp>TV.getFortuneForecast</samp> method and stores the value it returns: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| string forecast = this.Helper.Reflection | | string forecast = this.Helper.Reflection |
Line 56: |
Line 56: |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | If the method expects arguments, you can just add those to the <tt>Invoke</tt> method: | + | If the method expects arguments, you can just add those to the <samp>Invoke</samp> method: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| Vector2 spawnTile = new Vector2(25, 25); | | Vector2 spawnTile = new Vector2(25, 25); |