Ejemplo n.º 1
0
 public static int RegisterSingletonType <T>(string uri, int versionMajor = 1, int versionMinor = 0)
 {
     using (var type = NetTypeManager.GetTypeInfo <T>())
     {
         return(Interop.QQmlApplicationEngine.RegisterSingletonTypeNet(type.Handle, uri, versionMajor, versionMinor, typeof(T).Name));
     }
 }
Ejemplo n.º 2
0
        public void Can_get_method_parameters()
        {
            var typeInfo = NetTypeManager.GetTypeInfo <TestType2>();

            typeInfo.EnsureLoaded();

            typeInfo.MethodCount.Should().Be(1);

            var method = typeInfo.GetMethod(0);

            method.Should().NotBeNull();
            method.ParameterCount.Should().Be(2);

            var parameter1 = method.GetParameter(0);

            parameter1.Should().NotBeNull();
            parameter1.Name.Should().Be("parameter1");
            var parameter1Type = parameter1.Type;

            parameter1Type.Should().NotBeNull();
            parameter1Type.ClassName.Should().Be("Int32");

            var parameter2 = method.GetParameter(1);

            parameter2.Should().NotBeNull();
            parameter2.Name.Should().Be("parameter2");
            var parameter2Type = parameter2.Type;

            parameter2Type.Should().NotBeNull();
            parameter2Type.ClassName.Should().Be("TestType1");
        }
Ejemplo n.º 3
0
Archivo: Qml.cs Proyecto: xlmnxp/qmlnet
 public static int RegisterType(Type type, string qmlName, string uri, int versionMajor = 1, int versionMinor = 0)
 {
     using (var typeInfo = NetTypeManager.GetTypeInfo(type))
     {
         return(QQmlApplicationEngine.RegisterType(typeInfo, uri, qmlName, versionMajor, versionMinor));
     }
 }
Ejemplo n.º 4
0
Archivo: Qml.cs Proyecto: xlmnxp/qmlnet
 public static int RegisterSingletonType(Type type, string qmlName, string uri, int versionMajor = 1, int versionMinor = 0)
 {
     using (var typeInfo = NetTypeManager.GetTypeInfo(type))
     {
         return(Interop.QQmlApplicationEngine.RegisterSingletonTypeNet(typeInfo.Handle, uri, versionMajor, versionMinor, qmlName));
     }
 }
Ejemplo n.º 5
0
        public void Can_get_property()
        {
            try
            {
                var value = new TestObject();
                value.TestProperty = "value1";

                var typeInfo = NetTypeManager.GetTypeInfo <TestObject>();
                typeInfo.EnsureLoaded();

                var readProperty =
                    global::Qml.Net.Internal.CodeGen.CodeGen.BuildReadPropertyDelegate(typeInfo.GetProperty(0));

                NetVariant result     = new NetVariant();
                Task       taskResult = null;
                readProperty(NetReference.CreateForObject(value), new NetVariantList(), result, ref taskResult);

                result.String.Should().Be("value1");

                throw new Exception("This shouldn't be ran");
            }
            catch (Exception ex)
            {
                ex.Message.Should().StartWith("Can't operate on struct types yet");
            }
        }
Ejemplo n.º 6
0
        public void Can_get_unique_id_for_types()
        {
            var type1 = NetTypeManager.GetTypeInfo <TestType16>();
            var type2 = NetTypeManager.GetTypeInfo <TestType17>();

            type1.Id.Should().NotBe(type2.Id);
        }
Ejemplo n.º 7
0
        public void Can_get_method_name()
        {
            var type = NetTypeManager.GetTypeInfo <TestType5>();

            type.EnsureLoaded();
            var method = type.GetMethod(0);

            method.MethodName.Should().Be("ThisIsMethodName");
        }
Ejemplo n.º 8
0
        public void Can_get_method_without_return_type()
        {
            var type = NetTypeManager.GetTypeInfo <TestType4>();

            type.EnsureLoaded();
            var returnType = type.GetMethod(0).ReturnType;

            returnType.Should().BeNull();
        }
Ejemplo n.º 9
0
        public void Can_get_method_with_return_type()
        {
            var type = NetTypeManager.GetTypeInfo <TestType3>();

            type.EnsureLoaded();
            var returnType = type.GetMethod(0).ReturnType;

            returnType.Should().NotBeNull();
            returnType.ClassName.Should().Be("TestType3");
        }
Ejemplo n.º 10
0
        public void Can_detect_static_methods()
        {
            var type = NetTypeManager.GetTypeInfo <TestType12>();

            type.EnsureLoaded();
            type.MethodCount.Should().Be(2);
            type.LocalMethodCount.Should().Be(1);
            type.GetLocalMethod(0).MethodName.Should().Be("LocalMethod");
            type.GetStaticMethod(0).MethodName.Should().Be("StaticMethod");
        }
