/// <summary> /// Gets all of the methods for a type, filtering out methods with duplicate names by choosing the method with the most /// derived declaring type. /// </summary> /// <param name="type">Type to get methods for</param> /// <param name="bindingFlags">Binding flags for methods</param> /// <param name="predicate">Any additional filtering on methods</param> /// <returns>Filtered methods</returns> private static IEnumerable <MethodInfo> GetMethodsWithoutDuplicateNames(IReflect type, BindingFlags bindingFlags, Func <MethodInfo, bool> predicate = null) { IList <MemberInfo> methods = predicate != null ? type.GetMethods(bindingFlags) .Where(predicate) .Cast <MemberInfo>() .ToList() : type.GetMethods(bindingFlags) .Cast <MemberInfo>() .ToList(); return(GetMembersWithoutDuplicateNames(methods) .Cast <MethodInfo>()); }
public Method(IReflect type, string methodName, params Type[] types) { var methods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var m = methods.FirstOrDefault(mi => mi.Name == methodName && mi.GetGenericArguments().Length == types.Length); method = m?.MakeGenericMethod(types) ?? throw new MissingMethodException(); }
private static List <JSMemberInfo> GetJsMapInfo(IReflect sender) { var jsMapInfoList = new List <JSMemberInfo>(); var methods = sender.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static); methods.ForEachOfUnNone(method => { var attributes = method.CustomAttributes.ToList(); if (attributes.CanBeCount()) { var jsfunction = attributes.FirstOrDefault(attr => attr.AttributeType == typeof(JSchemaAttribute)); if (jsfunction != null) { var types = method.GetParameters().ToList().ConvertAll(arg => arg.ParameterType.Name); var jsMapInfo = new JSMemberInfo { Member = method.Name, Types = types, IsStatic = method.IsStatic }; jsMapInfoList.Add(jsMapInfo); } } }); return(jsMapInfoList); }
private static List <AssemblyMethod> GetMethods(IReflect type) { return((from m in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly) let currentMethodSignature = GetMethodSignature(m) select new AssemblyMethod(m.Name, currentMethodSignature)).ToList()); }
/// <summary> /// Initializes a new instance of the <see cref="BaseReflectedSpecificFunctions"/> class. /// </summary> /// <param name="staticReflectedClass"> /// The static reflected class. /// </param> /// <param name="access"> /// The DB Access. /// </param> protected BaseReflectedSpecificFunctions(IReflect staticReflectedClass, IDbAccess access) : base(access) { this.Methods = staticReflectedClass.GetMethods(BindingFlags.Static | BindingFlags.Public) .ToDictionary(k => k, k => k.GetParameters()); this.SupportedOperations = this.Methods.Select(x => x.Key.Name).ToList(); }
private static IEnumerable <string> CompareTypes(IReflect type1, IReflect type2, BindingFlags bindingFlags) { MethodInfo[] typeTMethodInfo = type1.GetMethods(bindingFlags); MethodInfo[] typeXMethodInfo = type2.GetMethods(bindingFlags); return(typeTMethodInfo.Select(x => x.Name) .Except(typeXMethodInfo.Select(x => x.Name))); }
private static Dictionary <Type, MethodInfo> GetEventMethod(IReflect type) { return(type .GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(m => m.Name == "When") .Where(m => m.GetParameters().Length == 1) .ToDictionary(m => m.GetParameters().First().ParameterType, m => m)); }
private static IEnumerable<string> CompareTypes(IReflect type1, IReflect type2, BindingFlags bindingFlags) { MethodInfo[] typeTMethodInfo = type1.GetMethods(bindingFlags); MethodInfo[] typeXMethodInfo = type2.GetMethods(bindingFlags); return typeTMethodInfo.Select(x => x.Name) .Except(typeXMethodInfo.Select(x => x.Name)); }
private Tuple <MethodInfo, MemberInfo?>?FindCommandPair([NotNull] IReflect targetType, out bool finded) { finded = false; var methods = targetType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var main = (from method in methods let attr = method.GetCustomAttribute <CommandTargetAttribute>() where attr != null && attr.ProvideMemberName(method) == Name select new { Method = method, IsSync = attr.Synchronize }).FirstOrDefault(); if (main == null) { Debug.Print($"CommandBinder: No Command-Method Found: {Name}"); return(null); } finded = true; _isSync = main.IsSync; var mainAttr = main.Method.GetCustomAttribute <CommandTargetAttribute>(); MemberInfo?second = null; foreach (var m in targetType.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { var attr = m.GetCustomAttribute <CommandTargetAttribute>(); if (attr == null) { continue; } var name = attr.ProvideMemberName(m); if (mainAttr?.CanExecuteMember != null) { if (mainAttr.CanExecuteMember != name) { continue; } } else { if (Name != name && m == main.Method) { continue; } if ("Can" + Name != name) { continue; } } second = m; break; } return(Tuple.Create(main.Method, second)); }
static bool IsCastDefined(IReflect type, Func <MethodInfo, Type> baseType, Func <MethodInfo, Type> derivedType, bool implicitly, bool lookInBase) { var bindinFlags = BindingFlags.Public | BindingFlags.Static | (lookInBase ? BindingFlags.FlattenHierarchy : BindingFlags.DeclaredOnly); return(type.GetMethods(bindinFlags).Any( m => (m.Name == "op_Implicit" || (!implicitly && m.Name == "op_Explicit")) && baseType(m).IsAssignableFrom(derivedType(m)))); }
private void GenerateMethods(IReflect type, CodeTypeDeclaration contract) { var methods = type .GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(m => IsExcluded(m) == false); foreach (var method in methods) { contract.Members.Add(_memberGenerator.GenerateMethodDeclaration(method)); } }
private void GenerateMethods(IReflect type, CodeTypeDeclaration generatedType) { var methods = type .GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(m => m.IsSpecialName == false) .Where(m => m.Name != "GetType"); foreach (var method in methods) { generatedType.Members.Add(GenerateMethod(method)); } }
private MethodInfo GetMethod(IReflect viewModelType, string actionName) { var methodInfo = viewModelType .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) .SingleOrDefault( method => method.Name == actionName && method.GetParameters().Count() <= 1); return(methodInfo); }
protected IEnumerable <MethodInfo> ImplementationsToTest() { var infos = TypeToTest.GetMethods(BindingFlags.Public | BindingFlags.Static); foreach (var methodInfo in infos) { if (methodInfo.Name.StartsWith(ImplementationMethodsPrefix) && methodInfo.Name.EndsWith(ImplementationMethodsSuffix)) { yield return(methodInfo); } } }
private void ScanTypeForCommands(IReflect type) { foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { var commandAttribute = method.GetCustomAttribute <CommandAttribute>(); if (commandAttribute == null) { continue; } Commands.Add(new OpenModComponentBoundCommandRegistration(m_OpenModComponent, method)); } }
private static IEnumerable <IHash> InstantiateHashes(IReflect type) { var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Static); var infos = methodInfos.Where(methodInfo => methodInfo.GetParameters().Length == 0 || methodInfo.GetParameters().All(parameterInfo => parameterInfo.IsOptional)); return(infos.Select(info => new { info, parameterInfos = info.GetParameters() }) .Select(t => ((IHash)t.info.Invoke(null, t.parameterInfos.Length == 0 ? null : Enumerable.Repeat(Type.Missing, t.parameterInfos.Length).ToArray()))?.Clone())); }
private static IEnumerable <string> GetMethods(IReflect myType, BindingFlags flags) { var methods = myType.GetMethods(flags).Where(info => !info.IsSpecialName).ToList(); foreach (var methodInfo in methods) { var type = ToPrettyString(methodInfo.ReturnType); if (!returnTypeDictionary.ContainsKey(type)) { returnTypeDictionary[type] = new List <string>(); } returnTypeDictionary[type].Add(methodInfo.Name); } return(methods.Select(x => x.Name).Distinct()); }
private static void ScanMethodsForLocalizerInvocations(IReflect callerType, IDictionary <string, List <string> > translations) { foreach (var callerMethod in callerType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.CreateInstance)) { if (GetStringMethods.Contains(callerMethod)) { continue; // skip calling self } var callerBody = callerMethod.GetMethodBody(); if (callerBody == null) { continue; // no body } var instructions = MethodBodyReader.GetInstructions(callerMethod); foreach (Instruction instruction in instructions) { if (instruction.Operand is MethodInfo methodInfo && GetStringMethods.Contains(methodInfo) && instruction.Previous.Operand is string value) { string scope; if (instruction.Previous.Previous != null && instruction.Previous.Previous.Operand is FieldInfo field) { scope = field.DeclaringType != null && field.DeclaringType.IsGenericType ? field.DeclaringType.NormalizeResourceControllerName().Replace("_T", string.Empty) : field.DeclaringType?.Name ?? string.Empty; } else { scope = string.Empty; } if (!translations.TryGetValue(scope, out var list)) { translations.Add(scope, list = new List <string>()); } list.Add(value); } } } }
private void WriteAdditionalChecksDescription(IReflect type, int propertyLevel) { var additionalChecks = type .GetMethods(BindingFlags.Instance | BindingFlags.Public) .Where(mi => mi.ReturnType == typeof(bool) && mi.GetParameters().Length == 0) .SelectMany(c => c.GetCustomAttributes <ApiViewCheckAttribute>()) .Where(ac => ac.CheckDescription != null) .ToList(); if (!additionalChecks.Any()) { return; } WritePaddedLine(_settings.AdditionalChecksHeaderPattern, propertyLevel); foreach (var additionalCheck in additionalChecks) { WriteLine($"* {additionalCheck.CheckDescription}"); } }
private static List <MethodInfo> GetMethodList <T>(IReflect type) where T : Attribute { var methods = type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod); var methodList = new List <MethodInfo>(); foreach (var method in methods) { if (method.GetCustomAttributes(typeof(T), true).Length > 0) { methodList.Add(method); } } return(methodList); }
private static Il2CppSystem.Object FindInstance(IReflect WhereLooking, Type WhatLooking) // Credits to Teo { try { var methodInfo = WhereLooking.GetMethods(BindingFlags.Public | BindingFlags.Static) .FirstOrDefault(m => m.ReturnType == WhatLooking && m.GetParameters().Length == 0); if (methodInfo != null) { return((Il2CppSystem.Object)methodInfo.Invoke(null, null)); } MelonLogger.Error("[FindInstance] MethodInfo for " + WhatLooking.Name + " is null"); } catch (Exception e) { MelonLogger.Error($"[FindInstance] {e}"); } return(null); }
private void ScanTypeForCommands(IReflect type) { try { foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { var commandAttribute = method.GetCustomAttribute <CommandAttribute>(); if (commandAttribute == null) { continue; } m_Commands.Add(new OpenModComponentBoundCommandRegistration(m_OpenModComponent, method)); } } catch (Exception ex) { m_Logger.LogDebug(ex, $"Exception in ScanTypeForCommands for type: {type}"); } }
private static void GetExtensionMethods(IReflect type) { var methods = type .GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(info => info.IsDefined(typeof(ExtensionAttribute), true)) .Where(info => info.GetParameters().Length > 0); foreach (var info in methods) { var t = info.GetParameters()[0].ParameterType; if (t.IsGenericType) { t = t.GetGenericTypeDefinition(); } if (!extensionMethods.ContainsKey(t)) { extensionMethods[t] = new HashSet <Tuple <string, string> >(); } var returnType = info.ReturnParameter == null ? "null" : ToPrettyString(info.ReturnParameter.ParameterType); extensionMethods[t].Add(Tuple.Create(info.Name, returnType)); } }
private static object GetMethodExtended( IReflect type, string name, bool staticMethod, int parameterCount) { var haveMethodName = false; BindingFlags flags = (staticMethod ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod; foreach (var method in type.GetMethods(flags)) { if (method.Name.Equals(name)) { haveMethodName = true; if (method.GetParameters().Length == parameterCount) { return(method); } } } return(haveMethodName ? type.GetMethod(name, flags) : null); }
private async Task <IEnumerable <MethodInfo> > GetMatchingMethodsAsync(IReflect matchingType) { var matchingMethods = new List <MethodInfo>(); foreach (MethodInfo method in matchingType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(arg => !arg.IsSpecialName)) { bool matches = true; foreach (IMethodFilter methodFilter in _methodFilters) { if (!await methodFilter.MatchesAsync(method)) { matches = false; break; } } if (matches) { matchingMethods.Add(method); } } return(matchingMethods); }
private MethodInfo GetMethod(IReflect viewModelType, string actionName) { var methodInfo = viewModelType .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) .SingleOrDefault( method => method.Name == actionName && method.GetParameters().Count() <= 1); return methodInfo; }
System.Reflection.MethodInfo[] IReflect.GetMethods(System.Reflection.BindingFlags bindingAttr) { return(typeIReflectImplementation.GetMethods(bindingAttr)); }
MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr) => publicIReflect.GetMethods(bindingAttr);
private static IEnumerable<MethodInfo> GetRestOperations(IReflect type) { return type .GetMethods(BindingFlags.Instance | BindingFlags.Public) .Where(m => m.HasAttribute<RestOperationAttribute>()); }
private static void GetExtensionMethods(IReflect type) { var methods = type .GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(info => info.IsDefined(typeof(ExtensionAttribute), true)) .Where(info => info.GetParameters().Length > 0); foreach (var info in methods) { var t = info.GetParameters()[0].ParameterType; if (t.IsGenericType) t = t.GetGenericTypeDefinition(); if (!extensionMethods.ContainsKey(t)) extensionMethods[t] = new HashSet<Tuple<string, string>>(); var returnType = info.ReturnParameter == null ? "null" : ToPrettyString(info.ReturnParameter.ParameterType); extensionMethods[t].Add(Tuple.Create(info.Name, returnType)); } }
private static IEnumerable<string> GetMethods(IReflect myType, BindingFlags flags) { var methods = myType.GetMethods(flags).Where(info => !info.IsSpecialName).ToList(); foreach (var methodInfo in methods) { var type = ToPrettyString(methodInfo.ReturnType); if (!returnTypeDictionary.ContainsKey(type)) returnTypeDictionary[type] = new List<string>(); returnTypeDictionary[type].Add(methodInfo.Name); } return methods.Select(x => x.Name).Distinct(); }
private static void ProcessMethods(IReflect type, IDictionary <string, Func <object, MoParams, IMoValue> > functions) { var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (var method in methods) { var functionAttribute = method.GetCustomAttribute <MoFunctionAttribute>(); if (functionAttribute == null) { continue; } foreach (var name in functionAttribute.Name) { if (functions.ContainsKey(name)) { Debug.WriteLine($"Duplicate function \'{name}\' in {type.ToString()}"); continue; } IMoValue ExecuteMolangFunction(object instance, MoParams mo) { var methodParams = method.GetParameters(); IMoValue value = DoubleValue.Zero; object[] parameters = new object[methodParams.Length]; //List<object> parameters = new List<object>(); if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(MoParams)) { parameters[0] = mo; //parameters.Add(mo); } else { for (var index = 0; index < methodParams.Length; index++) { var parameter = methodParams[index]; if (!mo.Contains(index)) { if (!parameter.IsOptional) { throw new MissingMethodException($"Missing parameter: {parameter.Name}"); } break; } var t = parameter.ParameterType; if (t == typeof(MoParams)) { parameters[index] = mo; //.Add(mo); } else if (t == typeof(int)) { parameters[index] = mo.GetInt(index); } else if (t == typeof(double)) { parameters[index] = mo.GetDouble(index); } else if (t == typeof(float)) { parameters[index] = (float)mo.GetDouble(index); } else if (t == typeof(string)) { parameters[index] = mo.GetString(index); } else if (typeof(IMoStruct).IsAssignableFrom(t)) { parameters[index] = mo.GetStruct(index); } else if (typeof(MoLangEnvironment).IsAssignableFrom(t)) { parameters[index] = mo.GetEnv(index); } else { throw new Exception("Unknown parameter type."); } //TODO: Continue. } } var result = method.Invoke(instance, parameters); if (result != null) { if (result is IMoValue moValue) { return(moValue); } return(MoValue.FromObject(result)); } return(value); } functions.Add(name, ExecuteMolangFunction); } } }
static IEnumerable <MethodInfo> GetDeclaredMethods(IReflect type) => type.GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
private static object GetMethodExtended( IReflect type, string name, bool staticMethod, int parameterCount) { var haveMethodName = false; BindingFlags flags = (staticMethod ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod; foreach (var method in type.GetMethods(flags)) { if (method.Name.Equals(name)) { haveMethodName = true; if (method.GetParameters().Length == parameterCount) { return method; } } } return haveMethodName ? type.GetMethod(name, flags) : null; }
MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr) { var ret = typeIReflectImplementation.GetMethods(bindingAttr); return(ret); }
// Get methods with attribute private static IEnumerable<MethodInfo> GetMethodsWithAttribute<T>(this IReflect t, BindingFlags flags) where T : Attribute => t .GetMethods(flags) .Where(p => p.IsDefined(typeof(T)));
private MethodInfoCache(IReflect type) { Methods = type.GetMethods(SimpleReflection.BindingFlags); _methods = Methods.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.ToArray(), StringComparer.Ordinal); }
private static IEnumerable<MethodInfo> AllMethodsFrom(IReflect type) { return type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); }