private void TestClass(Type classType, Type referenceType, bool is64Bits) { counter++; DynamicAssembly assembly = new DynamicAssembly(DynamicAssemblyHelper.GetAssemblyName(this, counter), "MyModule"); MethodTuple[] extensionMethods = Bridge.CollectStaticMethods(classType); Assert.IsTrue(Array.TrueForAll(extensionMethods, m => m.MethodInfo.IsStatic)); CategoryGenerator generator = new CategoryGenerator(assembly, is64Bits); Type proxyType = generator.DefineCategoryProxy(classType, extensionMethods); try { assembly.Save(); Type type = assembly.GetType(proxyType.FullName); DynamicAssemblyHelper.Compare(referenceType, type); } catch (Exception ex) { Assert.Fail("Type retrieval failed with " + ex); } }
/// <summary> /// Defines a category, by using various attributes. /// </summary> /// <param name = "categoryGenerator">The category generator to use.</param> /// <param name = "type">The type that contains the definition attibutes.</param> public static void DefineCategory (CategoryGenerator categoryGenerator, Type type) { if (type == null) { throw new ArgumentNullException ("type", Resources.CannotDefineObjectiveCCategroyFromNullType); } // Extract the category's class String className = ExtractCategoryClassName (type); // Check that the category's class exists Class cls = Class.Get (className); if (cls == null) { throw new ObjectiveCCategoryMappingException (String.Format (CultureInfo.CurrentCulture, Resources.ClassNotFoundForCategory, className, type)); } if (Logger.DebugEnabled) { Logger.Debug ("Bridge", "Defining category " + type + " <-> " + className + "(" + type.Name + ")"); } // Collects the informations needed for class generation MethodTuple[] extensionMethods = CollectStaticMethods (type); // Generates the class proxy with the associated structures Type proxyType = categoryGenerator.DefineCategoryProxy (type, extensionMethods); // Add class methods #if USE_CLOSURES foreach (MethodTuple tuple in extensionMethods) { MethodInfo methodInfo = proxyType.GetMethod(tuple.ProxyMethodInfo.Name); IntPtr handle = methodInfo.MethodHandle.Value; AddMethod(cls.pointer, false, handle, tuple.Selector, ObjectiveCEncoding.GetSignature(methodInfo, 1)); } #else String[] methodNames = Array.ConvertAll (extensionMethods, tuple => tuple.Selector); IntPtr[] methodImplementations = Array.ConvertAll (extensionMethods, tuple => tuple.GetFunction (proxyType)); String[] methodEncoding = Array.ConvertAll (extensionMethods, tuple => ObjectiveCEncoding.GetSignature (tuple.MethodInfo, 1)); AddMethods (cls.pointer, false, methodNames, methodImplementations, methodEncoding); #endif }