Ejemplo n.º 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());
        }
Ejemplo n.º 2
0
 public static object FromString(ContractValue contract, string value)
 {
     if (stringtovalue.TryGetValue(contract.GetTypeFromTask(), out ConvertFromString function))
     {
         return(function(value));
     }
     throw new InvokeException($"no value-converter for {contract.Name}");
 }
Ejemplo n.º 3
0
 private static string ToString(ContractValue contract, object value)
 {
     if (valuetostring.TryGetValue(contract.GetTypeFromTask(), out ConvertToString function))
     {
         return(function(value));
     }
     throw new InvokeException($"no value-converter for {contract.Name}");
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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}"]));
        }
Ejemplo n.º 6
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()));
            }
        }
Ejemplo n.º 7
0
 public static Type GetTypeFromTask(this ContractValue contract)
 {
     return(GetTypeFromTask(contract.Type));
 }
Ejemplo n.º 8
0
 public static object FromValues(ContractValue contract, NameValueCollection parms)
 {
     return(FromValues(contract, parms, contract.Name));
 }
Ejemplo n.º 9
0
 public static void GetValues(ContractValue contract, object value, NameValueCollection parms)
 {
     GetValues(contract, value, parms, contract.Name);
 }