public override void Bind(object obj, string name) { object value = MpReflection.GetValue(obj, name); Type type = value.GetType(); var res = Sync.ReadSyncObject(reader, type); MpReflection.SetValue(obj, name, res); }
private static Delegate ReadDelegate(ByteReader reader) { if (!reader.ReadBool()) { return(null); } var delegateType = SyncSerialization.ReadSync <Type>(reader); var type = SyncSerialization.ReadSync <Type>(reader); var methodName = SyncSerialization.ReadSync <string>(reader); object target = null; if (reader.ReadBool()) { var targetType = SyncSerialization.ReadSync <Type>(reader); var fieldPaths = GetFields(targetType).ToArray(); var fieldTypes = fieldPaths.Select(path => MpReflection.PathType(path)).ToArray(); target = Activator.CreateInstance(targetType); for (int i = 0; i < fieldPaths.Length; i++) { string path = fieldPaths[i]; string noTypePath = MpReflection.RemoveType(path); Type fieldType = fieldTypes[i]; object value; if (fieldType.IsCompilerGenerated()) { value = Activator.CreateInstance(fieldType); } else if (typeof(Delegate).IsAssignableFrom(fieldType)) { value = ReadDelegate(reader); } else { value = SyncSerialization.ReadSyncObject(reader, fieldType); } MpReflection.SetValue(target, path, value); } } return(Delegate.CreateDelegate( delegateType, target, AccessTools.Method(type, methodName) )); }
public static void SetPropertyOrField(this object obj, string memberPath, object value, object index = null) { MpReflection.SetValue(obj, memberPath, value, index); }