public override object ToList(Type rListType, Type rElemType) { var rCLRType = ITypeRedirect.GetRedirectType(rListType); if (rCLRType.IsArray) { Array rObject = Array.CreateInstance(rCLRType.GetElementType(), this.Count); for (int i = 0; i < this.Count; i++) { object rValue = this.list[i].ToObject(rElemType); rObject.SetValue(rValue, i); } return(rObject); } else if (rCLRType.IsGenericType && typeof(IList).IsAssignableFrom(rCLRType.GetGenericTypeDefinition())) //是否为泛型 { IList rObject = (IList)Activator.CreateInstance(rListType); Type[] rArgsTypes = rCLRType.GetGenericArguments(); for (int i = 0; i < this.Count; i++) { object rValue = this.list[i].ToObject(rElemType); rObject.Add(rValue); } return(rObject); } return(null); }
public override object ToDict(Type rDictType, Type rKeyType, Type rValueType) { rDictType = ITypeRedirect.GetRedirectType(rDictType); if (rDictType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rDictType.GetGenericTypeDefinition())) { // 特殊处理IDictionary<,>类型 IDictionary rObject = (IDictionary)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default); foreach (var rItem in this.dict) { object rKey = GetKey_ByString(rKeyType, rItem.Key); object rValue = rItem.Value.ToObject(rValueType); rObject.Add(rKey, rValue); } return(rObject); } else if (rDictType.IsGenericType && typeof(IDict).IsAssignableFrom(rDictType.GetGenericTypeDefinition())) { // 特殊处理IDict<,>的类型 IDict rObject = (IDict)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default); foreach (var rItem in this.dict) { object rKey = GetKey_ByString(rKeyType, rItem.Key); object rValue = rItem.Value.ToObject(rValueType); rObject.AddObject(rKey, rValue); } return(rObject); } return(null); }
public static IEnumerable <BindableMember <PropertyInfo> > GetListViewModelProperties(GameObject rGo) { var rBindableMembers = rGo.GetComponentsInParent <ViewModelDataSource>(true) .Where(ds => ds != null && !string.IsNullOrEmpty(ds.ViewModelPath) && !string.IsNullOrEmpty(ds.Key)) .SelectMany(ds => { var rType = TypeResolveManager.Instance.GetType(ds.ViewModelPath); return(rType? .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(prop => new BindableMember <PropertyInfo>(ds, prop, rType))); }) .Where(prop => { var rPropType = ITypeRedirect.GetRedirectType(prop.Member.PropertyType); return (prop != null && rPropType.IsGenericType && typeof(IList).IsAssignableFrom(rPropType.GetGenericTypeDefinition()) && prop.Member.GetSetMethod(false) != null && prop.Member.GetGetMethod(false) != null && !ViewComponentBlackList.Contains(prop.ViewModelType) && prop.Member.GetCustomAttributes(typeof(DataBindingAttribute), true).Any()); } ); return(rBindableMembers); }
public override object ToObject(Type rType) { rType = ITypeRedirect.GetRedirectType(rType); if (rType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rType.GetGenericTypeDefinition())) { // 特殊处理IDictionary<,>类型 IDictionary rObject = (IDictionary)ReflectionAssist.CreateInstance(rType, ReflectionAssist.flags_all); Type[] rArgsTypes = rType.GetGenericArguments(); foreach (var rItem in this.dict) { object rKey = GetKey_ByString(rArgsTypes[0], rItem.Key); object rValue = rItem.Value.ToObject(rArgsTypes[1]); rObject.Add(rKey, rValue); } return(rObject); } else if (rType.IsGenericType && typeof(IDict).IsAssignableFrom(rType.GetGenericTypeDefinition())) { // 特殊处理IDict<,>的类型 IDict rObject = (IDict)ReflectionAssist.CreateInstance(rType, ReflectionAssist.flags_all); Type[] rArgsTypes = rType.GetGenericArguments(); foreach (var rItem in this.dict) { object rKey = GetKey_ByString(rArgsTypes[0], rItem.Key); object rValue = rItem.Value.ToObject(rArgsTypes[1]); rObject.AddObject(rKey, rValue); } return(rObject); } else if (rType.IsClass) { BindingFlags rBindFlags = ReflectionAssist.flags_all; object rObject = ReflectionAssist.CreateInstance(rType, rBindFlags); foreach (var rItem in this.dict) { Type rMemberType = null; FieldInfo rFieldInfo = rType.GetField(rItem.Key, rBindFlags); if (rFieldInfo != null) { rMemberType = rFieldInfo.FieldType; object rValueObj = rItem.Value.ToObject(rMemberType); rFieldInfo.SetValue(rObject, rValueObj); continue; } PropertyInfo rPropInfo = rType.GetProperty(rItem.Key, rBindFlags); if (rPropInfo != null) { rMemberType = rPropInfo.PropertyType; object rValueObj = rItem.Value.ToObject(rMemberType); rPropInfo.SetValue(rObject, rValueObj, null); continue; } } return(rObject); } return(null); }
public override object ToObject(Type rType) { rType = ITypeRedirect.GetRedirectType(rType); if (rType.IsPrimitive) { if (rType == typeof(int)) { return(CastInt(this.value)); } else if (rType == typeof(uint)) { return(CastUInt(this.value)); } else if (rType == typeof(long)) { return(CastLong(this.value)); } else if (rType == typeof(ulong)) { return(CastULong(this.value)); } else if (rType == typeof(float)) { return(CastFloat(this.value)); } else if (rType == typeof(double)) { return(CastDouble(this.value)); } else if (rType == typeof(bool)) { return(CastBool(this.value)); } else if (rType == typeof(byte)) { return(CastByte(this.value)); } else if (rType == typeof(short)) { return(CastShort(this.value)); } else if (rType == typeof(ushort)) { return(CastUShort(this.value)); } } else if (rType.IsEnum) { return(CastEnum(rType, this.value)); } else if (rType == typeof(string)) { return(this.value); } Debug.LogErrorFormat("{0}不是基础类型,不能解析成为JsonData !", this.value); return(this.value.Trim('"')); }
public object CastEnum(Type type, string value) { //如果enum是数字,那么返回数字 int re = 0; if (int.TryParse(value, out re)) { return(re); } //如果不是数字,而是字符串,直接转换为enum type = ITypeRedirect.GetRedirectType(type); return(Enum.Parse(type, value, true)); }
public override object ToObject(Type rType) { rType = ITypeRedirect.GetRedirectType(rType); if (rType.IsPrimitive) { if (rType == typeof(int)) { return(CastInt(this.value)); } else if (rType == typeof(uint)) { return(CastUInt(this.value)); } else if (rType == typeof(long)) { return(CastLong(this.value)); } else if (rType == typeof(ulong)) { return(CastULong(this.value)); } else if (rType == typeof(float)) { return(CastFloat(this.value)); } else if (rType == typeof(double)) { return(CastDouble(this.value)); } else if (rType == typeof(bool)) { return(CastBool(this.value)); } } else if (rType.IsEnum) { return(CastEnum(rType, this.value)); } else if (rType == typeof(string)) { return(this.value); } Console.WriteLine("{0}不是基础类型,不能解析成为JsonData !", this.value); return(this.value.Trim('"')); }