private static TypeBuilder CreateTypeBuilder(ModuleScope modulescope, string name, Type baseType, IEnumerable <Type> interfaces, TypeAttributes flags, bool forceUnsigned) { bool isAssemblySigned = !forceUnsigned && !StrongNameUtil.IsAnyTypeFromUnsignedAssembly(baseType, interfaces); return(modulescope.DefineType(isAssemblySigned, name, flags)); }
private void AddTypeWithCustomModifiersAsModreqOnReturnType(ModuleScope moduleScope, string typeName, params Type[] returnTypeModreqs) { // This method generates a type that would look as follows in IL: // // .class interface public abstract auto ansi beforefieldinit <typeName> // { // .method public hidebysig newslot abstract virtual instance int32 modreq(<modreq1>) modreq(<modreq2>) Foo() cil managed { } // } typeName += "_AsModreqOnReturnType"; var typeBuilder = moduleScope.DefineType( true, typeName, TypeAttributes.Class | TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit); var methodBuilder = typeBuilder.DefineMethod( "Foo", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Abstract | MethodAttributes.Virtual, returnType: typeof(int), returnTypeRequiredCustomModifiers: returnTypeModreqs, returnTypeOptionalCustomModifiers: null, parameterTypes: null, parameterTypeRequiredCustomModifiers: null, parameterTypeOptionalCustomModifiers: null, callingConvention: CallingConventions.Standard); this.generatedTypes.Add(typeName, typeBuilder.CreateType()); }
private void AddTypeWithCustomModifiersAsModoptOnReturnType( ModuleScope moduleScope, string typeName, params Type[] returnTypeModopts ) { // This method generates a type that would look as follows in IL: // // .class interface public abstract auto ansi beforefieldinit <typeName> // { // .method public hidebysig newslot abstract virtual instance int32 modopt(<modopt1>) modopt(<modopt2>) Foo() cil managed { } // } // // The C++/CLI equivalent would look like this (but note that only some modopts // used in this test fixture have a corresponding C++/CLI modifier): // // public interface class <typeName> { // public virtual <additional_modifiers> signed int Foo(); // }; typeName += "_AsModoptOnReturnType"; var typeBuilder = moduleScope.DefineType( true, typeName, TypeAttributes.Class | TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit ); var methodBuilder = typeBuilder.DefineMethod( "Foo", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Abstract | MethodAttributes.Virtual, returnType: typeof(int), returnTypeRequiredCustomModifiers: null, returnTypeOptionalCustomModifiers: returnTypeModopts, parameterTypes: null, parameterTypeRequiredCustomModifiers: null, parameterTypeOptionalCustomModifiers: null, callingConvention: CallingConventions.Standard ); this.generatedTypes.Add(typeName, typeBuilder.CreateType()); }
public void GenerateDynamicAssemblyHavingModopts() { // One test below tries to proxy a type that has a method whose signature // involves an optional modifier (modopt). These are never produced by the // C# nor VB.NET compilers, but the C++/CLI compiler produces those. However, // if we added a C++/CLI project to this solution, it could only be compiled // on Windows (since the only extant C++/CLI compiler is MSVC). So what we do // instead to get some modopts is to generate a dynamic test assembly. // // Instead of using System.Reflection.AssemblyBuilder directly, we use // Castle's own ModuleScope since that seems the easiest way to get a strong- // named persistent assembly that can be referenced by DynamicProxy. const string assemblyName = "Rhino.Mocks.CPP.Interfaces"; const string assemblyFileName = "Rhino.Mocks.CPP.Interfaces.dll"; var moduleScope = new ModuleScope( savePhysicalAssembly: true, disableSignedModule: false, namingScope: new Generators.NamingScope(), strongAssemblyName: assemblyName, strongModulePath: assemblyFileName, weakAssemblyName: assemblyName, weakModulePath: assemblyFileName); // This is the C++/CLI type we want to generate: // // namespace RhinoMocksCPPInterfaces { // public interface class IHaveMethodWithModOpts { // virtual void StartLiveOnSlot(long int slotNumber); // }; // } // // which corresponds to this IL: // // .class interface public abstract auto ansi beforefieldinit RhinoMocksCPPInterfaces.IHaveMethodWithModOpts // { // .method public hidebysig newslot abstract virtual instance void StartLiveOnSlot(int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong) slotNumber) cil managed // { } // } var typeBuilder = moduleScope.DefineType( true, "RhinoMocksCPPInterfaces.IHaveMethodWithModOpts", TypeAttributes.Class | TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit); var methodBuilder = typeBuilder.DefineMethod( "StartLiveOnSlot", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Abstract | MethodAttributes.Virtual, returnType: typeof(void), returnTypeRequiredCustomModifiers: null, returnTypeOptionalCustomModifiers: null, parameterTypes: new[] { typeof(int) }, parameterTypeRequiredCustomModifiers: null, parameterTypeOptionalCustomModifiers: new[] { new[] { typeof(IsLong) } }, callingConvention: CallingConventions.Standard); var iHaveMethodWithModOptsType = typeBuilder.CreateType(); #if FEATURE_ASSEMBLYBUILDER_SAVE && FEATURE_TEST_PEVERIFY // Let's persist and PE-verify the dynamic assembly before it gets used in tests: var assemblyPath = moduleScope.SaveAssembly(); base.RunPEVerifyOnGeneratedAssembly(assemblyPath); #endif this.iHaveMethodWithModOptsType = iHaveMethodWithModOptsType; }
private static TypeBuilder CreateTypeBuilder(ModuleScope modulescope, string name, Type baseType, IEnumerable<Type> interfaces, TypeAttributes flags, bool forceUnsigned) { var isAssemblySigned = !forceUnsigned && !StrongNameUtil.IsAnyTypeFromUnsignedAssembly(baseType, interfaces); return modulescope.DefineType(isAssemblySigned, name, flags); }