public FieldInfo GetField(Type type, string name, bool instance) { var fullName = type.FullName + "." + name; if (!instance) { fullName += "$static$"; } if (!fieldMap.ContainsKey(fullName)) { var flag = BindingFlags.Public | BindingFlags.NonPublic; flag |= (instance ? BindingFlags.Instance : BindingFlags.Static); var field = CoreUtil.GetField(type, name, flag, true); if (field != null && !field.IsLiteral && !field.IsInitOnly) { fieldMap.Add(fullName, field); } else { fieldMap.Add(fullName, null); } } return(fieldMap[fullName]); }
public PropertyInfo GetProp(Type type, string name, bool instance) { var fullName = type.FullName + "." + name; if (!instance) { fullName += "$static$"; } if (!propMap.ContainsKey(fullName)) { var flag = BindingFlags.Public | BindingFlags.NonPublic; flag |= (instance ? BindingFlags.Instance : BindingFlags.Static); var prop = CoreUtil.GetProperty(type, name, flag, true); if (prop != null && prop.CanWrite) { propMap.Add(fullName, prop); } else { propMap.Add(fullName, null); } } return(propMap[fullName]); }
public FieldInfo[] GetFields(Type type, bool instance) { var fullName = type.FullName; if (!instance) { fullName += "$static$"; } if (!fieldsMap.ContainsKey(fullName)) { var list = new List <FieldInfo>(); var flag = BindingFlags.Public | BindingFlags.NonPublic; flag |= instance ? BindingFlags.Instance : BindingFlags.Static; CoreUtil.GetFields(list, type, flag, true); var fields = (IEnumerable <FieldInfo>)list; var typeAttrs = type.GetCustomAttributes(typeof(PersistenceAttribute), false); if (typeAttrs.Length > 0) { fields = fields.Where(f => { var attributes = f.GetCustomAttributes(typeof(PersistentFieldAttribute), false); return(attributes.Length > 0); }); } fields = fields.Where(f => !f.IsLiteral && !f.IsInitOnly && CoreUtil.IsSafetyReflectionType(f.FieldType)); fieldsMap.Add(fullName, fields.ToArray()); } return(fieldsMap[fullName]); }
public PropertyInfo[] GetProps(Type type, bool instance) { var fullName = type.FullName; if (!instance) { fullName += "$static$"; } if (!propsMap.ContainsKey(fullName)) { var list = new List <PropertyInfo>(); var flag = BindingFlags.Public; flag |= (instance ? BindingFlags.Instance : BindingFlags.Static); CoreUtil.GetProperties(list, type, flag, true); var props = (IEnumerable <PropertyInfo>)list; var typeAttrs = type.GetCustomAttributes(typeof(PersistenceAttribute), false); if (typeAttrs.Length > 0) { props = props.Where(p => { var attributes = p.GetCustomAttributes(typeof(PersistentFieldAttribute), false); return(attributes.Length > 0); }); } props = props.Where(p => p.CanRead && p.CanWrite && p.GetIndexParameters().Length == 0 && CoreUtil.IsSafetyReflectionType(p.PropertyType)); propsMap.Add(fullName, props.ToArray()); } return(propsMap[fullName]); }
public Type FindType(string fullName) { if (!typeMap.ContainsKey(fullName)) { typeMap.Add(fullName, CoreUtil.FindType(fullName)); } return(typeMap[fullName]); }
public UpdateMemberCommand(object obj, string name, object value) { mType = obj is Type ? obj as Type : obj.GetType(); mObj = obj is Type ? null : obj; mMemberValue = value; mMemberName = name; mMemberPath = CoreUtil.FindMemberPath(mType, name); mMemberRaw = CoreUtil.GetMemberValue(mMemberPath, mObj); }
public object DeserializeArray(Type type, BinaryReader reader) { var length = reader.ReadInt32(); var obj = CoreUtil.CreateInstance(type, new object[] { length }) as Array; for (var i = 0; i < obj.Length; i++) { var index = i; Deserialize(reader, ret => obj.SetValue(ret, index)); } return(obj); }
private void InitCustomPersistence() { mCustomPersistenceMap = new Dictionary <Type, CustomPersistence>(); mCustomGenericPersistenceMap = new Dictionary <Type, CustomPersistence>(); var customTypes = CoreUtil.FindSubTypes(typeof(CustomPersistence)); foreach (var type in customTypes) { var instance = (CustomPersistence)CoreUtil.CreateInstance(type, new object[] { this }); if (instance.persistentType.IsGenericTypeDefinition) { mCustomGenericPersistenceMap.Add(instance.persistentType, instance); } else { mCustomPersistenceMap.Add(instance.persistentType, instance); } } }
public object DeserializeConstruction(Type type, bool instance, BinaryReader reader) { var obj = instance ? CoreUtil.CreateInstance(type) : null; var fieldCount = reader.ReadInt32(); for (var i = 0; i < fieldCount; i++) { var fieldName = reader.ReadString(); var field = mCache.GetField(type, fieldName, instance); Debug.Assert(field != null, type.FullName + "." + fieldName + " is not exist or writable."); Deserialize(reader, ret => { if (field != null) { field.SetValue(obj, ret); } }); } var propertyCount = reader.ReadInt32(); for (var i = 0; i < propertyCount; i++) { var propertyName = reader.ReadString(); var property = mCache.GetProp(type, propertyName, instance); Debug.Assert(property != null, type.FullName + "." + propertyName + " is not exist or writable."); Deserialize(reader, ret => { if (property != null) { property.SetValue(obj, ret, null); } }); } return(obj); }
public override void Undo() { CoreUtil.SetMemberValue(mMemberPath, mObj, mMemberRaw); }
public override void Execute() { CoreUtil.SetMemberValue(mMemberPath, mObj, mMemberValue); }