コード例 #1
0
            static bool TryRegisterNativeMembers(JniType nativeClass, Type marshalType, string methods)
            {
                bool lockTaken = false;

                try {
                    Monitor.TryEnter(sharedRegistrations, ref lockTaken);
                    List <JniNativeMethodRegistration> registrations;
                    if (lockTaken)
                    {
                        sharedRegistrations.Clear();
                        registrations = sharedRegistrations;
                    }
                    else
                    {
                        registrations = new List <JniNativeMethodRegistration> ();
                    }
                    JniNativeMethodRegistrationArguments arguments = new JniNativeMethodRegistrationArguments(registrations, methods);
                    foreach (var methodInfo in marshalType.GetRuntimeMethods())
                    {
                        if (methodInfo.GetCustomAttribute(typeof(JniAddNativeMethodRegistrationAttribute)) == null)
                        {
                            continue;
                        }

                        if ((methodInfo.Attributes & MethodAttributes.Static) != MethodAttributes.Static)
                        {
                            throw new InvalidOperationException($"The method {methodInfo} marked with {nameof (JniAddNativeMethodRegistrationAttribute)} must be static");
                        }

                        var register = (Action <JniNativeMethodRegistrationArguments>)methodInfo.CreateDelegate(typeof(Action <JniNativeMethodRegistrationArguments>));
                        register(arguments);
                    }
                    nativeClass.RegisterNativeMethods(registrations.ToArray());
                } finally {
                    if (lockTaken)
                    {
                        Monitor.Exit(sharedRegistrations);
                    }
                }

                return(true);
            }
コード例 #2
0
            static bool TryRegisterNativeMembers(JniType nativeClass, Type marshalType, string methods, MethodInfo registerMethod)
            {
                bool lockTaken = false;
                bool rv        = false;

                try {
                    Monitor.TryEnter(sharedRegistrations, ref lockTaken);
                    List <JniNativeMethodRegistration> registrations;
                    if (lockTaken)
                    {
                        sharedRegistrations.Clear();
                        registrations = sharedRegistrations;
                    }
                    else
                    {
                        registrations = new List <JniNativeMethodRegistration> ();
                    }
                    JniNativeMethodRegistrationArguments arguments = new JniNativeMethodRegistrationArguments(registrations, methods);
                    if (registerMethod != null)
                    {
                        registerMethod.Invoke(null, new object [] { arguments });
                        rv = true;
                    }
                    else
                    {
                        rv = FindAndCallRegisterMethod(marshalType, arguments);
                    }

                    if (registrations.Count > 0)
                    {
                        nativeClass.RegisterNativeMethods(registrations.ToArray());
                    }
                } finally {
                    if (lockTaken)
                    {
                        Monitor.Exit(sharedRegistrations);
                    }
                }

                return(rv);
            }