public void WriteAMF3Data(object data) { if (data == null) { WriteAMF3Null(); return; } Type type = data.GetType(); IAMFWriter amfWriter = null; if (amfWriterTable.ContainsKey(type)) { amfWriter = amfWriterTable[type] as IAMFWriter; } else if (ReflectionUtils.IsSubClass(type, ReflectionUtils.GenericIListType)) { amfWriterTable.Add(type, new AMF3ArrayWriter()); } //Second try with basetype (Enums for example) if (amfWriter == null && type.BaseType != null && amfWriterTable.ContainsKey(type.BaseType)) { amfWriter = amfWriterTable[type.BaseType] as IAMFWriter; } if (amfWriter == null) { if (!amfWriterTable.ContainsKey(type)) { amfWriter = new AMF3ObjectWriter(); amfWriterTable.Add(type, amfWriter); } else { amfWriter = amfWriterTable[type] as IAMFWriter; } } if (amfWriter != null) { amfWriter.WriteData(this, data); } else { throw new NotImplementedException(type.FullName + "not has amf3writer!"); } }
public static bool IsListType(Type type) { if (type.IsArray) { return(true); } else if (typeof(IList).IsAssignableFrom(type)) { return(true); } else if (ReflectionUtils.IsSubClass(type, typeof(IList <>))) { return(true); } else { return(false); } }
public static IList CreateList(Type listType) { IList list; bool isReadOnlyOrFixedSize = false; if (listType.IsArray) { list = new List <object>(); isReadOnlyOrFixedSize = true; } else if (typeof(IList).IsAssignableFrom(listType) && ReflectionUtils.IsInstantiatableType(listType)) { list = (IList)Activator.CreateInstance(listType); } else if (ReflectionUtils.IsSubClass(listType, typeof(IList <>))) { list = CreateGenericList(ReflectionUtils.GetGenericArguments(listType)[0]) as IList; } else { throw new Exception(string.Format("Cannot create and populate list type {0}.", listType)); } // create readonly and fixed sized collections using the temporary list if (isReadOnlyOrFixedSize) { if (listType.IsArray) { list = ((List <object>)list).ToArray(); } else if (ReflectionUtils.IsSubClass(listType, typeof(ReadOnlyCollection <>))) { list = (IList)Activator.CreateInstance(listType, list); } } return(list); }
internal void SetMember(object instance, string memberName, object value) { if (instance is ASObject) { ((ASObject)instance)[memberName] = value; return; } Type type = instance.GetType(); FieldInfo fi = type.GetField(memberName, BindingFlags.Public | BindingFlags.Instance); try { bool has = false; if (fi != null) { if (value != null && ReflectionUtils.IsSubClass(value.GetType(), fi.FieldType)) { fi.SetValue(instance, value); } else { value = Convert.ChangeType(value, fi.FieldType); fi.SetValue(instance, value); } has = true; } else { PropertyInfo mi = type.GetProperty(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty); if (mi != null) { //value = Convert.ChangeType(value, mi.PropertyType);del by wangershi 此行导致自定义enum转换失败 mi.SetValue(instance, value, null); has = true; } } if (has == false && Application.isEditor) { notFoundMessage.AppendLine("amf class:" + instance.GetType().Name + " field:" + memberName + " not found!"); } } catch (Exception ex) { IAmfSetMember setMember = instance as IAmfSetMember; if (setMember != null) { setMember.__AmfSetMember(memberName, value); } else { string valueType = "null"; if (value != null) { valueType = value.GetType().ToString(); } throw new Exception("amf class:" + instance.GetType().Name + " field:" + memberName + " newValue typeof:" + valueType + " msg:" + ex.Message); } } }