Skip to content

Commit

Permalink
Import reflection modifiers (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbevain authored May 21, 2024
1 parent a840bdb commit fec4ee9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Mono.Cecil/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit fec4ee9

Please sign in to comment.