diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-10-25 16:58:42 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-10-25 16:58:42 +0200 |
| commit | d530d39d7ed9b05e3e233adc62dceba2fd17e1fe (patch) | |
| tree | 2e042288fdeedd12e3ca0ee331743ebc115eb4b2 /Software/Visual_Studio/Tango.Core/ExtensionMethods | |
| parent | adaddad79352c156303e9178a6f172a18af50cd2 (diff) | |
| download | Tango-d530d39d7ed9b05e3e233adc62dceba2fd17e1fe.tar.gz Tango-d530d39d7ed9b05e3e233adc62dceba2fd17e1fe.zip | |
Improved extension methods support on procedures.
Drastically reduces procedure designer loading time.
DataStore proto support fully working and tested.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs | 74 |
1 files changed, 67 insertions, 7 deletions
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs index 66978ec19..44297b277 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs @@ -10,6 +10,19 @@ using System.Threading.Tasks; /// </summary> public static class TypeExtensions { + private class AssemblyExtensionMethods + { + public Assembly Assembly { get; set; } + public List<MethodInfo> Methods { get; set; } + + public AssemblyExtensionMethods() + { + Methods = new List<MethodInfo>(); + } + } + + private static List<AssemblyExtensionMethods> _extensionMethodsCache = new List<AssemblyExtensionMethods>(); + /// <summary> /// Gets all the extension methods registered in the specified assembly. /// </summary> @@ -18,14 +31,61 @@ public static class TypeExtensions /// <returns></returns> public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly extensionsAssembly) { - var query = from t in extensionsAssembly.GetTypes() - where !t.IsGenericType && !t.IsNested - from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) - where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false) - where m.GetParameters()[0].ParameterType == type - select m; + if (type.Name == "CalculateRequest") + { + + } + + var asmMethods = _extensionMethodsCache.FirstOrDefault(x => x.Assembly == extensionsAssembly); + + if (asmMethods == null) + { + var query = from t in extensionsAssembly.GetTypes() + where !t.IsGenericType && !t.IsNested + from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) + where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false) + select m; + + asmMethods = new AssemblyExtensionMethods(); + asmMethods.Assembly = extensionsAssembly; + asmMethods.Methods = query.ToList(); + _extensionMethodsCache.Add(asmMethods); + } + + List<MethodInfo> methods = new List<MethodInfo>(); + + asmMethods.Methods.Where(x => x.GetParameters()[0].ParameterType.IsAssignableFrom(type)); + + foreach (var method in asmMethods.Methods) + { + var parameter = method.GetParameters()[0]; + + if (parameter.ParameterType.IsGenericParameter) + { + var constraints = parameter.ParameterType.GetGenericParameterConstraints().ToList(); + + if (constraints.Count > 0) + { + if (constraints[0].IsAssignableFrom(type)) + { + methods.Add(method); + } + else if (constraints[0].GetInterfaces().Any(x => x.IsAssignableFrom(type))) + { + methods.Add(method); + } + } + } + else + { + if (parameter.ParameterType.IsAssignableFrom(type)) + { + methods.Add(method); + } + } + } - return query; + return methods; } /// <summary> |
