Exception CreateException (int code, ObjCMember member, string message, params object[] args) { var method = member as ObjCMethod; if (method != null) return CreateException (code, method.Method, message, args); var property = member as ObjCProperty; if (property != null) return CreateException (code, property.Property, message, args); return CreateException (code, message, args); }
bool AddToMap (ObjCMember member, ref List<Exception> exceptions) { ObjCMember existing; bool rv = true; if (Map.TryGetValue (member.Selector, out existing)) { if (existing.IsImplicit) { AddException (ref exceptions, CreateException (4141, member, "Cannot register the selector '{0}' on the member '{1}.{2}' because Xamarin.iOS implicitly registers this selector.", member.Selector, Registrar.GetTypeFullName (Type), Registrar.GetMemberName (member))); } else { AddException (ref exceptions, CreateException (4119, member, "Could not register the selector '{0}' of the member '{1}.{2}' because the selector is already registered on the member '{3}'.", member.Selector, Registrar.GetTypeFullName (Type), Registrar.GetMemberName (member), Registrar.GetMemberName (existing))); } rv = false; } Map [member.Selector] = member; return rv; }
protected string GetMemberName (ObjCMember member) { var method = member as ObjCMethod; if (method != null) { if (method.Method != null) return GetMethodName (method.Method); return method.MethodName ?? "<implicit>"; } var property = member as ObjCProperty; if (property != null) return GetPropertyName (property.Property); return ((ObjCField)member).Name; }
string ValueTypeSignature (TType type, ObjCMember member, ref bool success) { var signature = new StringBuilder (); signature.Append ("{"); signature.AppendFormat ("{0}=", GetTypeName (type)); foreach (TField field in GetFields (type)) { if (IsStatic (field)) continue; signature.Append (ToSignature (GetFieldType (field), member, ref success)); } signature.Append ("}"); return signature.ToString (); }
string ValueTypeSignature (TType type, ObjCMember member) { bool success = true; return ValueTypeSignature (type, member, ref success); }
protected string ToSignature (TType type, ObjCMember member, ref bool success, bool forProperty = false) { bool isNativeEnum; switch (GetTypeFullName (type)) { case "System.IntPtr": return "^v"; case "System.SByte": return "c"; case "System.Byte": return "C"; case "System.Char": return "c"; case "System.Int16": return "s"; case "System.UInt16": return "S"; case "System.Int32": return "i"; case "System.UInt32": return "I"; case "System.Int64": return "q"; case "System.UInt64": return "Q"; case "System.Single": return "f"; case "System.Double": return "d"; case "System.Boolean": // map managed 'bool' to ObjC BOOL = 'unsigned char' in OSX and 32bit iOS architectures and 'bool' in 64bit iOS architectures #if MONOMAC return "c"; #else return Is64Bits ? "B" : "c"; #endif case "System.Void": return "v"; case "System.String": return forProperty ? "@\"NSString\"" : "@"; case "System.nint": return Is64Bits ? "q" : "i"; case "System.nuint": return Is64Bits ? "Q" : "I"; case "System.nfloat": return Is64Bits ? "d" : "f"; case "System.DateTime": throw CreateException (4102, member, "The registrar found an invalid type `{0}` in signature for method `{2}`. Use `{1}` instead.", "System.DateTime", IsDualBuild ? "Foundation.NSDate" : CompatNamespace + ".Foundation.NSDate", member.FullName); } if (Is (type, ObjCRuntime, "Selector")) return ":"; if (Is (type, ObjCRuntime, "Class")) return "#"; if (IsINativeObject (type)) { if (IsNSObject (type) && forProperty) { return "@\"" + GetExportedTypeName (type) + "\""; } else { return "@"; } } if (IsDelegate (type)) return "^v"; if (IsEnum (type, out isNativeEnum)) { if (isNativeEnum && !Is64Bits) { switch (GetEnumUnderlyingType (type).FullName) { case "System.Int64": return "i"; case "System.UInt64": return "I"; default: throw CreateException (4145, "Invalid enum '{0}': enums with the [Native] attribute must have a underlying enum type of either 'long' or 'ulong'.", GetTypeFullName (type)); } } else { return ToSignature (GetEnumUnderlyingType (type), member, ref success); } } if (IsValueType (type)) return ValueTypeSignature (type, member, ref success); if (IsArray (type)) { ToSignature (GetElementType (type), member, ref success); // this validates that the element type is a type we support. return "@"; // But we don't care about the actual type, we'll just return '@'. We only support NSArrays of the element type, so '@' is always right. } success = false; return string.Empty; }
protected string ToSignature (TType type, ObjCMember member, bool forProperty = false) { bool success = true; var rv = ToSignature (type, member, ref success, forProperty); if (success) return rv; var objcMethod = member as ObjCMethod; if (objcMethod != null) throw ErrorHelper.CreateError (4111, "The registrar cannot build a signature for type `{0}' in method `{1}`.", GetTypeFullName (type), GetTypeFullName (objcMethod.DeclaringType.Type) + "." + objcMethod.MethodName); throw ErrorHelper.CreateError (4101, "The registrar cannot build a signature for type `{0}`.", GetTypeFullName (type)); }
public string ComputeSignature (TType DeclaringType, TMethod Method, ObjCMember member = null, bool isCategoryInstance = false) { var success = true; var signature = new StringBuilder (); bool is_ctor; var method = member as ObjCMethod; if (Method != null) { is_ctor = IsConstructor (Method); } else { is_ctor = method.IsConstructor; } if (is_ctor) { signature.Append ('@'); } else { var ReturnType = Method != null ? GetReturnType (Method) : method.ReturnType; signature.Append (ToSignature (ReturnType, member, ref success)); if (!success) throw CreateException (4104, Method, "The registrar cannot marshal the return value of type `{0}` in the method `{1}.{2}`.", GetTypeFullName (ReturnType), GetTypeFullName (DeclaringType), GetDescriptiveMethodName (Method)); } signature.Append ("@:"); TType[] parameters; if (Method != null) { parameters = GetParameters (Method); } else { parameters = method.Parameters; } if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { if (i == 0 && isCategoryInstance) continue; var type = parameters [i]; if (IsByRef (type)) { signature.Append ("^"); signature.Append (ToSignature (GetElementType (type), member, ref success)); } else { signature.Append (ToSignature (type, member, ref success)); } if (!success) throw CreateException (4136, Method, "The registrar cannot marshal the parameter type '{0}' of the parameter '{1}' in the method '{2}.{3}'", GetTypeFullName (type), GetParameterName (Method, i), GetTypeFullName (DeclaringType), GetDescriptiveMethodName (Method)); } } return signature.ToString (); }