Ejemplo n.º 11
0
        public void Can_instantiate_type()
        {
            var type = NetTypeManager.GetTypeInfo <TestObject>();

            using (var instance = new NetReference(Interop.Callbacks.InstantiateType(type.Handle), false))
            {
                instance.Instance.Should().NotBeNull();
                instance.Instance.Should().BeOfType <TestObject>();
            }
        }
Ejemplo n.º 12
0
        public void Can_get_net_type()
        {
            var typeInfo = NetTypeManager.GetTypeInfo <TestType1>();

            typeInfo.EnsureLoaded();

            typeInfo.FullTypeName.Should().Be(typeof(TestType1).AssemblyQualifiedName);
            typeInfo.ClassName.Should().Be("TestType1");
            typeInfo.MethodCount.Should().Be(1);
            typeInfo.PropertyCount.Should().Be(1);
        }
Ejemplo n.º 13
0
        public void Can_auto_add_signal_for_property_notification()
        {
            var type = NetTypeManager.GetTypeInfo <TestType9>();

            type.EnsureLoaded();
            type.SignalCount.Should().Be(1);
            type.PropertyCount.Should().Be(1);
            type.GetProperty(0).Name.Should().Be("Property");
            type.GetProperty(0).NotifySignal.Should().NotBeNull();
            type.GetProperty(0).NotifySignal.Name.Should().Be("signalName");
        }
Ejemplo n.º 14
0
        public void Can_get_parent_type_on_method()
        {
            var typeInfo = NetTypeManager.GetTypeInfo <TestType1>();

            typeInfo.EnsureLoaded();

            var method = typeInfo.GetMethod(0);

            method.MethodName.Should().Be("TestMethod");
            method.ParentType.FullTypeName.Should().Be(typeof(TestType1).AssemblyQualifiedName);
        }
Ejemplo n.º 15
0
        public void Can_get_parent_type_on_property()
        {
            var typeInfo = NetTypeManager.GetTypeInfo <TestType1>();

            typeInfo.EnsureLoaded();

            var property = typeInfo.GetProperty(0);

            property.Name.Should().Be("TestProperty");
            property.ParentType.FullTypeName.Should().Be(typeof(TestType1).AssemblyQualifiedName);
        }
Ejemplo n.º 16
0
        public void Can_auto_create_signal_with_property_name_if_no_name_given()
        {
            var type = NetTypeManager.GetTypeInfo <TestType10>();

            type.EnsureLoaded();
            type.SignalCount.Should().Be(1);
            type.PropertyCount.Should().Be(1);
            type.GetProperty(0).Name.Should().Be("Property");
            type.GetProperty(0).NotifySignal.Should().NotBeNull();
            type.GetProperty(0).NotifySignal.Name.Should().Be("propertyChanged");
        }
Ejemplo n.º 17
0
        public void Can_detect_base_class()
        {
            var type1 = NetTypeManager.GetTypeInfo <TestType15>();

            type1.BaseType.Should().StartWith("System.Object, ");
            var type2 = NetTypeManager.GetTypeInfo <TestType16>();

            type2.BaseType.Should().StartWith("Qml.Net.Tests.Types.NetTypeManagerTests+TestType15, ");
            var type3 = NetTypeManager.GetTypeInfo <object>();

            type3.BaseType.Should().BeNull();
        }
Ejemplo n.º 18
0
        public void Can_detect_array_type()
        {
            var type = NetTypeManager.GetTypeInfo <TestType13>();

            type.EnsureLoaded();
            var property   = type.GetProperty(0);
            var returnType = property.ReturnType;

            returnType.EnsureLoaded();
            returnType.IsArray.Should().BeTrue();
            returnType.IsList.Should().BeFalse();
        }
Ejemplo n.º 19
0
        public void Can_get_net_type()
        {
            var typeInfo = NetTypeManager.GetTypeInfo <TestType1>();

            typeInfo.EnsureLoaded();

            typeInfo.FullTypeName.Should().Be(typeof(TestType1).AssemblyQualifiedName);
            typeInfo.ClassName.Should().Be("TestType1");
            typeInfo.PrefVariantType.Should().Be(NetVariantType.Object);
            typeInfo.MethodCount.Should().Be(3); // the property has a "getter" and "setter" method
            typeInfo.PropertyCount.Should().Be(1);
        }
Ejemplo n.º 20
0
        public void Can_get_signal()
        {
            var type = NetTypeManager.GetTypeInfo <TestType7>();

            type.EnsureLoaded();
            type.SignalCount.Should().Be(1);
            var signal = type.GetSignal(0);

            signal.Name.Should().Be("testSignal");
            signal.ParameterCount.Should().Be(2);
            signal.GetParameter(0).Should().Be(NetVariantType.DateTime);
            signal.GetParameter(1).Should().Be(NetVariantType.Object);
        }
