When I used Linked library IOS to use third party library it throws System.InvalidCastException: Specified cast is not valid

Fernando Tear 1 Reputation point
2022-03-21T13:34:23.847+00:00

I need use a third party library in my Xamarin forms project. In android not problem, with IOS, I created an linked library and use sharpie to generate the ApiDefinitions.cs. This file is generated with warnings and when i try to use it in my project throws an System.InvalidCastException: Specified cast is not valid

The complete exception

{System.InvalidCastException: Specified cast is not valid. at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) at ObjCRuntime.Runtime.ConstructNSObject[T] (System.IntPtr ptr, System.Type type, ObjCRuntime.Runtime+MissingCtorResolution missingCtorResolution) [0x0003f] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:1217 at ObjCRuntime.Runtime.GetNSObject[T] (System.IntPtr ptr) [0x000d2] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:1388 at NativeLibrary.libreriaC2D.CrearEnrolamiento (NativeLibrary.DatosEnrolamiento datosEnrolamiento, NativeLibrary.Usuario usuario) [0x00018] in D:\vs.net\Movired\Movired\Movired.IOS.Legacy.Libraries\obj\Release\iOS\NativeLibrary\libreriaC2D.g.cs:205 at Movired.iOS.Application.Main (System.String[] args) [0x00048] in D:\vs.net\Movired\Movired\Movired.iOS\Main.cs:18 }

my skds

sdk: appletvos15.2 arch: arm64
sdk: iphoneos15.2 arch: arm64 armv7
sdk: ios15.2-macabi arch: x86_64
sdk: macosx12.1 arch: x86_64
sdk: watchos8.3 arch: armv7k

sharpie command i use

$ sharpie bind -sdk iphoneos15.2 Config.h Usuario.h DatosEnrolamiento.h libreriaC2D.h -c

Warning in generation

sifamerica@MacBook-Pro-de-Sif libreriaC2D % sharpie bind -sdk iphoneos15.2 Config.h Usuario.h DatosEnrolamiento.h libreriaC2D.h -c > warnings.txt
Parsing 4 header files...
In file included from /var/folders/jt/rs267zf175jgm7l5_wb8mwdc0000gn/T/tmp104274bf.h:2:
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:14:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *email;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:14:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:15:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *rut;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:15:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:16:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *nombres;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:16:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:17:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *apellidos;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:17:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:18:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *numeroCelular;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/Usuario.h:18:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
In file included from /var/folders/jt/rs267zf175jgm7l5_wb8mwdc0000gn/T/tmp104274bf.h:3:

/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:14:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSNumber *codigoOperador;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:14:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:15:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSNumber *codigoApp;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:15:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:16:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *clave;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:16:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:17:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *uuid;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:17:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:18:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute]
@property NSNumber *idEnrolamiento;
^
/Users/sifamerica/libreria_ios 2.2.2/include/libreriaC2D/DatosEnrolamiento.h:18:1: warning: default property attribute 'assign' not appropriate for object [-Wobjc-property-no-attribute]

Binding...
[write] ApiDefinitions.cs
[write] StructsAndEnums.cs
20 warnings generated.

System.BadImageFormatException: Invalid Image
File name: '/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll'
at (wrapper managed-to-native) System.Reflection.Assembly.LoadFrom(string,bool,System.Threading.StackCrawlMark&)
at System.Reflection.Assembly.LoadFrom (System.String assemblyFile) [0x00002] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Reflection/Assembly.cs:367
at Sharpie.Bind.Massagers.XamarinApiMappingMassager.Initialize () [0x0002c] in /Users/runner/work/1/s/Sharpie.Bind/Massagers/XamarinApiMappingMassager.cs:50

Binding Analysis:
Automated binding is complete, but there are a few APIs which have
been flagged with [Verify] attributes. While the entire binding
should be audited for best API design practices, look more closely
at APIs with the following Verify attribute hints:

ConstantsInterfaceAssociation (106 instances):
There's no foolproof way to determine with which Objective-C
interface an extern variable declaration may be associated.
Instances of these are bound as [Field] properties in a partial
interface into a nearby concrete interface to produce a more
intuitive API, possibly eliminating the 'Constants' interface
altogether.

MethodToProperty (130 instances):
An Objective-C method was bound as a C# property due to
convention such as taking no parameters and returning a value (
non-void return). Often methods like these should be bound as
properties to surface a nicer API, but sometimes false-positives
can occur and the binding should actually be a method.

StronglyTypedNSArray (28 instances):
A native NSArray* was bound as NSObject[]. It might be possible
to more strongly type the array in the binding based on
expectations set through API documentation (e.g. comments in the
header file) or by examining the array contents through testing.
For example, an NSArray* containing only NSNumber* instances can
be bound as NSNumber[] instead of NSObject[].

