Browse Source

Merge pull request #93 from ErisApps/feature/Add_ReflectionUtil_helper_method

Add helper method for converting a property name to the compiler-generated backing field name
nike4613 9 months ago
committed by GitHub
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions
  1. +13

+ 13
- 0
IPA.Loader/Utilities/ReflectionUtil.cs View File

@ -130,6 +130,19 @@ namespace IPA.Utilities
return copy;
/// <summary>
/// Converts the property name to the one of the compiler-generated backing field.
/// This can be used for the field-based reflection when you want to set the value of a get-only property
/// </summary>
/// <param name="propertyName">Name of the property</param>
/// <returns>Name of the backing field</returns>
/// <remarks>
/// Only works for properties with compiler-generated backing fields.
/// This is only a simple method and doesn't have any guarantees to work 100% of the time across different compilers/runtimes.
/// See <a href="">this link</a> for more info.
/// </remarks>
public static string ToCompilerGeneratedBackingField(string propertyName) => $"<{propertyName}>k__BackingField";
private static void CopyForType(Type type, Component source, Component destination)
FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);