|
| 1 | +using System; |
| 2 | +using System.Resources; |
| 3 | +using System.Reflection; |
| 4 | +using System.Reflection.Emit; |
| 5 | +using System.ComponentModel.DataAnnotations; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +#nullable disable |
| 11 | + |
| 12 | +namespace DynamicObjects { |
| 13 | + public class M { |
| 14 | + public const string ObjectRequiredMessage = "some string"; |
| 15 | + public static int Main() { |
| 16 | + var instance = createObject(); |
| 17 | + var attrs = instance.GetType().GetProperty("prop1").GetCustomAttributes(); |
| 18 | + |
| 19 | + Assert.True(attrs.Count() == 2); |
| 20 | + Assert.Equal(attrs.ElementAt(0).ToString(), "System.ComponentModel.DataAnnotations.DisplayAttribute"); |
| 21 | + Assert.Equal(attrs.ElementAt(1).ToString(), "System.ComponentModel.DataAnnotations.RequiredAttribute"); |
| 22 | + Assert.Equal(typeof(RequiredAttribute), attrs.ElementAt(1).GetType()); |
| 23 | + Assert.Equal(ObjectRequiredMessage, ((RequiredAttribute)attrs.ElementAt(1)).FormatErrorMessage("abc")); |
| 24 | + |
| 25 | + Console.WriteLine("Success"); |
| 26 | + return 100; |
| 27 | + } |
| 28 | + |
| 29 | + public static object createObject () { |
| 30 | + var an = new AssemblyName { Name = "TempAssembly" ,Version = new Version(1, 0, 0, 0) }; |
| 31 | + var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); |
| 32 | + var moduleBuilder = assemblyBuilder.DefineDynamicModule("TempWorkflowAssembly.dll"); |
| 33 | + var tb = moduleBuilder.DefineType("namespace.myclass" |
| 34 | + , TypeAttributes.Public | |
| 35 | + TypeAttributes.Class | |
| 36 | + TypeAttributes.AnsiClass | |
| 37 | + TypeAttributes.BeforeFieldInit |
| 38 | + , typeof(object)); |
| 39 | + |
| 40 | + FieldBuilder fb = tb.DefineField("_prop1", |
| 41 | + typeof(string), |
| 42 | + FieldAttributes.Private); |
| 43 | + |
| 44 | + var pb = tb.DefineProperty("prop1", PropertyAttributes.HasDefault, typeof(string), null); |
| 45 | + MethodAttributes getSetAttr = |
| 46 | + MethodAttributes.Public | MethodAttributes.SpecialName | |
| 47 | + MethodAttributes.HideBySig; |
| 48 | + |
| 49 | + // Define the "get" accessor method for prop1. |
| 50 | + MethodBuilder custNameGetPropMthdBldr = |
| 51 | + tb.DefineMethod("get_prop1", |
| 52 | + getSetAttr, |
| 53 | + typeof(string), |
| 54 | + Type.EmptyTypes); |
| 55 | + |
| 56 | + ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator(); |
| 57 | + |
| 58 | + custNameGetIL.Emit(OpCodes.Ldarg_0); |
| 59 | + custNameGetIL.Emit(OpCodes.Ldfld, fb); |
| 60 | + custNameGetIL.Emit(OpCodes.Ret); |
| 61 | + |
| 62 | + // Define the "set" accessor method for prop1. |
| 63 | + MethodBuilder custNameSetPropMthdBldr = |
| 64 | + tb.DefineMethod("set_prop1", |
| 65 | + getSetAttr, |
| 66 | + null, |
| 67 | + new Type[] { typeof(string) }); |
| 68 | + |
| 69 | + ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator(); |
| 70 | + |
| 71 | + custNameSetIL.Emit(OpCodes.Ldarg_0); |
| 72 | + custNameSetIL.Emit(OpCodes.Ldarg_1); |
| 73 | + custNameSetIL.Emit(OpCodes.Stfld, fb); |
| 74 | + custNameSetIL.Emit(OpCodes.Ret); |
| 75 | + |
| 76 | + // Last, we must map the two methods created above to our PropertyBuilder to |
| 77 | + // their corresponding behaviors, "get" and "set" respectively. |
| 78 | + pb.SetGetMethod(custNameGetPropMthdBldr); |
| 79 | + pb.SetSetMethod(custNameSetPropMthdBldr); |
| 80 | + |
| 81 | + |
| 82 | + ///create display attribute |
| 83 | + var dat = typeof(DisplayAttribute); |
| 84 | + CustomAttributeBuilder CAB = new CustomAttributeBuilder(dat.GetConstructor(new Type[0]), |
| 85 | + new object[0], |
| 86 | + new PropertyInfo[1] { dat.GetProperty(nameof(DisplayAttribute.Name))}, |
| 87 | + new object[] { "property 1"}); |
| 88 | + pb.SetCustomAttribute(CAB); |
| 89 | + |
| 90 | + // //create required attribute |
| 91 | + var rat = typeof(RequiredAttribute); |
| 92 | + CustomAttributeBuilder CABR = new CustomAttributeBuilder(rat.GetConstructor(new Type[0]), |
| 93 | + new object[0], |
| 94 | + new PropertyInfo[2] { rat.GetProperty(nameof(RequiredAttribute.ErrorMessageResourceType)),rat.GetProperty(nameof(RequiredAttribute.ErrorMessageResourceName))}, |
| 95 | + new object[] {typeof(ValidationErrors), "ObjectRequired" }); |
| 96 | + pb.SetCustomAttribute(CABR); |
| 97 | + |
| 98 | + var objectType = tb.CreateType(); |
| 99 | + return Activator.CreateInstance(objectType); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public class ValidationErrors { |
| 104 | + public static string ObjectRequired => M.ObjectRequiredMessage; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments