/// <summary> /// 根据类型获取成员信息集合 /// </summary> /// <param name="type">类型</param> /// <returns>成员信息集合</returns> private static memberInfo[] getMembers(Type type) { memberInfo[] members; hashCode <Type> hashType = type; if (!memberCache.TryGetValue(hashType, out members)) { if (type.IsEnum) { memberCache[hashType] = members = type.GetFields(BindingFlags.Public | BindingFlags.Static).getArray(value => new memberInfo(value, memberFilter.PublicStaticField)); } else { memberGroup memberGroup = new memberGroup(type); memberCache[hashType] = members = array.concat(memberGroup.PublicFields.getArray(value => new memberInfo(value, memberFilter.PublicInstanceField)), memberGroup.NonPublicFields.getArray(value => new memberInfo(value, memberFilter.NonPublicInstanceField)), memberGroup.PublicProperties.getArray(value => new memberInfo(value, memberFilter.PublicInstanceProperty)), memberGroup.NonPublicProperties.getArray(value => new memberInfo(value, memberFilter.NonPublicInstanceProperty))); int memberIndex = 0; foreach (memberInfo member in members) { member.MemberIndex = memberIndex++; } } } return(members); }
/// <summary> /// 获取默认构造函数 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="constructors">默认构造函数集合</param> /// <returns>默认构造函数,失败返回null</returns> private static func <valueType> get <valueType>(Dictionary <hashCode <Type>, MethodInfo> constructors) { Type type = typeof(valueType); if (type.IsGenericType) { MethodInfo method; if (constructors.TryGetValue(type.GetGenericTypeDefinition(), out method)) { if (!method.IsGenericMethodDefinition) { log.Default.Throw(type.fullName() + " 无法匹配构造函数 " + method.fullName(), false, true); } return((func <valueType>)Delegate.CreateDelegate(typeof(func <valueType>), method.MakeGenericMethod(type.GetGenericArguments()))); } } else { MethodInfo method; hashCode <Type> hashType = type; if (constructors.TryGetValue(hashType, out method)) { if (method.IsGenericMethodDefinition) { log.Default.Throw(type.fullName() + " 无法匹配构造函数 " + method.fullName(), false, true); } constructors.Remove(hashType); return((func <valueType>)Delegate.CreateDelegate(typeof(func <valueType>), method)); } } return(null); }
/// <summary> /// 获取类型的成员方法集合 /// </summary> /// <param name="type">类型</param> /// <returns>成员方法集合</returns> private static methodInfo[] getMethods(Type type) { methodInfo[] methods; hashCode <Type> hashType = type; if (!methodCache.TryGetValue(hashType, out methods)) { int index = 0; methodCache[hashType] = methods = array.concat( type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).getArray(value => new methodInfo(value, memberFilter.PublicStatic)), type.GetMethods(BindingFlags.Public | BindingFlags.Instance).getArray(value => new methodInfo(value, memberFilter.PublicInstance)), type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy).getArray(value => new methodInfo(value, memberFilter.NonPublicStatic)), type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).getArray(value => new methodInfo(value, memberFilter.NonPublicInstance))) .each(value => value.MemberIndex = index++); } return(methods); }
/// <summary> /// 获取 查询字符串获取器 /// </summary> /// <param name="type">对象类型</param> /// <returns>查询字符串获取器</returns> private static func <object, Encoding, setup.memberFilter, string> getter(Type type) { func <object, Encoding, setup.memberFilter, string> value; hashCode <Type> hashType = type; while (Interlocked.CompareExchange(ref getterLock, 1, 0) != 0) { Thread.Sleep(1); } try { if (!getters.TryGetValue(hashType, out value)) { getters.Add(hashType, value = (func <object, Encoding, setup.memberFilter, string>)Delegate.CreateDelegate(typeof(func <object, Encoding, setup.memberFilter, string>), typeof(query <>).MakeGenericType(type).GetMethod("getObject", BindingFlags.Static | BindingFlags.NonPublic))); } } finally { getterLock = 0; } return(value); }
/// <summary> /// 获取默认构造函数 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="type">默认构造函数所在类型</param> /// <returns>默认构造函数,失败返回null</returns> protected internal static func <valueType> get <valueType>(Type type) { if (type != null) { if (type.IsGenericType) { log.Default.Throw(log.exceptionType.ErrorOperation); } hashCode <Type> hashType = type; Monitor.Enter(cacheLock); try { Dictionary <hashCode <Type>, MethodInfo> constructors; if (!cache.TryGetValue(hashType, out constructors)) { constructors = new Dictionary <hashCode <Type>, MethodInfo>(); foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { Type returnType = method.ReturnType; if (returnType != typeof(void) && method.GetParameters().Length == 0) { if (method.IsGenericMethodDefinition) { if (returnType.IsGenericType) { constructors[returnType.GetGenericTypeDefinition()] = method.GetGenericMethodDefinition(); } } else { constructors[method.ReturnType] = method; } } } cache.Add(hashType, constructors); } return(get <valueType>(constructors)); } finally { Monitor.Exit(cacheLock); } } return(null); }
/// <summary> /// 添加代码 /// </summary> /// <param name="cSharperType">模板类型</param> /// <param name="type">实例类型</param> /// <param name="code">代码,null表示锁定</param> /// <returns>锁定是否成功</returns> public static bool Add(hashCode <Type> cSharperType, hashCode <Type> type, string code) { if (code == null) { HashSet <hashCode <Type> > types; if (!codeTypes.TryGetValue(cSharperType, out types)) { codeTypes.Add(cSharperType, types = new HashSet <hashCode <Type> >()); } if (types.Contains(type)) { return(false); } types.Add(type); } else { Add(code); } return(true); }
/// <summary> /// 获取成员信息集合 /// </summary> /// <param name="type">类型</param> /// <returns>成员信息集合</returns> protected static Dictionary <string, memberIndex> getMemberCache(hashCode <Type> type) { Dictionary <string, memberIndex> values; if (!memberCache.TryGetValue(type, out values)) { try { memberCache[type] = values = memberIndexGroup.Get((Type)type).Find(memberFilter.Instance) .getDictionary(value => value.Member.Name); } catch (Exception error) { string output = string.Join(",", memberIndexGroup.Get((Type)type).Find(memberFilter.Instance) .groupCount(value => value.Member.Name) .getFind(value => value.Value != 1) .GetArray(value => value.Key)); log.Default.ThrowReal(error, ((Type)type).FullName + " : " + output, true); } } return(values); }
/// <summary> /// 获取泛型类型函数信息 /// </summary> /// <param name="remoteType">调用代理类型</param> /// <param name="methodName">调用函数名称</param> /// <returns>泛型类型函数信息</returns> public static MethodInfo getGenericTypeMethod(fastCSharp.setup.remoteType remoteType, string methodName) { Type type = remoteType.Type; if (type.Name == genericTypeServerName && type.DeclaringType.IsGenericType) { tcpCall tcpCall = type.customAttribute <tcpCall>(); if (tcpCall != null && tcpCall.IsGenericTypeServerMethod && tcpCall.IsIgnore) { tcpCall = type.DeclaringType.customAttribute <tcpCall>(false, cSharp.Default.IsInheritAttribute); if (tcpCall != null && tcpCall.IsSetup) { keyValue <Type, staticDictionary <string, MethodInfo> > methods; int version = genericTypeMethodVersion; hashCode <Type> hashType = type; if (!genericTypeMethods.TryGetValue(hashType, out methods) || methods.Key != type) { while (Interlocked.CompareExchange(ref genericTypeMethodLock, 1, 0) != 0) { Thread.Sleep(1); } try { if (version == genericTypeMethodVersion || !genericTypeMethods.TryGetValue(hashType, out methods)) { methods = new keyValue <Type, staticDictionary <string, MethodInfo> >(type, new staticDictionary <string, MethodInfo>(type.GetMethods(BindingFlags.Static | BindingFlags.Public), value => value.Name)); genericTypeMethods.Add(hashType, methods); ++genericTypeMethodVersion; } } finally { genericTypeMethodLock = 0; } } return(methods.Value.Get(methodName, null)); } } } log.Default.Throw(type.fullName() + " 不符合泛型类型服务器端调用", false, false); return(null); }
hashCode = CombineHashCode(hashCode, obj.Name.GetHashCode());