RcBuilder@walla.com
call centre: 054-5614020

ToParams – string extension

28
May
2014
Posted by: RcBuilder  /   Category: Dictionary / Extensions / Linq / List / Reference Types / SOURCE CODE   /   1 Comment

ToParams – string extension

ResponseParams

public class ResponseParams
{
private Dictionary<string, string> _prms = null;

public ResponseParams() {
_prms = new Dictionary<string, string>();
}

public ResponseParams(IEnumerable<KeyValuePair<string, string>> prms) : this()
{
foreach (var p in prms)
_prms[p.Key] = p.Value;
}

public string this[string Name] {
get { return _prms[Name]; }
set {
if (!_prms.ContainsKey(Name)) // new
_prms.Add(Name, value);
else // update
_prms[Name] = value;
}
}
}

Extensions

internal static class Extensions
{
public static ResponseParams ToParams(this string me){
var res = from param in me.Split(‘&’)
let keypair = param.Split(‘=’)
select new KeyValuePair<string,string>(keypair[0], keypair[1]);

return new ResponseParams(res);
}
}

USING:
// result=1&p1=1&p2=2
string response = Invoke(“action=getgroups”);
var prms =response.ToParams();

if (prms["result"] != “1″) return null;

prms["p1"] // 1
prms["p2"] // 2

Author Avatar

About the Author

בניית אתרים ופתרונות טכנולוגים | RcBuilder

1 Comment

  1. Dopang August 5, 2014 at 6:14 am  /  Comment Reply

    you can use linq to objectsDictionary d = new Dictionary(); d.Add(1,”Test 1″); d.Add(2,”Test 2″); d.Add(5,”Test 5″); d.Add(3,”Test 3″); d.Add(4, “Test 4″); var srotedDct = (from i in d orderby i.Value ascending select i); srotedDct.Dump();


  • פיתוח מערכות
  • פתרונות טכנולוגים
  • קידום אתרים
  • בניית אתרים