Beispiel #1
0
        protected DeputyBase(DeputyProps props = null)
        {
            props = props ?? new DeputyProps();

            System.Type type = GetType();

            // If this is a native object, it won't have any jsii metadata.
            JsiiClassAttribute attribute          = ReflectionUtils.GetClassAttribute(type);
            string             fullyQualifiedName = attribute?.FullyQualifiedName ?? "Object";

            Parameter[] parameters = attribute?.Parameters ?? new Parameter[] { };

            IServiceProvider serviceProvider = ServiceContainer.ServiceProvider;
            IClient          client          = serviceProvider.GetRequiredService <IClient>();
            CreateResponse   response        = client.Create(
                fullyQualifiedName,
                ConvertArguments(parameters, props.Arguments),
                GetOverrides()
                );

            Reference = new ByRefValue(response["$jsii.byref"] as string);
            IReferenceMap referenceMap = serviceProvider.GetRequiredService <IReferenceMap>();

            referenceMap.AddNativeReference(Reference, this, true);

            Override[] GetOverrides()
            {
                return(GetMethodOverrides().Concat(GetPropertyOverrides()).ToArray());
            }

            IEnumerable <Override> GetMethodOverrides()
            {
                foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    JsiiMethodAttribute inheritedAttribute   = method.GetCustomAttribute <JsiiMethodAttribute>(true);
                    JsiiMethodAttribute uninheritedAttribute = method.GetCustomAttribute <JsiiMethodAttribute>(false);

                    if ((inheritedAttribute != null && uninheritedAttribute == null) || uninheritedAttribute?.IsOverride == true)
                    {
                        yield return(new Override(method: inheritedAttribute.Name));
                    }
                }
            }

            IEnumerable <Override> GetPropertyOverrides()
            {
                foreach (PropertyInfo property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    JsiiPropertyAttribute inheritedAttribute   = property.GetCustomAttribute <JsiiPropertyAttribute>(true);
                    JsiiPropertyAttribute uninheritedAttribute = property.GetCustomAttribute <JsiiPropertyAttribute>(false);

                    if ((inheritedAttribute != null && uninheritedAttribute == null) || uninheritedAttribute?.IsOverride == true)
                    {
                        yield return(new Override(property: inheritedAttribute.Name));
                    }
                }
            }
        }
Beispiel #2
0
        protected static T InvokeStaticMethod <T>(System.Type type, System.Type[] parameterTypes, object[] arguments, [CallerMemberName] string methodName = null)
        {
            JsiiMethodAttribute methodAttribute = GetStaticMethodAttribute(type, methodName, parameterTypes);
            JsiiClassAttribute  classAttribute  = ReflectionUtils.GetClassAttribute(type);

            return(InvokeMethodCore <T>(
                       methodAttribute,
                       arguments,
                       (client, args) => throw new ArgumentException("Async static methods are not supported in JSII", nameof(methodAttribute)),
                       (client, args) => client.StaticInvoke(
                           classAttribute.FullyQualifiedName,
                           methodAttribute.Name,
                           ConvertArguments(methodAttribute.Parameters, arguments)
                           )
                       ));
        }
Beispiel #3
0
        protected T InvokeInstanceMethod <T>(System.Type[] parameterTypes, object[] arguments, [CallerMemberName] string methodName = null)
        {
            JsiiMethodAttribute methodAttribute = GetInstanceMethodAttribute(methodName, parameterTypes);

            return(InvokeMethodCore <T>(
                       methodAttribute,
                       arguments,
                       (client, args) => client.Begin(
                           Reference.ToObjectReference(),
                           methodAttribute.Name,
                           ConvertArguments(methodAttribute.Parameters, arguments)
                           ),
                       (client, args) => client.Invoke(
                           Reference.ToObjectReference(),
                           methodAttribute.Name,
                           ConvertArguments(methodAttribute.Parameters, arguments)
                           )
                       ));
        }
Beispiel #4
0
        static JsiiMethodAttribute GetMethodAttributeCore(System.Type type, string methodName, BindingFlags bindingFlags)
        {
            methodName = methodName ?? throw new ArgumentNullException(nameof(methodName));
            type       = type ?? throw new ArgumentNullException(nameof(type));

            MethodInfo methodInfo = type.GetMethod(methodName, bindingFlags);

            if (methodInfo == null)
            {
                throw new ArgumentException($"Method {methodName} does not exist", nameof(methodName));
            }

            JsiiMethodAttribute methodAttribute = methodInfo.GetCustomAttribute <JsiiMethodAttribute>();

            if (methodAttribute == null)
            {
                throw new ArgumentException($"Method {methodName} is missing JsiiMethodAttribute", nameof(methodName));
            }

            return(methodAttribute);
        }
Beispiel #5
0
        private static T InvokeMethodCore <T>(
            JsiiMethodAttribute methodAttribute,
            object?[] arguments,
            Func <IClient, object?[], BeginResponse> beginFunc,
            Func <IClient, object?[], InvokeResponse> invokeFunc
            )
        {
            var serviceProvider = ServiceContainer.ServiceProvider;

            var client       = serviceProvider.GetRequiredService <IClient>();
            var converter    = serviceProvider.GetRequiredService <IJsiiToFrameworkConverter>();
            var referenceMap = serviceProvider.GetRequiredService <IReferenceMap>();

            var result = GetResult();

            if (!converter.TryConvert(methodAttribute.Returns, typeof(T), referenceMap, result, out object?frameworkValue))
            {
                throw new NotSupportedException($"Could not convert result '{result}' for method '{methodAttribute.Name}'");
            }

            return((T)frameworkValue !);

            object?GetResult()
            {
                var args = ConvertArguments(methodAttribute.Parameters, arguments);

                if (methodAttribute.IsAsync)
                {
                    var beginResponse = beginFunc(client, args);

                    InvokeCallbacks();

                    return(client.End(beginResponse.PromiseId).Result);
                }

                var invokeResponse = invokeFunc(client, args);

                return(invokeResponse.Result);
            }
        }
Beispiel #6
0
        static T InvokeMethodCore <T>(
            JsiiMethodAttribute methodAttribute,
            object[] arguments,
            Func <IClient, object[], BeginResponse> beginFunc,
            Func <IClient, object[], InvokeResponse> invokeFunc
            )
        {
            IServiceProvider          serviceProvider = ServiceContainer.ServiceProvider;
            IClient                   client          = serviceProvider.GetRequiredService <IClient>();
            IJsiiToFrameworkConverter converter       = serviceProvider.GetRequiredService <IJsiiToFrameworkConverter>();
            IReferenceMap             referenceMap    = serviceProvider.GetRequiredService <IReferenceMap>();

            object result = GetResult();

            if (!converter.TryConvert(methodAttribute.Returns, referenceMap, result, out object frameworkValue))
            {
                throw new ArgumentException($"Could not convert result '{result}' for method '{methodAttribute.Name}'", nameof(result));
            }

            return((T)frameworkValue);

            object GetResult()
            {
                object[] args = ConvertArguments(methodAttribute.Parameters, arguments);

                if (methodAttribute.IsAsync)
                {
                    BeginResponse beginResponse = beginFunc(client, args);

                    InvokeCallbacks();

                    return(client.End(beginResponse.PromiseId).Result);
                }

                InvokeResponse invokeResponse = invokeFunc(client, args);

                return(invokeResponse.Result);
            }
        }