public GetSetGeneric(Type t, string name) { GetSetGeneric $this = this; this.Name = name; PropertyInfo property = t.GetProperty(name); if (property == null) { this.FieldInfo = t.GetField(this.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); this.Get = new Func <object, object>(this.FieldInfo.GetValue); this.Set = new Action <object, object>(this.FieldInfo.SetValue); this.IsStatic = this.FieldInfo.IsStatic; this.CollectionType = (this.FieldInfo.FieldType.GetInterface("IEnumerable", true) != null); return; } this.Info = property; this.CollectionType = (this.Info.PropertyType.GetInterface("IEnumerable", true) != null); MethodInfo getMethod = property.GetGetMethod(true); MethodInfo setMethod = property.GetSetMethod(true); this.IsStatic = getMethod.IsStatic; this.Get = ((object o) => getMethod.Invoke($this.IsStatic ? null : o, null)); this.Set = delegate(object o, object v) { setMethod.Invoke($this.IsStatic ? null : o, new object[] { v }); }; }
private static GetSet[][] GetAccessors(RuntimeTypeHandle type) { lock (PropertyAccess) { int index = 0; GetSet[][][] collection; if (!PropertyAccess.TryGetValue(type, out collection)) { collection = new GetSet[3][][]; PropertyAccess[type] = collection; } var accessors = collection[index]; if (accessors == null) { object vanilla = GetVanilla(type); bool canGetVanilla = false; if(vanilla != null) { canGetVanilla = !vanilla.Equals(null); } var acs = new List<GetSet>(); var props = UnitySerializer.GetPropertyInfo(type) .Where(p=>p.Name != "hideFlags") .Select(p=> new { priority = ((SerializationPriorityAttribute)p.GetCustomAttributes(false).FirstOrDefault(a=>a is SerializationPriorityAttribute)) ?? new SerializationPriorityAttribute(100), info=p}) .OrderBy(p=>p.priority.Priority).Select(p=>p.info); foreach (var p in props) { var getSet = new GetSetGeneric(p); if(!canGetVanilla) { getSet.Vanilla = null; } else { getSet.Vanilla = getSet.Get(vanilla); } acs.Add(getSet); } accessors = new GetSet[4][]; accessors[0] = acs.Where(a=>!a.IsStatic).ToArray(); accessors[2] = acs.ToArray(); acs.Clear(); var fields = UnitySerializer.GetFieldInfo(type) .Where(f=>f.Name != "hideFlags") .Select(p=> new { priority = ((SerializationPriorityAttribute)p.GetCustomAttributes(false).FirstOrDefault(a=>a is SerializationPriorityAttribute)) ?? new SerializationPriorityAttribute(100), info=p}) .OrderBy(p=>p.priority.Priority).Select(p=>p.info); foreach (var f in fields) { var getSet = new GetSetGeneric(f); if(!canGetVanilla) { getSet.Vanilla = null; } else { getSet.Vanilla = getSet.Get(vanilla); } acs.Add(getSet); } accessors[1] = acs.Where(a=>!a.IsStatic).ToArray(); accessors[3] = acs.ToArray(); collection[index] = accessors; } return accessors; } }
private static GetSet[][] GetAccessors(Type type) { lock (PropertyAccess) { var index = (UnitySerializer.IsChecksum ? 1 : 0) + (UnitySerializer.IsChecksum && UnitySerializer.IgnoreIds ? 1 : 0); GetSet[][][] collection; if (!PropertyAccess.TryGetValue(type, out collection)) { collection = new GetSet[3][][]; PropertyAccess[type] = collection; } var accessors = collection[index]; if (accessors == null) { object vanilla = GetVanilla(type); bool canGetVanilla = false; if (vanilla != null) { canGetVanilla = !vanilla.Equals(null); } var acs = new List <GetSet>(); var props = UnitySerializer.GetPropertyInfo(type) .Where(p => p.Name != "hideFlags") .Select(p => new { priority = ((SerializationPriorityAttribute)p.GetCustomAttributes(false).FirstOrDefault(a => a is SerializationPriorityAttribute)) ?? new SerializationPriorityAttribute(100), info = p }) .OrderBy(p => p.priority.Priority).Select(p => p.info); foreach (var p in props) { var getSet = new GetSetGeneric(p); if (!canGetVanilla) { getSet.Vanilla = null; } else { getSet.Vanilla = getSet.Get(vanilla); } acs.Add(getSet); } accessors = new GetSet[4][]; accessors[0] = acs.Where(a => !a.IsStatic).ToArray(); accessors[2] = acs.ToArray(); acs.Clear(); var fields = UnitySerializer.GetFieldInfo(type) .Where(f => f.Name != "hideFlags") .Select(p => new { priority = ((SerializationPriorityAttribute)p.GetCustomAttributes(false).FirstOrDefault(a => a is SerializationPriorityAttribute)) ?? new SerializationPriorityAttribute(100), info = p }) .OrderBy(p => p.priority.Priority).Select(p => p.info); foreach (var f in fields) { var getSet = new GetSetGeneric(f); if (!canGetVanilla) { getSet.Vanilla = null; } else { getSet.Vanilla = getSet.Get(vanilla); } acs.Add(getSet); } accessors[1] = acs.Where(a => !a.IsStatic).ToArray(); accessors[3] = acs.ToArray(); collection[index] = accessors; } return(accessors); } }