Beispiel #1
0
        public static string ToJSON(ContractValue contract, object value)
        {
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            using (var writer = new Newtonsoft.Json.JsonTextWriter(sw))
            {
                writer.WriteStartObject();
                var datacontract = ServicesFactory.GetDataContract(contract.Type);

                if (datacontract != null)
                {
                    foreach (var p in datacontract.Properties)
                    {
                        var v = p.Property.GetValue(value, new object[0]);

                        writer.WritePropertyName(p.Name);
                        writer.WriteValue(ValueConverter.ToString(p, v));
                    }
                }
                else
                {
                    writer.WritePropertyName("result");
                    writer.WriteValue(ValueConverter.ToString(contract, value));
                }
                writer.WriteEndObject();
            }
            return(sb.ToString());
        }
Beispiel #2
0
        private static void GetValues(ContractValue contract, object value, NameValueCollection parms, string header)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                foreach (var p in datacontract.Properties)
                {
                    object vv = p.Property.GetValue(value, new object[0]);
                    GetValues(p, vv, parms, $"{header}_{p.Name}");
                }
                return;
            }
            parms[$"{header}"] = ToString(contract, value);
        }
Beispiel #3
0
        private static object FromValues(ContractValue contract, NameValueCollection parms, string header)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                var result = Activator.CreateInstance(datacontract.Type);

                foreach (var p in datacontract.Properties)
                {
                    p.Property.SetValue(result, FromValues(p, parms, $"{header}_{p.Name}"), new object[0]);
                }
                return(result);
            }
            return(FromString(contract, parms[$"{header}"]));
        }
        public static ServiceDef GetServiceDefinition(Type type)
        {
            using (ReadLock(lockobj))
            {
                if (!servicedefs.TryGetValue(type, out ServiceDef service))
                {
                    var interfaces = _GetInterfaces(type).Where(_t => _t.GetCustomAttributes <ContractAttribute>().Any()).ToList();

                    if (type.IsInterface)
                    {
                        interfaces.Insert(0, type);
                    }

                    service = new ServiceDef()
                    {
                        Type = type
                    };
                    var l = interfaces.Select(_t => CreateInterfaceContract(service, _t)).ToList();
                    if (!type.IsInterface)
                    {
                        l.Insert(0, CreateInterfaceContract(service, type));
                    }
                    service.Contracts = l.ToArray();// interfaces.Select(_t => CreateInterfaceContract(service, _t)).ToArray();
                    ServicesFactory.CheckDataContracts(service);

                    var cookie = lockobj.UpgradeToWriterLock(-1);
                    try
                    {
                        servicedefs[type] = service;
                    }
                    catch { throw; }
                    finally
                    {
                        lockobj.DowngradeFromWriterLock(ref cookie);
                    }
                }
                return(service);
            }
        }
Beispiel #5
0
        internal static object FromJson(ContractValue contract, JObject data)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                //   var obj = data[contract.Name].Value<object>();

                var result = Activator.CreateInstance(datacontract.Type);

                foreach (var p in datacontract.Properties)
                {
                    p.Property.SetValue(result, data[p.Name].ToObject(p.Type));
                }
                return(result);
            }
            else
            {
                var obj = data["result"].Value <JValue>();
                return(obj.ToObject(contract.GetTypeFromTask()));
            }
        }