From fec4ee9f51e7f252a489d8a856ec879c629e8c63 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 20 May 2024 19:44:44 -0700 Subject: [PATCH] Import reflection modifiers (#943) --- Mono.Cecil/Import.cs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Mono.Cecil/Import.cs b/Mono.Cecil/Import.cs index 272e96076..e5670b601 100644 --- a/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Import.cs @@ -159,6 +159,19 @@ enum ImportGenericKind { { typeof (object), ElementType.Object }, }; + TypeReference ImportType (Type type, ImportGenericContext context, Type [] required_modifiers, Type [] optional_modifiers) + { + var import = ImportType (type, context); + + foreach (var modifier in required_modifiers) + import = new RequiredModifierType (ImportType (modifier, context), import); + + foreach (var modifier in optional_modifiers) + import = new OptionalModifierType (ImportType (modifier, context), import); + + return import; + } + TypeReference ImportType (Type type, ImportGenericContext context) { return ImportType (type, context, ImportGenericKind.Open); @@ -349,7 +362,7 @@ FieldReference ImportField (SR.FieldInfo field, ImportGenericContext context) return new FieldReference { Name = field.Name, DeclaringType = declaring_type, - FieldType = ImportType (field.FieldType, context), + FieldType = ImportType (field.FieldType, context, field.GetRequiredCustomModifiers (), field.GetOptionalCustomModifiers ()), }; } finally { context.Pop (); @@ -393,15 +406,17 @@ MethodReference ImportMethod (SR.MethodBase method, ImportGenericContext context try { var method_info = method as SR.MethodInfo; reference.ReturnType = method_info != null - ? ImportType (method_info.ReturnType, context) + ? ImportType (method_info.ReturnType, context, method_info.ReturnParameter.GetRequiredCustomModifiers (), method_info.ReturnParameter.GetOptionalCustomModifiers ()) : ImportType (typeof (void), default (ImportGenericContext)); var parameters = method.GetParameters (); var reference_parameters = reference.Parameters; - for (int i = 0; i < parameters.Length; i++) + for (int i = 0; i < parameters.Length; i++) { + var parameter = parameters [i]; reference_parameters.Add ( - new ParameterDefinition (ImportType (parameters [i].ParameterType, context))); + new ParameterDefinition (ImportType (parameter.ParameterType, context, parameter.GetRequiredCustomModifiers (), parameter.GetOptionalCustomModifiers ()))); + } reference.DeclaringType = declaring_type;