static void Main(string[] args) { var lol = new AssemblyCreator("testAssembly"); EnumCreator enumCreator = lol.CreateEnum("lol"); enumCreator.AddEntry("lol", 1); enumCreator.Compile(); TypeCreator typeCreator = lol.CreateClass("lol2"); GenericParameterCreator genericParameter = typeCreator.SetGenericParameter("bon")[0]; typeCreator.AddConstructor(MethodAttributes.Public, new List <Metadata>() { genericParameter }); typeCreator.AddField("lol", genericParameter, FieldAttributes.Public); MethodCreator methodCreator = typeCreator.AddMethod("hey", MethodAttributes.Public, CallingConventions.HasThis); methodCreator.SetParameters(genericParameter); methodCreator.SetReturnType(genericParameter); methodCreator.ConfigureParameter(1, ParameterAttributes.None, "salut"); typeCreator.AddProperty("salut", genericParameter, PropertyAttributes.HasDefault); Type type1 = typeCreator.Compile(); object test = Activator.CreateInstance(type1.MakeGenericType(typeof(int)), Enum.ToObject(enumCreator, 1)); return; }
private void ProcessClass(TypeDefinition type) { _logInfo("\tProcess immutable class:" + type.FullName); // Remove [ImmutableClass] attribute from the class var customAttribute = type.CustomAttributes.First(x => x.AttributeType.Name == "ImmutableClassAttribute"); type.CustomAttributes.Remove(customAttribute); // Add a default constructor having parameters for each property in the class MethodCreator mc = new MethodCreator(_moduleDefinition, _logInfo); type.Methods.Add(mc.CreateDefaultConstructor(type)); // For each property: // 1. If the property has a set method, set it as private // 2. Create a 'with' method, so that a copy of the class can be created with only the value of that single property changed // Properties marked with the [ImmutableClassIgnore] attribute are ignored foreach (var prop in type.Properties) { if (prop.CustomAttributes.ContainsAttribute("ImmutableClassIgnoreAttribute")) { _logInfo("Ignoring property " + prop.Name); continue; } if (prop.SetMethod != null) { prop.SetMethod.IsPrivate = true; prop.SetMethod.IsPublic = false; } type.Methods.Add(mc.CreateWithMethodForProperty(type, prop)); } // Destroy the empty constructor if it exists mc.DestroyEmptyConstructor(type); // Remove [ImmutableClassIgnore] attribute from the properties foreach (var prop in type.Properties) { var ignoreProp = prop.CustomAttributes.FirstOrDefault(x => x.AttributeType.Name == "ImmutableClassIgnoreAttribute"); if (ignoreProp != null) { { _logInfo("Removing [ImmutableClassIgnore] attribute from property " + prop.Name); prop.CustomAttributes.Remove(ignoreProp); } } } }
public Result CreateDelegate(AstNodeStm astNodeStm, int totalInstructions) { var time0 = DateTime.UtcNow; astNodeStm = AstOptimizerPsp.GlobalOptimize(_processor, Ast.Statements(astNodeStm, Ast.Return())); var time1 = DateTime.UtcNow; #if DEBUG_GENERATE_IL Console.WriteLine("{0}", GeneratorIL.GenerateToString <GeneratorILPsp>(DynamicMethod, AstNodeStm)); #endif #if DEBUG_GENERATE_IL_CSHARP Console.WriteLine("{0}", AstNodeExtensions.ToCSharpString(AstNodeStm).Replace("CpuThreadState.", "")); #endif Action <CpuThreadState> Delegate; var time2 = time1; var disableOptimizations = DynarecConfig.DisableDotNetJitOptimizations; if (totalInstructions >= DynarecConfig.InstructionCountToDisableOptimizations) { disableOptimizations = true; } if (Platform.IsMono) { disableOptimizations = false; } if (DynarecConfig.ForceJitOptimizationsOnEvenLargeFunctions) { disableOptimizations = false; } try { Delegate = MethodCreator.CreateDynamicMethod <Action <CpuThreadState> >( //Delegate = MethodCreator.CreateMethodInClass<Action<CpuThreadState>>( Assembly.GetExecutingAssembly().ManifestModule, $"DynamicMethod_0x{this._pc:X}", disableOptimizations, dynamicMethod => { astNodeStm.GenerateIl(dynamicMethod); time2 = DateTime.UtcNow; } ); } catch (InvalidProgramException) { Console.Error.WriteLine("Invalid Delegate:"); #if LOG_TRACE Console.WriteLine("Invalid Delegate:"); foreach (var Line in SafeILGenerator.GetEmittedInstructions()) { if (Line.Substr(0, 1) == ":") { Console.WriteLine("{0}", Line); } else { Console.WriteLine(" {0}", Line); } } #endif throw; } var time3 = DateTime.UtcNow; return(new Result { Delegate = Delegate, DisableOptimizations = disableOptimizations, TimeOptimize = time1 - time0, TimeGenerateIl = time2 - time1, TimeCreateDelegate = time3 - time2, }); }