Exemple #1
0
        private static void DoInitClass(string name, string baseName, Type type, string outlets)
        {
            Class superClass = new Class(baseName);

            IntPtr exception = IntPtr.Zero;
            IntPtr klass     = CreateClass(superClass, name, ref exception);

            if (exception != IntPtr.Zero)
            {
                CocoaException.Raise(exception);
            }

            if (outlets != null)
            {
                string[] ivnames = outlets.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                int  size      = Marshal.SizeOf(typeof(IntPtr));
                byte alignment = (byte)Math.Log(size, 2);
                foreach (string ivname in ivnames)
                {
                    byte added = class_addIvar(klass, ivname, new IntPtr(size), alignment, "@");
                    if (added == 0)
                    {
                        throw new ArgumentException(string.Format("Couldn't add outlet {0} to {1}. Is there already an outlet with that name?", ivname, name));
                    }
                }
            }

            BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            foreach (MethodInfo info in type.GetMethods(flags))
            {
                if (!info.IsSpecialName && info.DeclaringType.Name != "NSObject")
                {
                    if (info.Name == "dealloc")
                    {
                        throw new ArgumentException(string.Format("Instead of using {0}::{1}, override OnDealloc.", type, info.Name));
                    }

                    RegisterAttribute attr = Attribute.GetCustomAttribute(info, typeof(RegisterAttribute), false) as RegisterAttribute;
                    if (attr != null)
                    {
                        DoAddMethod(name, info, attr.Name ?? info.Name, klass, superClass);
                    }
                    else if (char.IsLower(info.Name[0]))
                    {
                        if (info.GetParameters().Length == 0)
                        {
                            DoAddMethod(name, info, info.Name, klass, superClass);
                        }

                        else if (info.GetParameters().Length == 1)
                        {
                            DoAddMethod(name, info, info.Name + ":", klass, superClass);
                        }

                        else if (info.GetParameters().Length >= 2 && info.Name.Contains("_"))
                        {
                            DoAddMethod(name, info, info.Name.Replace('_', ':') + ":", klass, superClass);
                        }

                        else
                        {
                            DoAddMethod(name, info, info.Name, klass, superClass);
                        }
                    }
                }
            }

            MethodInfo info2 = typeof(NSObject).GetMethod("Deallocated", BindingFlags.Instance | BindingFlags.NonPublic);

            DoAddMethod(name, info2, "dealloc", klass, superClass);

            RegisterClass(klass, ref exception);
            if (exception != IntPtr.Zero)
            {
                CocoaException.Raise(exception);
            }
        }