Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import reflection modifiers #943

Merged
merged 1 commit into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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