PlatformInvoke (2564 instances):
In general P/Invoke bindings are not as correct or complete as
Objective-C bindings (at least currently). You may need to fix up
the library name (it defaults to '__Internal') and return/
parameter types manually to conform to C calling conventionsfor
the target platform. You may find you don't even want to expose
the C API in your binding, but if you do, you'll probably also
want to relocate the definition to a more appropriate class and
expose a stronger type-safe wrapper. For P/Invoke guidance, see
http://www.mono-project.com/docs/advanced/pinvoke/.

InferredFromMemberPrefix (27 instances):
The name of this originally anonymous declaration was taken from
a common prefix of its members.

Once you have verified a Verify attribute, you should remove it
from the binding source code. The presence of Verify attributes
intentionally cause build failures.

For more information about the Verify attribute hints above,
consult the Objective Sharpie documentation by running 'sharpie
docs' or visiting the following URL:

http://xmn.io/sharpie-docs

ApiDefinitions.cs Generated

using System;

using ObjCRuntime;
using Foundation;
using UIKit;

namespace NativeLibrary
{
// @interface Config : NSObject
[BaseType(typeof(NSObject))]
interface Config
{
// @property (nonatomic, class) int maxTiempoSenial;
[Static, Export("maxTiempoSenial")]
int MaxTiempoSenial { get; set; }
}

// @interface DatosEnrolamiento : NSObject
[BaseType(typeof(NSObject))]
interface DatosEnrolamiento
{
    // @property NSNumber * _Nonnull codigoOperador;
    [Export("codigoOperador", ArgumentSemantic.Assign)]
    NSNumber CodigoOperador { get; set; }

    // @property NSNumber * _Nonnull codigoApp;
    [Export("codigoApp", ArgumentSemantic.Assign)]
    NSNumber CodigoApp { get; set; }

    // @property NSString * _Nonnull clave;
    [Export("clave")]
    string Clave { get; set; }

    // @property NSString * _Nonnull uuid;
    [Export("uuid")]
    string Uuid { get; set; }

    // @property NSNumber * _Nonnull idEnrolamiento;
    [Export("idEnrolamiento", ArgumentSemantic.Assign)]
    NSNumber IdEnrolamiento { get; set; }

    // -(id _Nonnull)initWithOperador:(NSNumber * _Nonnull)codigoOperador andApp:(NSNumber * _Nonnull)codigoApp andClave:(NSString * _Nonnull)clave andUuid:(NSString * _Nonnull)uuid;
    [Export("initWithOperador:andApp:andClave:andUuid:")]
    IntPtr Constructor(NSNumber codigoOperador, NSNumber codigoApp, string clave, string uuid);

    // -(id _Nonnull)initWithOperador:(NSNumber * _Nonnull)codigoOperador andApp:(NSNumber * _Nonnull)codigoApp andClave:(NSString * _Nonnull)clave andUuid:(NSString * _Nonnull)uuid andIdEnrolamiento:(NSNumber * _Nonnull)idEnrolamiento;
    [Export("initWithOperador:andApp:andClave:andUuid:andIdEnrolamiento:")]
    IntPtr Constructor(NSNumber codigoOperador, NSNumber codigoApp, string clave, string uuid, NSNumber idEnrolamiento);
}

// @interface Usuario : NSObject
[BaseType(typeof(NSObject))]
interface Usuario
{
    // @property NSString * _Nonnull email;
    [Export("email")]
    string Email { get; set; }

    // @property NSString * _Nonnull rut;
    [Export("rut")]
    string Rut { get; set; }

    // @property NSString * _Nonnull nombres;
    [Export("nombres")]
    string Nombres { get; set; }

    // @property NSString * _Nonnull apellidos;
    [Export("apellidos")]
    string Apellidos { get; set; }

    // @property NSString * _Nonnull numeroCelular;
    [Export("numeroCelular")]
    string NumeroCelular { get; set; }

    // -(id _Nonnull)initWithEmail:(NSString * _Nonnull)email andRut:(NSString * _Nonnull)rut andNombres:(NSString * _Nonnull)nombres andApellidos:(NSString * _Nonnull)apellidos andNumeroCelular:(NSString * _Nonnull)numeroCelular;
    [Export("initWithEmail:andRut:andNombres:andApellidos:andNumeroCelular:")]
    IntPtr Constructor(string email, string rut, string nombres, string apellidos, string numeroCelular);
}

// @interface libreriaC2D : NSObject
[BaseType(typeof(NSObject))]
interface libreriaC2D
{
    // +(NSMutableDictionary *)crearEnrolamiento:(NSNumber *)cod_operador :(NSNumber *)cod_app :(NSString *)clave :(NSString *)uuid :(NSString *)email :(NSString *)rut :(NSString *)nombres :(NSString *)apellidos :(NSString *)numeroCelular __attribute__((deprecated("Utilice 'crearEnrolamiento:(DatosEnrolamiento*)datos:(Usuario*)usuario'")));
    [Static, Export("crearEnrolamiento:::::::::")]
    NSMutableDictionary CrearEnrolamiento(NSNumber cod_operador, NSNumber cod_app, string clave, string uuid, string email, string rut, string nombres, string apellidos, string numeroCelular);

