Changes

Jump to navigation Jump to search
m
Replace deprecated <source> tags with <syntaxhighlight> tags
Line 30: Line 30:     
You can get the value directly:
 
You can get the value directly:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
// get value of instance field
 
// get value of instance field
 
bool wasPet = this.Helper.Reflection.GetField<bool>(pet, "wasPetToday").GetValue();
 
bool wasPet = this.Helper.Reflection.GetField<bool>(pet, "wasPetToday").GetValue();
</source>
+
</syntaxhighlight>
    
Or you can keep a reference to the reflection data, and change the value separately:
 
Or you can keep a reference to the reflection data, and change the value separately:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
// set value of static field
 
// set value of static field
 
IReflectedField<int> soundTimer = this.Helper.Reflection.GetField<int>(typeof(Junimo), "soundTimer");
 
IReflectedField<int> soundTimer = this.Helper.Reflection.GetField<int>(typeof(Junimo), "soundTimer");
 
soundTimer.SetValue(100);
 
soundTimer.SetValue(100);
</source>
+
</syntaxhighlight>
    
If you need to access the field/property repeatedly, keeping the reflection object will improve performance.
 
If you need to access the field/property repeatedly, keeping the reflection object will improve performance.
Line 50: Line 50:     
For example, this calls the private <tt>TV.getFortuneForecast</tt> method and stores the value it returns:
 
For example, this calls the private <tt>TV.getFortuneForecast</tt> method and stores the value it returns:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
string forecast = this.Helper.Reflection
 
string forecast = this.Helper.Reflection
 
   .GetMethod(new TV(), "getFortuneForecast")
 
   .GetMethod(new TV(), "getFortuneForecast")
 
   .Invoke<string>();
 
   .Invoke<string>();
</source>
+
</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 <tt>Invoke</tt> method:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
Vector2 spawnTile = new Vector2(25, 25);
 
Vector2 spawnTile = new Vector2(25, 25);
 
this.helper.Reflection
 
this.helper.Reflection
 
   .GetMethod(Game1.getFarm(), "doSpawnCrow")
 
   .GetMethod(Game1.getFarm(), "doSpawnCrow")
 
   .Invoke(spawnTile);
 
   .Invoke(spawnTile);
</source>
+
</syntaxhighlight>
    
==Advanced reflection==
 
==Advanced reflection==
 
If you need to do more, you can access the underlying C# reflection types:
 
If you need to do more, you can access the underlying C# reflection types:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
FieldInfo field = this.Helper.Reflection.GetField<string>(…).FieldInfo;
 
FieldInfo field = this.Helper.Reflection.GetField<string>(…).FieldInfo;
 
MethodInfo method = this.Helper.Reflection.GetMethod(…).MethodInfo;
 
MethodInfo method = this.Helper.Reflection.GetMethod(…).MethodInfo;
</source>
+
</syntaxhighlight>
    
Or even use [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection C# reflection] directly. Note that SMAPI adds caching and optimisations which you'll need to handle yourself if you do this.
 
Or even use [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection C# reflection] directly. Note that SMAPI adds caching and optimisations which you'll need to handle yourself if you do this.
114

edits

Navigation menu