DextopRemoteMethodInvokeResult Instantiate(IDextopRemotable target, String[] arguments) { try { if (arguments.Length < 1) { throw new InvalidDextopRemoteMethodCallException(); } InstantiateOptions options; if (arguments[0] != null && arguments[0].StartsWith("{")) { options = DextopUtil.Decode <InstantiateOptions>(arguments[0]); } else { options = new InstantiateOptions { subRemote = true, type = arguments[0] } }; if (options.type == null) { throw new InvalidDextopRemoteMethodCallException(); } String config = null; if (arguments.Length > 1) { config = arguments[1]; } if (options.type == null) { throw new InvalidDextopRemoteMethodCallException(); } List <RemotableConstructor> constructors; if (!constructorCache.TryGetValue(options.type, out constructors)) { String typeName; DextopConfig routeParams; if (MatchRoutes(options.type, out typeName, out routeParams)) { return(Instantiate(target, new[] { typeName, DextopUtil.Encode(routeParams) })); } if (constructors == null || constructors.Count == 0) { constructors = LoadRemotableConstructors(options.type); } } if (constructors == null || constructors.Count == 0) { throw new InvalidDextopRemoteMethodCallException(); } object[] args; RemotableConstructor c; if (config == null) { args = new object[0]; c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Default); if (c == null) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); args = new object[1]; } } else if (config.StartsWith("[")) { var argss = DextopUtil.Decode <String[]>(config); c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array && a.Args.Length >= argss.Length).OrderBy(a => a.Args.Length).FirstOrDefault(); if (c == null) { c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array).OrderByDescending(a => a.Args.Length).FirstOrDefault(); } if (c == null) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); } if (c == null) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Default); } if (c == null) { throw new InvalidDextopRemoteMethodCallException(); } args = new object[c.Args.Length]; for (var i = 0; i < c.Args.Length && i < argss.Length; i++) { args[i] = DextopUtil.DecodeValue(argss[i], c.Args[i].ParameterType); } } else if (config.StartsWith("{")) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); if (c != null) { args = new object[1] { DextopUtil.Decode(config, c.Args[0].ParameterType) }; } else { var configs = DextopUtil.Decode <Dictionary <String, String> >(config) ?? new Dictionary <String, String>(); var candidates = constructors.Select(a => new { Constructor = a, ArgumentMatch = a.Args.Count(b => configs.ContainsKey(b.Name)) }).OrderByDescending(x => x.ArgumentMatch).ToArray(); c = null; foreach (var cand in candidates) { if (cand.ArgumentMatch == configs.Count && cand.Constructor.Args.Length == cand.ArgumentMatch) { c = cand.Constructor; break; } } if (c == null) // if we still don't have a match take the candidate with the biggest number of matching arguments, and prefer parameterless constructor if zero { c = candidates.OrderByDescending(a => a.ArgumentMatch).ThenBy(a => (int)a.Constructor.ArgumentsType).First().Constructor; } args = new object[c.Args.Length]; for (var i = 0; i < c.Args.Length; i++) { String argString; if (configs.TryGetValue(c.Args[i].Name, out argString)) { args[i] = DextopUtil.DecodeValue(argString, c.Args[i].ParameterType); } } } } else { c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array && a.Args.Length >= 1).OrderBy(a => a.Args.Length).FirstOrDefault(); if (c == null) { throw new InvalidDextopRemoteMethodCallException(); } args = new object[c.Args.Length]; args[0] = DextopUtil.DecodeValue(config, c.Args[0].ParameterType); } var remotable = (IDextopRemotable)c.ConstructorInfo.Invoke(args); try { return(new DextopRemoteMethodInvokeResult { Success = true, Result = target.Remote.TrackRemotableComponent(remotable, remoteId: options.remoteId, subRemote: options.subRemote ?? true, own: options.own ?? true ) }); } catch { remotable.Dispose(); throw; } } catch (TargetInvocationException tix) { return(new DextopRemoteMethodInvokeResult { Success = false, Exception = tix.InnerException ?? tix }); } catch (Exception ex) { return(new DextopRemoteMethodInvokeResult { Success = false, Exception = ex }); } }
DextopRemoteMethodInvokeResult Instantiate(IDextopRemotable target, String[] arguments) { try { if (arguments.Length<1) throw new InvalidDextopRemoteMethodCallException(); InstantiateOptions options; if (arguments[0]!=null && arguments[0].StartsWith("{")) options = DextopUtil.Decode<InstantiateOptions>(arguments[0]); else options = new InstantiateOptions { subRemote = true, type = arguments[0] }; if (options.type == null) throw new InvalidDextopRemoteMethodCallException(); String config = null; if (arguments.Length > 1) config = arguments[1]; RemotableConstructor c; if (options.type == null || !constructorCache.TryGetValue(options.type, out c)) throw new InvalidDextopRemoteMethodCallException(); object[] args; if (c.Args == null || c.Args.Length == 0) args = new object[0]; else if (c.Args.Length == 1 && c.Args[0].ParameterType == typeof(DextopConfig)) { var rc = DextopUtil.Decode(config, c.Args[0].ParameterType); args = new object[] { rc }; } else { args = new object[c.Args.Length]; if (config == null) { } else if (config.StartsWith("[")) { var argss = DextopUtil.Decode<String[]>(config); for (var i = 0; i < c.Args.Length && i < argss.Length; i++) args[i] = DextopUtil.DecodeValue(argss[i], c.Args[i].ParameterType); } else { var configs = DextopUtil.Decode<Dictionary<String, String>>(config) ?? new Dictionary<String, String>(); for (var i = 0; i < c.Args.Length; i++) { String argString; if (configs.TryGetValue(c.Args[i].Name, out argString)) args[i] = DextopUtil.Decode(argString, c.Args[i].ParameterType); } } } var remotable = (IDextopRemotable)c.ConstructorInfo.Invoke(args); try { return new DextopRemoteMethodInvokeResult { Success = true, Result = target.Remote.TrackRemotableComponent(remotable, remoteId: options.remoteId, subRemote: options.subRemote ?? true, own : options.own ?? true ) }; } catch { remotable.Dispose(); throw; } } catch (TargetInvocationException tix) { return new DextopRemoteMethodInvokeResult { Success = false, Exception = tix.InnerException ?? tix }; } catch (Exception ex) { return new DextopRemoteMethodInvokeResult { Success = false, Exception = ex }; } }
DextopRemoteMethodInvokeResult Instantiate(IDextopRemotable target, String[] arguments) { try { if (arguments.Length < 1) { throw new InvalidDextopRemoteMethodCallException(); } InstantiateOptions options; if (arguments[0] != null && arguments[0].StartsWith("{")) { options = DextopUtil.Decode <InstantiateOptions>(arguments[0]); } else { options = new InstantiateOptions { subRemote = true, type = arguments[0] } }; if (options.type == null) { throw new InvalidDextopRemoteMethodCallException(); } String config = null; if (arguments.Length > 1) { config = arguments[1]; } RemotableConstructor c; if (options.type == null || !constructorCache.TryGetValue(options.type, out c)) { throw new InvalidDextopRemoteMethodCallException(); } object[] args; if (c.Args == null || c.Args.Length == 0) { args = new object[0]; } else if (c.Args.Length == 1 && c.Args[0].ParameterType == typeof(DextopConfig)) { var rc = DextopUtil.Decode(config, c.Args[0].ParameterType); args = new object[] { rc }; } else { args = new object[c.Args.Length]; if (config == null) { } else if (config.StartsWith("[")) { var argss = DextopUtil.Decode <String[]>(config); for (var i = 0; i < c.Args.Length && i < argss.Length; i++) { args[i] = DextopUtil.DecodeValue(argss[i], c.Args[i].ParameterType); } } else { var configs = DextopUtil.Decode <Dictionary <String, String> >(config) ?? new Dictionary <String, String>(); for (var i = 0; i < c.Args.Length; i++) { String argString; if (configs.TryGetValue(c.Args[i].Name, out argString)) { args[i] = DextopUtil.Decode(argString, c.Args[i].ParameterType); } } } } var remotable = (IDextopRemotable)c.ConstructorInfo.Invoke(args); try { return(new DextopRemoteMethodInvokeResult { Success = true, Result = target.Remote.TrackRemotableComponent(remotable, remoteId: options.remoteId, subRemote: options.subRemote ?? true, own: options.own ?? true ) }); } catch { remotable.Dispose(); throw; } } catch (TargetInvocationException tix) { return(new DextopRemoteMethodInvokeResult { Success = false, Exception = tix.InnerException ?? tix }); } catch (Exception ex) { return(new DextopRemoteMethodInvokeResult { Success = false, Exception = ex }); } }
DextopRemoteMethodInvokeResult Instantiate(IDextopRemotable target, String[] arguments) { try { if (arguments.Length<1) throw new InvalidDextopRemoteMethodCallException(); InstantiateOptions options; if (arguments[0]!=null && arguments[0].StartsWith("{")) options = DextopUtil.Decode<InstantiateOptions>(arguments[0]); else options = new InstantiateOptions { subRemote = true, type = arguments[0] }; if (options.type == null) throw new InvalidDextopRemoteMethodCallException(); String config = null; if (arguments.Length > 1) config = arguments[1]; if (options.type == null) throw new InvalidDextopRemoteMethodCallException(); List<RemotableConstructor> constructors; if (!constructorCache.TryGetValue(options.type, out constructors)) { String typeName; DextopConfig routeParams; if (MatchRoutes(options.type, out typeName, out routeParams)) return Instantiate(target, new[] { typeName, DextopUtil.Encode(routeParams) }); if (constructors==null || constructors.Count == 0) constructors = LoadRemotableConstructors(options.type); } if (constructors == null || constructors.Count == 0) throw new InvalidDextopRemoteMethodCallException(); object[] args; RemotableConstructor c; if (config == null) { args = new object[0]; c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Default); if (c == null) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); args = new object[1]; } } else if (config.StartsWith("[")) { var argss = DextopUtil.Decode<String[]>(config); c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array && a.Args.Length >= argss.Length).OrderBy(a => a.Args.Length).FirstOrDefault(); if (c == null) c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array).OrderByDescending(a => a.Args.Length).FirstOrDefault(); if (c == null) c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); if (c == null) c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Default); if (c == null) throw new InvalidDextopRemoteMethodCallException(); args = new object[c.Args.Length]; for (var i = 0; i < c.Args.Length && i < argss.Length; i++) args[i] = DextopUtil.DecodeValue(argss[i], c.Args[i].ParameterType); } else if (config.StartsWith("{")) { c = constructors.FirstOrDefault(a => a.ArgumentsType == ConstructorArgments.Hash); if (c != null) { args = new object[1] { DextopUtil.Decode(config, c.Args[0].ParameterType) }; } else { var configs = DextopUtil.Decode<Dictionary<String, String>>(config) ?? new Dictionary<String, String>(); var candidates = constructors.Select(a => new { Constructor = a, ArgumentMatch = a.Args.Count(b => configs.ContainsKey(b.Name)) }).OrderByDescending(x => x.ArgumentMatch).ToArray(); c = null; foreach (var cand in candidates) { if (cand.ArgumentMatch == configs.Count && cand.Constructor.Args.Length == cand.ArgumentMatch) { c = cand.Constructor; break; } } if (c == null) // if we still don't have a match take the candidate with the biggest number of matching arguments, and prefer parameterless constructor if zero c = candidates.OrderByDescending(a => a.ArgumentMatch).ThenBy(a => (int)a.Constructor.ArgumentsType).First().Constructor; args = new object[c.Args.Length]; for (var i = 0; i < c.Args.Length; i++) { String argString; if (configs.TryGetValue(c.Args[i].Name, out argString)) args[i] = DextopUtil.DecodeValue(argString, c.Args[i].ParameterType); } } } else { c = constructors.Where(a => a.ArgumentsType == ConstructorArgments.Array && a.Args.Length >= 1).OrderBy(a => a.Args.Length).FirstOrDefault(); if (c == null) throw new InvalidDextopRemoteMethodCallException(); args = new object[c.Args.Length]; args[0] = DextopUtil.DecodeValue(config, c.Args[0].ParameterType); } var remotable = (IDextopRemotable)c.ConstructorInfo.Invoke(args); try { return new DextopRemoteMethodInvokeResult { Success = true, Result = target.Remote.TrackRemotableComponent(remotable, remoteId: options.remoteId, subRemote: options.subRemote ?? true, own : options.own ?? true ) }; } catch { remotable.Dispose(); throw; } } catch (TargetInvocationException tix) { return new DextopRemoteMethodInvokeResult { Success = false, Exception = tix.InnerException ?? tix }; } catch (Exception ex) { return new DextopRemoteMethodInvokeResult { Success = false, Exception = ex }; } }