コード例 #1
0
        protected static void SetStaticProperty <T>(System.Type type, T value, [CallerMemberName] string?propertyName = null)
        {
            type         = type ?? throw new ArgumentNullException(nameof(type));
            propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

            JsiiTypeAttributeBase.Load(type.Assembly);

            var classAttribute    = ReflectionUtils.GetClassAttribute(type) !;
            var propertyAttribute = GetStaticPropertyAttribute(type, propertyName);

            SetPropertyCore(
                value,
                propertyAttribute,
                (client, jsiiValue) => client.StaticSet(classAttribute.FullyQualifiedName, propertyAttribute.Name, jsiiValue)
                );
        }
コード例 #2
0
        protected static T InvokeStaticMethod <T>(System.Type type, System.Type[] parameterTypes, object[] arguments, [CallerMemberName] string methodName = "")
        {
            JsiiTypeAttributeBase.Load(type.Assembly);

            var methodAttribute = GetStaticMethodAttribute(type, methodName, parameterTypes);
            var classAttribute  = ReflectionUtils.GetClassAttribute(type) !;

            return(InvokeMethodCore <T>(
                       methodAttribute,
                       arguments,
                       (client, args) => throw new NotSupportedException("Async static methods are currently not supported"),
                       (client, args) => client.StaticInvoke(
                           classAttribute.FullyQualifiedName,
                           methodAttribute.Name,
                           ConvertArguments(methodAttribute.Parameters, arguments)
                           )
                       ));
        }
コード例 #3
0
        protected DeputyBase(DeputyProps?props = null)
        {
            var type = GetType();

            JsiiTypeAttributeBase.Load(type.Assembly);

            // If this is a native object, it won't have any jsii metadata.
            var attribute          = ReflectionUtils.GetClassAttribute(type);
            var fullyQualifiedName = attribute?.FullyQualifiedName ?? "Object";
            var parameters         = attribute?.Parameters ?? Array.Empty <Parameter>();

            var serviceProvider = ServiceContainer.ServiceProvider;
            var client          = serviceProvider.GetRequiredService <IClient>();
            var response        = client.Create(
                fullyQualifiedName,
                ConvertArguments(parameters, props?.Arguments ?? Array.Empty <object>()),
                GetOverrides(),
                GetInterfaces()
                );

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

            referenceMap.AddNativeReference(Reference, this, true);

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

            IEnumerable <Override> GetMethodOverrides()
            {
                var typeMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                  .Where(method => !method.DeclaringType?.Equals(typeof(DeputyBase)) ?? false)
                                  .Where(method => !method.DeclaringType?.Equals(typeof(object)) ?? false);

                foreach (var method in typeMethods)
                {
                    var inheritedAttribute   = method.GetAttribute <JsiiMethodAttribute>();
                    var uninheritedAttribute = method.GetAttribute <JsiiMethodAttribute>(false);

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

            IEnumerable <Override> GetPropertyOverrides()
            {
                var typeProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                     .Where(prop => !prop.DeclaringType?.Equals(typeof(DeputyBase)) ?? false)
                                     .Where(prop => !prop.DeclaringType?.Equals(typeof(object)) ?? false);

                foreach (var property in typeProperties)
                {
                    var inheritedAttribute   = property.GetAttribute <JsiiPropertyAttribute>();
                    var uninheritedAttribute = property.GetAttribute <JsiiPropertyAttribute>(false);

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

            string[] GetInterfaces()
            {
                return(type.GetInterfaces()
                       .Select(iface => iface.GetCustomAttribute <JsiiInterfaceAttribute>())
                       .Where(jsiiIface => jsiiIface != null)
                       .Select(jsiiIface => jsiiIface !.FullyQualifiedName)
                       .ToArray());
            }
        }