diff --git a/Cpp2IL.Core/Model/Contexts/InjectedMethodAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/InjectedMethodAnalysisContext.cs index c4555cb8..a027f1f9 100644 --- a/Cpp2IL.Core/Model/Contexts/InjectedMethodAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/InjectedMethodAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using System.Reflection; namespace Cpp2IL.Core.Model.Contexts; @@ -23,20 +24,28 @@ public InjectedMethodAnalysisContext( string name, TypeAnalysisContext returnType, MethodAttributes attributes, - TypeAnalysisContext[] injectedParameterTypes, - string[]? injectedParameterNames = null, - ParameterAttributes[]? injectedParameterAttributes = null, + ReadOnlySpan injectedParameterTypes, + ReadOnlySpan injectedParameterNames = default, + ReadOnlySpan injectedParameterAttributes = default, MethodImplAttributes defaultImplAttributes = MethodImplAttributes.Managed) : base(null, parent) { DefaultName = name; DefaultReturnType = returnType; DefaultAttributes = attributes; + var hasParameterNames = !injectedParameterNames.IsEmpty; + var hasParameterAttributes = !injectedParameterAttributes.IsEmpty; + + if (hasParameterNames && injectedParameterNames.Length != injectedParameterTypes.Length) + throw new ArgumentException("Length of injected parameter names must match length of injected parameter types.", nameof(injectedParameterNames)); + if (hasParameterAttributes && injectedParameterAttributes.Length != injectedParameterTypes.Length) + throw new ArgumentException("Length of injected parameter attributes must match length of injected parameter types.", nameof(injectedParameterAttributes)); + for (var i = 0; i < injectedParameterTypes.Length; i++) { var injectedParameterType = injectedParameterTypes[i]; - var injectedParameterName = injectedParameterNames?[i]; - var injectedParameterAttribute = injectedParameterAttributes?[i] ?? ParameterAttributes.None; + var injectedParameterName = hasParameterNames ? injectedParameterNames[i] : null; + var injectedParameterAttribute = hasParameterAttributes ? injectedParameterAttributes[i] : ParameterAttributes.None; Parameters.Add(new InjectedParameterAnalysisContext(injectedParameterName, injectedParameterType, injectedParameterAttribute, i, this)); } diff --git a/Cpp2IL.Core/Model/Contexts/InjectedTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/InjectedTypeAnalysisContext.cs index 549a2bc1..0ef368b3 100644 --- a/Cpp2IL.Core/Model/Contexts/InjectedTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/InjectedTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using System.Reflection; namespace Cpp2IL.Core.Model.Contexts; @@ -22,7 +23,7 @@ public InjectedTypeAnalysisContext(AssemblyAnalysisContext containingAssembly, s DefaultAttributes = typeAttributes; } - public InjectedMethodAnalysisContext InjectMethodContext(string methodName, TypeAnalysisContext returnType, MethodAttributes attributes, params TypeAnalysisContext[] args) + public InjectedMethodAnalysisContext InjectMethodContext(string methodName, TypeAnalysisContext returnType, MethodAttributes attributes, params ReadOnlySpan args) { var method = new InjectedMethodAnalysisContext(this, methodName, returnType, attributes, args); Methods.Add(method); diff --git a/Cpp2IL.Core/Model/MultiAssemblyInjectedType.cs b/Cpp2IL.Core/Model/MultiAssemblyInjectedType.cs index 9a431ac0..eca85aac 100644 --- a/Cpp2IL.Core/Model/MultiAssemblyInjectedType.cs +++ b/Cpp2IL.Core/Model/MultiAssemblyInjectedType.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -9,10 +10,17 @@ public class MultiAssemblyInjectedType(InjectedTypeAnalysisContext[] injectedTyp { public InjectedTypeAnalysisContext[] InjectedTypes { get; } = injectedTypes; - public Dictionary InjectMethodToAllAssemblies(string name, TypeAnalysisContext returnType, MethodAttributes attributes, params TypeAnalysisContext[] args) - => InjectedTypes.ToDictionary(t => t.DeclaringAssembly, t => t.InjectMethodContext(name, returnType, attributes, args)); + public Dictionary InjectMethodToAllAssemblies(string name, TypeAnalysisContext returnType, MethodAttributes attributes, params ReadOnlySpan args) + { + var dictionary = new Dictionary(); + foreach (var type in InjectedTypes) + { + dictionary[type.DeclaringAssembly] = type.InjectMethodContext(name, returnType, attributes, args); + } + return dictionary; + } - public Dictionary InjectConstructor(bool isStatic, params TypeAnalysisContext[] args) + public Dictionary InjectConstructor(bool isStatic, params ReadOnlySpan args) { var attributes = isStatic ? MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Static