public static uint GetTypeId(Type type) { if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); } else if (type.IsArray) { type = typeof(Array); } if (typeof(IDictionary).IsAssignableFrom(type)) { type = typeof(IDictionary); } else if (typeof(IList).IsAssignableFrom(type) && type != typeof(Array)) { type = typeof(IList); } ClassItem ci; if (TypeIdMap.TryGetValue(type, out ci)) { return(ci.TypeId); } var attributes = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attributes.Length > 0) { string name = ((GaeaSerializableAttribute)attributes[0]).Name; if (string.IsNullOrEmpty(name)) { name = type.Name; } uint typeId = (uint)name.GaeaHashCode(); var ci2 = new ClassItem(typeId, type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; return(typeId); } throw new DisallowedSerializeException(type); }
private static void LoadCustemType() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in assemblies) { Module[] modules = null; try { modules = assembly.GetModules(); } catch { continue; } foreach (var module in modules) { Type[] types = null; try { types = module.GetTypes(); } catch { continue; } foreach (var type in types) { try { var attrs = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attrs.Length > 0) { uint typeId = type.GetTypeId(); var ci = new ClassItem(typeId, type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; } } catch { } } } } string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories); foreach (var file in files) { try { FileInfo fi = new FileInfo(file); var modules = System.Reflection.Assembly.Load(fi.Name.Replace(fi.Extension, string.Empty)).GetModules(); foreach (var module in modules) { try { var types = module.GetTypes(); foreach (var type in types) { var attrs = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attrs.Length > 0) { uint typeId = type.GetTypeId(); var ci = new ClassItem(typeId, type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; } } } catch { } } } catch { } } }
private static void LoadCustemType() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in assemblies) { Module[] modules = null; try { modules = assembly.GetModules(); } catch { continue; } foreach (var module in modules) { Type[] types = null; try { types = module.GetTypes(); } catch { continue; } foreach (var type in types) { try { var attrs = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attrs.Length > 0) { uint typeId = type.GetTypeId(); var ci = new ClassItem(typeId,type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; } } catch { } } } } string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories); foreach (var file in files) { try { FileInfo fi = new FileInfo(file); var modules = System.Reflection.Assembly.Load(fi.Name.Replace(fi.Extension, string.Empty)).GetModules(); foreach (var module in modules) { try { var types = module.GetTypes(); foreach (var type in types) { var attrs = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attrs.Length > 0) { uint typeId = type.GetTypeId(); var ci = new ClassItem(typeId,type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; } } } catch { } } } catch { } } }
public static uint GetTypeId(Type type) { if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); } else if (type.IsArray) { type = typeof(Array); } if (typeof(IDictionary).IsAssignableFrom(type)) { type = typeof(IDictionary); } else if (typeof(IList).IsAssignableFrom(type) && type!=typeof(Array)) { type = typeof(IList); } ClassItem ci; if (TypeIdMap.TryGetValue(type, out ci)) { return ci.TypeId; } var attributes = type.GetCustomAttributes(typeof(GaeaSerializableAttribute), false); if (attributes.Length > 0) { string name = ((GaeaSerializableAttribute)attributes[0]).Name; if (string.IsNullOrEmpty(name)) { name = type.Name; } uint typeId = (uint)name.GaeaHashCode(); var ci2 = new ClassItem(typeId,type); IdTypeMap[typeId] = ci; TypeIdMap[type] = ci; return typeId; } throw new DisallowedSerializeException(type); }