public void WriteProperty(IntPtr p, IntPtr t, IntPtr ip, IntPtr v) { using (var property = new NetPropertyInfo(p)) using (var target = new NetReference(t)) using (var indexParameter = ip != IntPtr.Zero ? new NetVariant(ip) : null) using (var value = new NetVariant(v)) { var o = target.Instance; var propertInfo = o.GetType() .GetProperty(property.Name, BindingFlags.Instance | BindingFlags.Public); if (propertInfo == null) { throw new InvalidOperationException($"Invalid property {property.Name}"); } object newValue = null; Helpers.Unpackvalue(ref newValue, value); if (indexParameter != null) { object indexParameterValue = null; Helpers.Unpackvalue(ref indexParameterValue, indexParameter); propertInfo.SetValue(o, newValue, new[] { indexParameterValue }); } else { propertInfo.SetValue(o, newValue); } } }
private static IntPtr Create(List <string> args) { if (args == null) { args = new List <string>(); } // By default, the argv[0] should be the process name. // .NET doesn't pass that name, but Qt should get it // since it does in a normal Qt environment. args.Insert(0, System.Diagnostics.Process.GetCurrentProcess().ProcessName); using (var strings = new NetVariantList()) { foreach (var arg in args) { using (var variant = new NetVariant()) { variant.String = arg; strings.Add(variant); } } return(Interop.QGuiApplication.Create(strings.Handle, IntPtr.Zero)); } }
public override void InvokeMethod(NetMethodInfo methodInfo, NetInstance target, NetVariantVector parameters, NetVariant result) { var handle = (GCHandle)target.GetGCHandle(); var o = handle.Target; List <object> methodParameters = null; if (parameters.Count > 0) { methodParameters = new List <object>(); foreach (var parameterInstance in parameters) { object v = null; Unpackvalue(ref v, parameterInstance); methodParameters.Add(v); } } var r = o.GetType() .GetMethod(methodInfo.GetMethodName(), BindingFlags.Instance | BindingFlags.Public) .Invoke(o, methodParameters?.ToArray()); if (result == null) { // this method doesn't have return type } else { PackValue(ref r, result); } }
public static void Unpackvalue(ref object destination, NetVariant source) { switch (source.VariantType) { case NetVariantType.Invalid: destination = null; break; case NetVariantType.Bool: destination = source.Bool; break; case NetVariantType.Char: destination = source.Char; break; case NetVariantType.Int: destination = source.Int; break; case NetVariantType.UInt: destination = source.UInt; break; case NetVariantType.Long: destination = source.Long; break; case NetVariantType.ULong: destination = source.ULong; break; case NetVariantType.Float: destination = source.Float; break; case NetVariantType.Double: destination = source.Double; break; case NetVariantType.String: destination = source.String; break; case NetVariantType.DateTime: destination = source.DateTime; break; case NetVariantType.Object: destination = source.Instance.Instance; break; case NetVariantType.JsValue: destination = source.JsValue.AsDynamic(); break; default: throw new Exception("Unsupported variant type."); } }
public bool ActivateSignal(string signalName, params object[] parameters) { QmlNetConfig.EnsureUIThread(); if (parameters != null && parameters.Length > 0) { using (var list = new NetVariantList()) { foreach (var parameter in parameters) { using (var variant = new NetVariant()) { Helpers.PackValue(parameter, variant); list.Add(variant); } } return(Interop.NetReference.ActivateSignal(Handle, signalName, list.Handle) == 1); } } else { return(Interop.NetReference.ActivateSignal(Handle, signalName, IntPtr.Zero) == 1); } }
public void WriteProperty(IntPtr p, IntPtr t, IntPtr ip, IntPtr v) { using (var property = new NetPropertyInfo(p)) using (var target = new NetReference(t)) using (var indexParameter = ip != IntPtr.Zero ? new NetVariant(ip) : null) using (var value = new NetVariant(v)) { CodeGen.CodeGen.InvokeMethodDelegate del; if (!_cachedSetProperties.TryGetValue(property.Id, out del)) { del = CodeGen.CodeGen.BuildSetPropertyDelegate(property); _cachedSetProperties[property.Id] = del; } using (var list = new NetVariantList()) { if (indexParameter != null) { list.Add(indexParameter); } list.Add(value); Task resultTask = null; del(target, list, null, ref resultTask); } } }
private static object Obj(NetVariant variant) { using (var reference = variant.Instance) { return(reference?.Instance); } }
public void Can_store_variant_list() { using (var variant = new NetVariant()) using (var variantList = new NetVariantList()) { using (var value1 = NetVariant.From(3)) { variantList.Add(value1); } variant.VariantType.Should().Be(NetVariantType.Invalid); variant.NetVariantList = variantList; variant.VariantType.Should().Be(NetVariantType.NetVariantList); using (var resultVariantList = variant.NetVariantList) { resultVariantList.Should().NotBeNull(); resultVariantList.Count.Should().Be(variantList.Count); using (var value1 = resultVariantList.Get(0)) using (var value2 = variantList.Get(0)) { value1.Int.Should().Be(3); value2.Int.Should().Be(3); } } } }
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"); } }
public static void PackValue(object source, NetVariant destination) { if (source == null) { destination.Clear(); } else { var type = source.GetType(); if (type == typeof(bool)) { destination.Bool = (bool)source; } else if (type == typeof(char)) { destination.Char = (char)source; } else if (type == typeof(float)) { destination.Float = (float)source; } else if (type == typeof(double)) { destination.Double = (double)source; } else if (type == typeof(int)) { destination.Int = (int)source; } else if (type == typeof(uint)) { destination.UInt = (uint)source; } else if (type == typeof(long)) { destination.Long = (long)source; } else if (type == typeof(ulong)) { destination.ULong = (ulong)source; } else if (type == typeof(string)) { destination.String = (string)source; } else if (type == typeof(DateTimeOffset)) { destination.DateTime = ((DateTimeOffset)source).DateTime; } else if (typeof(INetJsValue).IsAssignableFrom(type)) { destination.JsValue = ((NetJsValue.NetJsValueDynamic)source).JsValue; } else { destination.Instance = NetReference.CreateForObject(source); } } }
private static void LoadObject(NetVariant variant, object value) { if (value != null) { if (value is bool valueBool) { variant.Bool = valueBool; } else if (value is char valueChar) { variant.Char = valueChar; } else if (value is int valueInt) { variant.Int = valueInt; } else if (value is uint valueUInt) { variant.UInt = valueUInt; } else if (value is long valueLong) { variant.Long = valueLong; } else if (value is ulong valueULong) { variant.ULong = valueULong; } else if (value is float valueFloat) { variant.Float = valueFloat; } else if (value is double valueDouble) { variant.Double = valueDouble; } else if (value is string valueString) { variant.String = valueString; } else if (value is DateTimeOffset valueDateTimeOffset) { variant.DateTime = valueDateTimeOffset; } else if (value is NetJsValue valueJsValue) { variant.JsValue = valueJsValue; } else { variant.Instance = NetReference.CreateForObject(value); } } else { variant.Clear(); } }
private static ulong?ULongNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.ULong); }
private static uint?UIntNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.UInt); }
private static double?DoubleNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.Double); }
private static bool?BoolNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.Bool); }
private static char?CharNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.Char); }
public void Can_clear_value() { var variant = new NetVariant(); variant.String = "test"; variant.VariantType.Should().Be(NetVariantType.String); variant.Clear(); variant.VariantType.Should().Be(NetVariantType.Invalid); }
public void Can_store_null() { using (var variant = new NetVariant()) { variant.VariantType.Should().Be(NetVariantType.Invalid); variant.SetNull(); variant.VariantType.Should().Be(NetVariantType.Null); } }
private static float?FloatNullable(NetVariant variant) { if (variant.VariantType == NetVariantType.Invalid) { return(null); } return(variant.Float); }
public void Can_store_double() { var variant = new NetVariant(); variant.Double = double.MinValue; variant.VariantType.Should().Be(NetVariantType.Double); variant.Double.Should().Be(double.MinValue); variant.Double = double.MaxValue; variant.Double.Should().Be(double.MaxValue); }
public void Can_store_bool() { var variant = new NetVariant(); variant.Bool = true; variant.VariantType.Should().Be(NetVariantType.Bool); variant.Bool.Should().BeTrue(); variant.Bool = false; variant.Bool.Should().BeFalse(); }
public void Can_store_long() { var variant = new NetVariant(); variant.Long = -1; variant.VariantType.Should().Be(NetVariantType.Long); variant.Long.Should().Be(-1); variant.Long = long.MaxValue; variant.Long.Should().Be(long.MaxValue); }
public void Can_store_uint() { var variant = new NetVariant(); variant.UInt = uint.MinValue; variant.VariantType.Should().Be(NetVariantType.UInt); variant.UInt.Should().Be(uint.MinValue); variant.UInt = uint.MaxValue; variant.UInt.Should().Be(uint.MaxValue); }
public void Can_store_int() { var variant = new NetVariant(); variant.Int = -1; variant.VariantType.Should().Be(NetVariantType.Int); variant.Int.Should().Be(-1); variant.Int = int.MaxValue; variant.Int.Should().Be(int.MaxValue); }
public void Can_store_char() { var variant = new NetVariant(); variant.Char = 'Ώ'; variant.VariantType.Should().Be(NetVariantType.Char); variant.Char.Should().Be('Ώ'); variant.Char = ' '; variant.Char.Should().Be(' '); }
public void Can_store_ulong() { var variant = new NetVariant(); variant.ULong = ulong.MinValue; variant.VariantType.Should().Be(NetVariantType.ULong); variant.ULong.Should().Be(ulong.MinValue); variant.ULong = ulong.MaxValue; variant.ULong.Should().Be(ulong.MaxValue); }
public void Can_store_float() { var variant = new NetVariant(); variant.Float = float.MinValue; variant.VariantType.Should().Be(NetVariantType.Float); variant.Float.Should().Be(float.MinValue); variant.Float = float.MaxValue; variant.Float.Should().Be(float.MaxValue); }
private static void LoadBoolNullable(NetVariant variant, bool?value) { if (value.HasValue) { variant.Bool = value.Value; } else { variant.Clear(); } }
private static void LoadUIntNullable(NetVariant variant, uint?value) { if (value.HasValue) { variant.UInt = value.Value; } else { variant.Clear(); } }
private static void LoadCharNullable(NetVariant variant, char?value) { if (value.HasValue) { variant.Char = value.Value; } else { variant.Clear(); } }