Ejemplo n.º 21
0
        public void Can_get_unique_id_for_property()
        {
            var type = NetTypeManager.GetTypeInfo <TestType18>();

            type.EnsureLoaded();

            var properties = new List <NetPropertyInfo>();

            for (var x = 0; x < type.PropertyCount; x++)
            {
                properties.Add(type.GetProperty(x));
            }

            properties.Select(x => x.Id).Distinct().Count().Should().Be(2);
        }
Ejemplo n.º 22
0
        public void Can_get_unique_id_for_method()
        {
            var type = NetTypeManager.GetTypeInfo <TestType17>();

            type.EnsureLoaded();

            var methods = new List <NetMethodInfo>();

            for (var x = 0; x < type.LocalMethodCount; x++)
            {
                methods.Add(type.GetMethod(x));
            }

            methods.Select(x => x.Id).Distinct().Count().Should().Be(2);
        }
Ejemplo n.º 23
0
        public void Can_get_property()
        {
            var type = NetTypeManager.GetTypeInfo <TestType6>();

            type.EnsureLoaded();
            type.PropertyCount.Should().Be(1);
            var property = type.GetProperty(0);

            property.Should().NotBeNull();
            property.Name.Should().Be("Property");
            property.CanRead.Should().BeTrue();
            property.CanWrite.Should().BeTrue();
            property.ReturnType.ClassName.Should().Be("String");
            property.ParentType.ClassName.Should().Be("TestType6");
            property.NotifySignal.Should().BeNull();
        }
Ejemplo n.º 24
0
        public void Can_detect_list_type()
        {
            var type = NetTypeManager.GetTypeInfo <TestType14>();

            type.EnsureLoaded();
            for (var x = 0; x < type.PropertyCount; x++)
            {
                var property = type.GetProperty(x);
                if (property.Name == "Prop1" || property.Name == "Prop2")
                {
                    property.ReturnType.EnsureLoaded();
                    property.ReturnType.IsList.Should().BeTrue();
                    property.ReturnType.IsArray.Should().BeFalse();
                }
            }
        }
Ejemplo n.º 25
0
        public void Can_lazy_load_types()
        {
            var type = NetTypeManager.GetTypeInfo <TestType11>();

            type.IsLoaded.Should().BeFalse();
            type.EnsureLoaded();
            type.MethodCount.Should().Be(1);
            type.IsLoaded.Should().BeTrue();
            var method = type.GetMethod(0);

            method.Should().NotBeNull();
            method.ReturnType.IsLoaded.Should().BeFalse();
            method.ReturnType.EnsureLoaded();
            method.ReturnType.MethodCount.Should().Be(0);
            method.ReturnType.IsLoaded.Should().BeTrue();
        }
Ejemplo n.º 26
0
        public void Can_invoke_method()
        {
            var o    = new TestObject();
            var type = NetTypeManager.GetTypeInfo <TestObject>();

            type.EnsureLoaded();
            var method   = type.GetMethod(0);
            var instance = NetReference.CreateForObject(o);

            // This will jump to native, to then call the .NET delegate (round trip).
            // The purpose is to simulate Qml invoking a method, sending .NET instance back.
            // We will inspect the returned instance that it got back to verify that it
            using (var parameter = new NetVariant())
                using (var list = new NetVariantList())
                {
                    parameter.Instance = instance;
                    list.Add(parameter);
                    Interop.Callbacks.InvokeMethod(method.Handle, instance.Handle, list.Handle, IntPtr.Zero);
                }

            o.Object.Should().NotBeNull();
            ReferenceEquals(o.Object, o).Should().BeTrue();
        }
Ejemplo n.º 27
0
 public static int RegisterType <T>(string uri, int versionMajor = 1, int versionMinor = 0)
 {
     return(RegisterType(NetTypeManager.GetTypeInfo <T>(), uri, typeof(T).Name, versionMajor, versionMinor));
 }
Ejemplo n.º 28
0
 protected CodeGenBase()
 {
     _mock     = new Mock <T>();
     _typeInfo = NetTypeManager.GetTypeInfo <T>();
     _typeInfo.EnsureLoaded();
 }
