Esempio n. 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));
                    }
                }
            }
        }
Esempio n. 2
0
        protected T GetInstanceProperty <T>([CallerMemberName] string propertyName = null)
        {
            propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

            JsiiPropertyAttribute propertyAttribute = GetInstancePropertyAttribute(propertyName);

            return(GetPropertyCore <T>(
                       propertyAttribute,
                       client => client.Get(Reference.ToObjectReference(), propertyAttribute.Name)
                       ));
        }
Esempio n. 3
0
        protected void SetInstanceProperty <T>(T value, [CallerMemberName] string propertyName = null)
        {
            propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

            JsiiPropertyAttribute propertyAttribute = GetInstancePropertyAttribute(propertyName);

            SetPropertyCore(
                value,
                propertyAttribute,
                (client, jsiiValue) => client.Set(Reference.ToObjectReference(), propertyAttribute.Name, jsiiValue)
                );
        }
Esempio n. 4
0
        protected static T GetStaticProperty <T>(System.Type type, [CallerMemberName] string propertyName = null)
        {
            type         = type ?? throw new ArgumentNullException(nameof(type));
            propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

            JsiiClassAttribute    classAttribute    = ReflectionUtils.GetClassAttribute(type);
            JsiiPropertyAttribute propertyAttribute = GetStaticPropertyAttribute(type, propertyName);

            return(GetPropertyCore <T>(
                       propertyAttribute,
                       client => client.StaticGet(classAttribute.FullyQualifiedName, propertyAttribute.Name)
                       ));
        }
Esempio n. 5
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));

            JsiiClassAttribute    classAttribute    = ReflectionUtils.GetClassAttribute(type);
            JsiiPropertyAttribute propertyAttribute = GetStaticPropertyAttribute(type, propertyName);

            SetPropertyCore(
                value,
                propertyAttribute,
                (client, jsiiValue) => client.StaticSet(classAttribute.FullyQualifiedName, propertyAttribute.Name, jsiiValue)
                );
        }
Esempio n. 6
0
        static void SetPropertyCore <T>(T value, JsiiPropertyAttribute propertyAttribute, Action <IClient, object> setAction)
        {
            IServiceProvider          serviceProvider = ServiceContainer.ServiceProvider;
            IFrameworkToJsiiConverter converter       = serviceProvider.GetRequiredService <IFrameworkToJsiiConverter>();
            IReferenceMap             referenceMap    = serviceProvider.GetRequiredService <IReferenceMap>();

            if (!converter.TryConvert(propertyAttribute, referenceMap, value, out object jsiiValue))
            {
                throw new ArgumentException($"Could not set property '{propertyAttribute.Name}' to '{value}'", nameof(value));
            }

            IClient client = serviceProvider.GetRequiredService <IClient>();

            setAction(client, jsiiValue);
        }
Esempio n. 7
0
        static T GetPropertyCore <T>(JsiiPropertyAttribute propertyAttribute, Func <IClient, GetResponse> getFunc)
        {
            IServiceProvider serviceProvider = ServiceContainer.ServiceProvider;
            IClient          client          = serviceProvider.GetRequiredService <IClient>();

            GetResponse response = getFunc(client);

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

            if (!converter.TryConvert(propertyAttribute, typeof(T), referenceMap, response.Value, out object frameworkValue))
            {
                throw new ArgumentException($"Could not convert value '{response.Value}' for property '{propertyAttribute.Name}'", nameof(getFunc));
            }

            return((T)frameworkValue);
        }
Esempio n. 8
0
        static JsiiPropertyAttribute GetPropertyAttributeCore(System.Type type, string propertyName, BindingFlags bindingFlags)
        {
            type         = type ?? throw new ArgumentNullException(nameof(type));
            propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

            PropertyInfo propertyInfo = type.GetProperty(propertyName, bindingFlags);

            if (propertyInfo == null)
            {
                throw new ArgumentException($"Property {propertyName} does not exist", nameof(propertyName));
            }

            JsiiPropertyAttribute attribute = propertyInfo.GetCustomAttribute <JsiiPropertyAttribute>();

            if (attribute == null)
            {
                throw new ArgumentException($"Property {propertyName} is missing JsiiPropertyAttribute", nameof(propertyName));
            }

            return(attribute);
        }