private static ConstructorInfo GetConstructor(ConstructorKey key) { ConstructorInfo result = null; //遍历整个继承树,根据参数数量和类型找到方法 const BindingFlags oneLevelFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase; Type currentType = key.ObjectType; do { ConstructorInfo[] infos = currentType.GetConstructors(oneLevelFlags); foreach (ConstructorInfo info in infos) { if (ValidateGenericArguments(info, key.GenericTypes) && ValidateParameters(info, key.Parameters)) { result = info; break; } } if (result != null) { break; } currentType = currentType.BaseType; } while (currentType != null); return(result); }
private static ConstructorInfo GetConstructor(Type type, Type[] genericTypes, MethodParameter[] prms, bool isGenericVersion) { genericTypes = genericTypes ?? Type.EmptyTypes; prms = prms ?? MethodParameter.EmptyParameters; ConstructorKey key = new ConstructorKey(type, genericTypes, prms, isGenericVersion); return(_getConstructor(key)); }