    // +(NSMutableDictionary *)actualizarEnrolamiento:(NSNumber *)cod_operador :(NSNumber *)cod_app :(NSString *)clave :(NSString *)uuid :(NSString *)email :(NSString *)rut :(NSString *)nombres :(NSString *)apellidos :(NSString *)numeroCelular :(NSNumber *)idEnrolamiento __attribute__((deprecated("Utilice actualizarEnrolamiento:(DatosEnrolamiento*)datos:(Usuario*)usuario")));
    [Static, Export("actualizarEnrolamiento::::::::::")]
    NSMutableDictionary ActualizarEnrolamiento(NSNumber cod_operador, NSNumber cod_app, string clave, string uuid, string email, string rut, string nombres, string apellidos, string numeroCelular, NSNumber idEnrolamiento);

    // +(NSMutableDictionary *)cambiarClave:(NSNumber *)cod_operador :(NSNumber *)cod_app :(NSString *)uuid :(NSString *)pin_actual :(NSString *)pin_nuevo __attribute__((deprecated("Utilice cambiarClave:(DatosEnrolamiento*)datosEnrolamiento:(NSString*)pin_nuevo")));
    [Static, Export("cambiarClave:::::")]
    NSMutableDictionary CambiarClave(NSNumber cod_operador, NSNumber cod_app, string uuid, string pin_actual, string pin_nuevo);

    // +(NSMutableDictionary *)solicitudC2D:(NSNumber *)cod_operador :(NSNumber *)cod_app :(NSString *)clave :(NSString *)uuid :(NSString *)nuabt __attribute__((deprecated("solicitudC2D:(DatosEnrolamiento*)datosEnrolamiento:(NSString*)nuabt")));
    [Static, Export("solicitudC2D:::::")]
    NSMutableDictionary SolicitudC2D(NSNumber cod_operador, NSNumber cod_app, string clave, string uuid, string nuabt);

    // +(NSMutableDictionary *)generarC2D:(NSNumber *)cod_operador :(NSNumber *)cod_app :(NSString *)clave :(NSString *)uuid :(NSString *)trama :(NSNumber *)precisionMinima __attribute__((deprecated("Utilice generarC2D:(DatosEnrolamiento*)datosEnrolamiento:(NSString*)trama:(NSNumber*)epochServidor")));
    [Static, Export("generarC2D::::::")]
    NSMutableDictionary GenerarC2D(NSNumber cod_operador, NSNumber cod_app, string clave, string uuid, string trama, NSNumber precisionMinima);

    // +(NSMutableDictionary *)crearEnrolamiento:(DatosEnrolamiento *)datosEnrolamiento usuario:(Usuario *)usuario;
    [Static, Export("crearEnrolamiento:usuario:")]
    NSMutableDictionary CrearEnrolamiento(DatosEnrolamiento datosEnrolamiento, Usuario usuario);

    // +(NSMutableDictionary *)actualizarEnrolamiento:(DatosEnrolamiento *)datosEnrolamiento usuario:(Usuario *)usuario;
    [Static, Export("actualizarEnrolamiento:usuario:")]
    NSMutableDictionary ActualizarEnrolamiento(DatosEnrolamiento datosEnrolamiento, Usuario usuario);

    // +(NSMutableDictionary *)cambiarClave:(DatosEnrolamiento *)datosEnrolamiento claveNueva:(NSString *)pin_nuevo;
    [Static, Export("cambiarClave:claveNueva:")]
    NSMutableDictionary CambiarClave(DatosEnrolamiento datosEnrolamiento, string pin_nuevo);

    // +(NSMutableDictionary *)solicitudC2D:(DatosEnrolamiento *)datosEnrolamiento nuabt:(NSString *)nuabt;
    [Static, Export("solicitudC2D:nuabt:")]
    NSMutableDictionary SolicitudC2D(DatosEnrolamiento datosEnrolamiento, string nuabt);

    // +(NSMutableDictionary *)generarC2D:(DatosEnrolamiento *)datosEnrolamiento tramaSistemaCentral:(NSString *)trama fechaHoraUTCServidor:(long)epochServidor;
    [Static, Export("generarC2D:tramaSistemaCentral:fechaHoraUTCServidor:")]
    NSMutableDictionary GenerarC2D(DatosEnrolamiento datosEnrolamiento, string trama, nint epochServidor);
}

}

Method who launch the exceptions (System.InvalidCastException: Specified cast is not valid)

    // +(NSMutableDictionary *)crearEnrolamiento:(DatosEnrolamiento *)datosEnrolamiento usuario:(Usuario *)usuario;
    [Static, Export("crearEnrolamiento:usuario:")]
    NSMutableDictionary CrearEnrolamiento(DatosEnrolamiento datosEnrolamiento, Usuario usuario);

I tryied to give all the information context. If you need more information to help me, please let me know. I don´t know which more i can try & test

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
{count} votes