/// <summary> /// Permet de retourner la liste des methodes d'evaluation disponibles dans les types fournis. /// </summary> /// <param name="types"></param> /// <returns></returns> private IEnumerable <(Type, ExposeClassAttribute, ExposeMethodAttribute, MethodInfo)> GetActions_Impl(BindingFlags bindings, params Type[] types) { var _result = new List <(Type, ExposeClassAttribute, ExposeMethodAttribute, MethodInfo)>(); var _types = new ExposedTypes() .GetTypes() .Where(c => types.Contains(c.Key)) .ToList(); foreach (var u in _types) { var type = u.Key; foreach (ExposeClassAttribute attribute in u.Value) { if (attribute.Context == Context) { var items = MethodDiscovery.GetMethods(type, bindings, returnType, methodSign); foreach (var method in items) { var attribute2 = method.GetCustomAttribute <ExposeMethodAttribute>(); if (attribute2 != null && (string.IsNullOrEmpty(Context) || attribute2.Context == Context)) { _result.Add((u.Key, attribute, attribute2, method)); } } } } } return(_result); }
/// <summary> /// Permet de retourner la liste des methodes d'evaluation disponibles dans les types fournis. /// </summary> /// <param name="types"></param> /// <returns></returns> private IEnumerable <BusinessAction <T> > GetActions_Impl <T>(BindingFlags bindings, params Type[] types) { var _result = new List <BusinessAction <T> >(); var _types = new ExposedTypes() .GetTypes() .Where(c => types.Contains(c.Key)) .ToList(); foreach (var u in _types) { var type = u.Key; foreach (ExposeClassAttribute attribute in u.Value) { string name = attribute.Name ?? type.Name; var items = MethodDiscovery.GetMethods(type, bindings, returnType, methodSign); foreach (var method in items) { RegisterMethodAttribute attribute2 = TypeDescriptor.GetAttributes(method).OfType <RegisterMethodAttribute>().FirstOrDefault(); if (attribute2 != null && (string.IsNullOrEmpty(Context) || attribute2.Context == Context)) { _result.Add(new BusinessAction <T> { Name = $"{name}.{attribute2.DisplayName}", Method = method, Type = type, RuleName = attribute2.DisplayName, Origin = $"Assembly {type.AssemblyQualifiedName}", Context = attribute2.Context, }); } } } } return(_result); }
/// <summary> /// Smarts convert help to create expression with many consideration on source type and target type. /// if no converter found yo can create one in custom class decorated '[Bb.ComponentModel.Attributes.ExposeClass(Context = ConstantsCore.Cast)]' /// </summary> /// <param name="self">The self.</param> /// <param name="targetType">Type of the target.</param> /// <returns></returns> /// <exception cref="System.InvalidCastException">no adapted method cast found for {argument.Type.Name} -> {targetType.Name}</exception> public static Expression SmartConvert(this Expression self, Type targetType) { var bindings = BindingFlags.Static | BindingFlags.Public; var argument = self; if (argument is ConstantExpression c) { argument = Expression.Constant(Convert.ChangeType(c.Value, targetType)); } else { var types = new List <Type>() { argument.Type }; MethodInfo convertMethod = null; if (targetType.IsAssignableFrom(self.Type)) { argument = Expression.Convert(self, targetType); } else { if (convertMethod == null) // try to get implicit or explicit opérator { convertMethod = MethodDiscovery.GetMethods(argument.Type, bindings, targetType, types).FirstOrDefault(); } if (convertMethod == null) // try in convert static methods { convertMethod = MethodDiscovery.GetMethods(typeof(Convert), bindings, targetType, types).FirstOrDefault(); } if (convertMethod == null) // try in custom class { var _types = TypeDiscovery.Instance.GetTypesWithAttributes <ExposeClassAttribute>(typeof(object), (attr) => attr.Context == ConstantsCore.Cast).ToList(); foreach (var item in _types) { if ((convertMethod = MethodDiscovery.GetMethods(item, bindings, targetType, types).FirstOrDefault()) != null) { break; } } } if (convertMethod != null) { argument = Expression.Call(convertMethod, argument); } else { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } throw new InvalidCastException($"no adapted method cast found for {argument.Type.Name} -> {targetType.Name}. Please considere use a static class tagged [ExposeClass(Context =Constants.Cast)] with a specialized method to convert."); } } } return(argument); }