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

[release/8.0] [mono] Initialize module's image ALC for dynamic objects #90912

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,7 @@ image_module_basic_init (MonoReflectionModuleBuilderHandle moduleb, MonoError *e
* determined at assembly save time.
*/
/*image = (MonoDynamicImage*)ab->dynamic_assembly->assembly.image; */
MonoAssemblyLoadContext *alc = mono_alc_get_default ();
MonoStringHandle abname = MONO_HANDLE_NEW_GET (MonoString, ab, name);
char *name = mono_string_handle_to_utf8 (abname, error);
return_val_if_nok (error, FALSE);
Expand All @@ -1300,6 +1301,7 @@ image_module_basic_init (MonoReflectionModuleBuilderHandle moduleb, MonoError *e
}
MonoDynamicAssembly *dynamic_assembly = MONO_HANDLE_GETVAL (ab, dynamic_assembly);
image = mono_dynamic_image_create (dynamic_assembly, name, fqname);
image->image.alc = alc;

MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoReflectionModule, moduleb), image, MonoImage*, &image->image);
MONO_HANDLE_SETVAL (moduleb, dynamic_image, MonoDynamicImage*, image);
Expand Down
107 changes: 107 additions & 0 deletions src/tests/Loader/CustomAttributes/DynamicObjects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Resources;
using System.Reflection;
using System.Reflection.Emit;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using Xunit;

#nullable disable

namespace DynamicObjects {
public class M {
public const string ObjectRequiredMessage = "some string";
public static int Main() {
var instance = createObject();
var attrs = instance.GetType().GetProperty("prop1").GetCustomAttributes();

Assert.True(attrs.Count() == 2);
Assert.Equal(attrs.ElementAt(0).ToString(), "System.ComponentModel.DataAnnotations.DisplayAttribute");
Assert.Equal(attrs.ElementAt(1).ToString(), "System.ComponentModel.DataAnnotations.RequiredAttribute");
Assert.Equal(typeof(RequiredAttribute), attrs.ElementAt(1).GetType());
Assert.Equal(ObjectRequiredMessage, ((RequiredAttribute)attrs.ElementAt(1)).FormatErrorMessage("abc"));

Console.WriteLine("Success");
return 100;
}

public static object createObject () {
var an = new AssemblyName { Name = "TempAssembly" ,Version = new Version(1, 0, 0, 0) };
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("TempWorkflowAssembly.dll");
var tb = moduleBuilder.DefineType("namespace.myclass"
, TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit
, typeof(object));

FieldBuilder fb = tb.DefineField("_prop1",
typeof(string),
FieldAttributes.Private);

var pb = tb.DefineProperty("prop1", PropertyAttributes.HasDefault, typeof(string), null);
MethodAttributes getSetAttr =
MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig;

// Define the "get" accessor method for prop1.
MethodBuilder custNameGetPropMthdBldr =
tb.DefineMethod("get_prop1",
getSetAttr,
typeof(string),
Type.EmptyTypes);

ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();

custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, fb);
custNameGetIL.Emit(OpCodes.Ret);

// Define the "set" accessor method for prop1.
MethodBuilder custNameSetPropMthdBldr =
tb.DefineMethod("set_prop1",
getSetAttr,
null,
new Type[] { typeof(string) });

ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();

custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, fb);
custNameSetIL.Emit(OpCodes.Ret);

// Last, we must map the two methods created above to our PropertyBuilder to
// their corresponding behaviors, "get" and "set" respectively.
pb.SetGetMethod(custNameGetPropMthdBldr);
pb.SetSetMethod(custNameSetPropMthdBldr);


///create display attribute
var dat = typeof(DisplayAttribute);
CustomAttributeBuilder CAB = new CustomAttributeBuilder(dat.GetConstructor(new Type[0]),
new object[0],
new PropertyInfo[1] { dat.GetProperty(nameof(DisplayAttribute.Name))},
new object[] { "property 1"});
pb.SetCustomAttribute(CAB);

// //create required attribute
var rat = typeof(RequiredAttribute);
CustomAttributeBuilder CABR = new CustomAttributeBuilder(rat.GetConstructor(new Type[0]),
new object[0],
new PropertyInfo[2] { rat.GetProperty(nameof(RequiredAttribute.ErrorMessageResourceType)),rat.GetProperty(nameof(RequiredAttribute.ErrorMessageResourceName))},
new object[] {typeof(ValidationErrors), "ObjectRequired" });
pb.SetCustomAttribute(CABR);

var objectType = tb.CreateType();
return Activator.CreateInstance(objectType);
}
}

public class ValidationErrors {
public static string ObjectRequired => M.ObjectRequiredMessage;
}

}
9 changes: 9 additions & 0 deletions src/tests/Loader/CustomAttributes/DynamicObjects.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@

<!-- Requires Debug build -->
<ExcludeList Include="$(XunitTestBinBase)/JIT\Directed\debugging\poisoning\poison\*" />

<ExcludeList Include="$(XunitTestBinBase)/Loader/CustomAttributes/**">
<Issue>Dynamic code generation is not supported on this platform</Issue>
</ExcludeList>
</ItemGroup>

<!-- run.proj finds all the *.cmd/*.sh scripts in a test folder and creates corresponding test methods.
Expand Down Expand Up @@ -3717,6 +3721,9 @@
<ExcludeList Include="$(XunitTestBinBase)/tracing/eventpipe/gcdump/**">
<Issue>https://github.com/dotnet/runtime/issues/90012</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Loader/CustomAttributes/**">
<Issue>Dynamic code generation is not supported on this platform</Issue>
</ExcludeList>
</ItemGroup>

<ItemGroup Condition="'$(TargetArchitecture)' == 'wasm' or '$(TargetsMobile)' == 'true'">
Expand Down
Loading