2727using System . Reflection ;
2828using System . Reflection . Emit ;
2929using System . Runtime . CompilerServices ;
30+ using System . Runtime . InteropServices ;
3031using System . Text ;
32+ using System . Threading ;
33+ using System . Xml . Serialization ;
34+
3135
3236#if NETFRAMEWORK
3337using AssemblyGenT = Microsoft . Scripting . Generation . AssemblyGen ;
@@ -223,19 +227,58 @@ private bool ShouldRewriteDelegate(Type delegateType)
223227 return false ;
224228 }
225229
226- // From Microsoft.Scripting.Generation.AssemblyGen
227- // Adapted to not being a method of AssemblyGen, which causes me to copy a WHOLE BUNCH of stuff.
230+ // // From Microsoft.Scripting.Generation.AssemblyGen
231+ // // Adapted to not being a method of AssemblyGen, which causes me to copy a WHOLE BUNCH of stuff.
232+
233+ // internal Type MakeDelegateType(string name, Type[] parameters, Type returnType)
234+ // {
235+ // TypeBuilder builder = /* _assemblyGen. */DefineType(name, typeof(MulticastDelegate), DelegateAttributes, false);
236+ //#if NET9_0_OR_GREATER
237+ // // cannot call SetImplementationFlags on a PersistedAssemblyBuilder
238+ // builder.DefineConstructor(CtorAttributes, CallingConventions.Standard, _DelegateCtorSignature);
239+ // builder.DefineMethod("Invoke", InvokeAttributes, returnType, parameters);
240+ //#else
241+ // builder.DefineConstructor(CtorAttributes, CallingConventions.Standard, _DelegateCtorSignature).SetImplementationFlags(ImplAttributes);
242+ // builder.DefineMethod("Invoke", InvokeAttributes, returnType, parameters).SetImplementationFlags(ImplAttributes);
243+ //#endif
244+ // return builder.CreateType();
245+ // }
246+
247+ //// From Microsoft.Scripting.Generation.AssemblyGen
248+ ////private int _index;
249+ //internal TypeBuilder DefineType(string name, Type parent, TypeAttributes attr, bool preserveName)
250+ //{
251+ // ContractUtils.RequiresNotNull(name, nameof(name));
252+ // ContractUtils.RequiresNotNull(parent, nameof(parent));
253+
254+ // StringBuilder sb = new StringBuilder(name);
255+ // if (!preserveName)
256+ // {
257+ // int index = RT.nextID(); //Interlocked.Increment(ref _index);
258+ // sb.Append('$');
259+ // sb.Append(index);
260+ // }
261+
262+ // // There is a bug in Reflection.Emit that leads to
263+ // // Unhandled Exception: System.Runtime.InteropServices.COMException (0x80131130): Record not found on lookup.
264+ // // if there is any of the characters []*&+,\ in the type name and a method defined on the type is called.
265+ // sb.Replace('+', '_').Replace('[', '_').Replace(']', '_').Replace('*', '_').Replace('&', '_').Replace(',', '_').Replace('\\', '_');
266+
267+ // name = sb.ToString();
268+ // return /* _myModule */ _assemblyGen.ModuleBuilder.DefineType(name, attr, parent);
269+ //}
270+ //private const MethodAttributes CtorAttributes = MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public;
271+ //private const MethodImplAttributes ImplAttributes = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
272+ //private const MethodAttributes InvokeAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
273+ //private const TypeAttributes DelegateAttributes = TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass;
274+ //private static readonly Type[] _DelegateCtorSignature = new Type[] { typeof(object), typeof(nint) };
275+
276+ // I had been using code (shown above) take from Microsoft.Scripting.Generation.AssemblyGen.
277+ // Elsewhere (MethodExpr) I had been using similar code from Microsoft.Scripting.Generation.Snippets.
278+ // I need to modify the Snippets code to work with PersistedAssemblyBuilder. (There is a call to add MethodImplAttributes to the emthod metadata that fails.)
279+
280+ // The two code bases are almost identical. THe code below combines them as appropriate and conditionalizes for PersistentAssemblyBuilder.
228281
229- internal Type MakeDelegateType ( string name , Type [ ] parameters , Type returnType )
230- {
231- TypeBuilder builder = /* _assemblyGen. */ DefineType ( name , typeof ( MulticastDelegate ) , DelegateAttributes , false ) ;
232- builder . DefineConstructor ( CtorAttributes , CallingConventions . Standard , _DelegateCtorSignature ) . SetImplementationFlags ( ImplAttributes ) ;
233- builder . DefineMethod ( "Invoke" , InvokeAttributes , returnType , parameters ) . SetImplementationFlags ( ImplAttributes ) ;
234- return builder . CreateType ( ) ;
235- }
236-
237- // From Microsoft.Scripting.Generation.AssemblyGen
238- //private int _index;
239282 internal TypeBuilder DefineType ( string name , Type parent , TypeAttributes attr , bool preserveName )
240283 {
241284 ContractUtils . RequiresNotNull ( name , nameof ( name ) ) ;
@@ -244,8 +287,8 @@ internal TypeBuilder DefineType(string name, Type parent, TypeAttributes attr, b
244287 StringBuilder sb = new StringBuilder ( name ) ;
245288 if ( ! preserveName )
246289 {
247- int index = RT . nextID ( ) ; //Interlocked.Increment(ref _index);
248- sb . Append ( '$' ) ;
290+ int index = RT . nextID ( ) ;
291+ sb . Append ( "$" ) ;
249292 sb . Append ( index ) ;
250293 }
251294
@@ -255,15 +298,59 @@ internal TypeBuilder DefineType(string name, Type parent, TypeAttributes attr, b
255298 sb . Replace ( '+' , '_' ) . Replace ( '[' , '_' ) . Replace ( ']' , '_' ) . Replace ( '*' , '_' ) . Replace ( '&' , '_' ) . Replace ( ',' , '_' ) . Replace ( '\\ ' , '_' ) ;
256299
257300 name = sb . ToString ( ) ;
258- return /* _myModule */ _assemblyGen . ModuleBuilder . DefineType ( name , attr , parent ) ;
301+
302+ return _assemblyGen . ModuleBuilder . DefineType ( name , attr , parent ) ;
303+ }
304+
305+
306+ public Type MakeDelegateType ( string name , Type [ ] parameters , Type returnType )
307+ {
308+ TypeBuilder builder = DefineType ( name , typeof ( MulticastDelegate ) , DelegateAttributes , false ) ;
309+ var ctor = builder . DefineConstructor ( CtorAttributes , CallingConventions . Standard , _DelegateCtorSignature ) ;
310+ var method = builder . DefineMethod ( "Invoke" , InvokeAttributes , returnType , parameters ) ;
311+
312+ #if NET9_0_OR_GREATER
313+
314+ if ( _assemblyGen . AssemblyBuilder is PersistedAssemblyBuilder )
315+ {
316+ // We cannot call SetImplementationFlags on a PersistedAssemblyBuilder -- trhows an error.
317+ // Without doing this, we get a different error = .ctor does not have a body.
318+ // That's be cause the MethodImplAttributes.Runtime flag indicates that the runtime should provide the implementation.
319+ // Since we can't specify that the runtime should define the constructor body, we'll have to do it ourselves.
320+ // Basically, all the superclass contstructor and return.
321+
322+ ////var baseCtor = typeof(MulticastDelegate).GetConstructor(_DelegateCtorSignature);
323+ ////var allBaseCtors = typeof(MulticastDelegate).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
324+ //CljILGen genCtor = new CljILGen(ctor.GetILGenerator());
325+ ////gen.EmitLoadArg(0);
326+ ////gen.EmitLoadArg(1);
327+ ////gen.EmitLoadArg(2);
328+ ////gen.Emit(OpCodes.Call, baseCtor);
329+ //genCtor.Emit(OpCodes.Ret);
330+
331+ ctor . SetImplementationFlags ( ImplAttributes ) ;
332+ method . SetImplementationFlags ( ImplAttributes ) ;
333+ }
334+ else
335+ {
336+ ctor . SetImplementationFlags ( ImplAttributes ) ;
337+ method . SetImplementationFlags ( ImplAttributes ) ;
338+ }
339+ #else
340+ ctor . SetImplementationFlags ( ImplAttributes ) ;
341+ method . SetImplementationFlags ( ImplAttributes ) ;
342+ #endif
343+
344+ return builder . CreateTypeInfo ( ) ;
259345 }
346+
260347 private const MethodAttributes CtorAttributes = MethodAttributes . RTSpecialName | MethodAttributes . HideBySig | MethodAttributes . Public ;
261348 private const MethodImplAttributes ImplAttributes = MethodImplAttributes . Runtime | MethodImplAttributes . Managed ;
262349 private const MethodAttributes InvokeAttributes = MethodAttributes . Public | MethodAttributes . HideBySig | MethodAttributes . NewSlot | MethodAttributes . Virtual ;
263350 private const TypeAttributes DelegateAttributes = TypeAttributes . Class | TypeAttributes . Public | TypeAttributes . Sealed | TypeAttributes . AnsiClass | TypeAttributes . AutoClass ;
264- private static readonly Type [ ] _DelegateCtorSignature = new Type [ ] { typeof ( object ) , typeof ( nint ) } ;
351+ private static readonly Type [ ] _DelegateCtorSignature = new Type [ ] { typeof ( object ) , typeof ( IntPtr ) } ;
265352
266- #endregion
353+ #endregion
267354
268355 #region Finalizing
269356
0 commit comments