Ejemplo n.º 29
0
        public void LoadTypeInfo(IntPtr t)
        {
            using (var type = new NetTypeInfo(t))
            {
                var typeInfo = Type.GetType(type.FullTypeName);
                if (typeInfo == null)
                {
                    throw new InvalidOperationException($"Invalid type {type.FullTypeName}");
                }

                // Don't grab properties and methods for system-level types.
                if (Helpers.IsPrimitive(typeInfo))
                {
                    return;
                }

                if (typeInfo.IsArray)
                {
                    type.IsArray = true;
                }
                else
                {
                    if (typeof(IList).IsAssignableFrom(typeInfo))
                    {
                        type.IsList = true;
                    }
                    else if (typeInfo.IsGenericType)
                    {
                        if (typeof(IList <>).IsAssignableFrom(typeInfo.GetGenericTypeDefinition()))
                        {
                            type.IsList = true;
                        }
                    }
                }

                if (typeof(IQmlComponentCompleted).IsAssignableFrom(typeInfo))
                {
                    type.HasComponentCompleted = true;
                }

                if (typeof(IQmlObjectDestroyed).IsAssignableFrom(typeInfo))
                {
                    type.HasObjectDestroyed = true;
                }

                foreach (var methodInfo in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly))
                {
                    if (methodInfo.IsGenericMethod)
                    {
                        continue;                             // No generics supported.
                    }
                    if (Helpers.IsPrimitive(methodInfo.DeclaringType))
                    {
                        continue;
                    }

                    if (methodInfo.IsSpecialName)
                    {
                        continue;                           // Ignore the property get/set methods.
                    }
                    NetTypeInfo returnType = null;

                    if (methodInfo.ReturnParameter != null && methodInfo.ReturnParameter.ParameterType != typeof(void))
                    {
                        returnType =
                            NetTypeManager.GetTypeInfo(methodInfo.ReturnParameter.ParameterType);
                    }

                    var method = new NetMethodInfo(type, methodInfo.Name, returnType, methodInfo.IsStatic);

                    foreach (var parameter in methodInfo.GetParameters())
                    {
                        method.AddParameter(parameter.Name, NetTypeManager.GetTypeInfo(parameter.ParameterType));
                    }

                    type.AddMethod(method);
                }

                var signals = new Dictionary <string, NetSignalInfo>();

                foreach (var signalAttribute in typeInfo.GetCustomAttributes(false).OfType <SignalAttribute>())
                {
                    if (string.IsNullOrEmpty(signalAttribute.Name))
                    {
                        throw new InvalidOperationException($"Signal name was null for {typeInfo.Name}");
                    }

                    if (!char.IsLower(signalAttribute.Name[0]))
                    {
                        throw new InvalidOperationException($"Signal {signalAttribute.Name} for {typeInfo.Name} must begin with a lower case letter.");
                    }

                    var signal = new NetSignalInfo(type, signalAttribute.Name);
                    foreach (var parameter in signalAttribute.Parameters)
                    {
                        signal.AddParameter(parameter);
                    }
                    type.AddSignal(signal);
                    signals.Add(signal.Name, signal);
                }

                foreach (var propertyInfo in typeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
                {
                    if (Helpers.IsPrimitive(propertyInfo.DeclaringType))
                    {
                        continue;
                    }

                    NetSignalInfo notifySignal          = null;
                    var           notifySignalAttribute = propertyInfo.GetCustomAttribute <NotifySignalAttribute>();
                    if (notifySignalAttribute != null)
                    {
                        var name = notifySignalAttribute.Name;
                        if (string.IsNullOrEmpty(name))
                        {
                            name = $"{propertyInfo.Name}Changed";
                            name = char.ToLower(name[0]) + name.Substring(1);
                        }

                        if (signals.ContainsKey(name))
                        {
                            notifySignal = signals[name];

                            // Make sure the signal we are referencing has no parameters.
                            if (notifySignal.ParameterCount != 0)
                            {
                                // TODO: They can actually of parameters, but not implemented yet.
                                throw new Exception("Notify signals must have no parameters.");
                            }
                        }
                        else
                        {
                            if (!char.IsLower(name[0]))
                            {
                                throw new InvalidOperationException($"Signal {name} for {typeInfo.Name} must begin with a lower case letter.");
                            }

                            notifySignal = new NetSignalInfo(type, name);
                            type.AddSignal(notifySignal);
                        }
                    }

                    using (var property = new NetPropertyInfo(
                               type,
                               propertyInfo.Name,
                               NetTypeManager.GetTypeInfo(propertyInfo.PropertyType),
                               propertyInfo.CanRead,
                               propertyInfo.CanWrite,
                               notifySignal))
                    {
                        foreach (var indexParameter in propertyInfo.GetIndexParameters())
                        {
                            property.AddIndexParameter(indexParameter.Name, NetTypeManager.GetTypeInfo(indexParameter.ParameterType));
                        }

                        type.AddProperty(property);
                    }
                }

                // NOTE: This type is going to get a typeInfo object
                // with IsLoading=true and IsLoaded=false. It technically
                // is loaded, but it's misleading.
                InteropBehaviors.OnNetTypeInfoCreated(type, typeInfo);
            }
        }