From 43b0f0bb226b89218eabb5d09f3601cf622b89ce Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 12 May 2023 21:18:18 +0200 Subject: [PATCH] Add helper method for converting a property name to the compiler-generated backing field name --- IPA.Loader/Utilities/ReflectionUtil.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/IPA.Loader/Utilities/ReflectionUtil.cs b/IPA.Loader/Utilities/ReflectionUtil.cs index 6f0a0195..9dc16e6a 100644 --- a/IPA.Loader/Utilities/ReflectionUtil.cs +++ b/IPA.Loader/Utilities/ReflectionUtil.cs @@ -130,6 +130,19 @@ namespace IPA.Utilities return copy; } + /// + /// 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 + /// + /// Name of the property + /// Name of the backing field + /// + /// 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 this link for more info. + /// + 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);