1

I have a class RspServer that contains few properties (Getters/Setter). With every property, I have an attribute associated with it. Server is returning a NameValueCollection object, that contains key & value for RspServer class. How can I get values for this class RspServer from NameValueCollection object using Reflection. Please advise

public class RspServer
{
    private string _receiptNum,_respCode;

    [Description("Receipt Number")]
    public string ReceiptNum
    {
        get { return _receiptNum; }
        set { _receiptNum = value; }
    }

    [Description("Response Code")]
    public string RespCode
    {
        get { return _respCode; }
        set { _respCode = value; }
    }
}
M.Y.Mnu
  • 718
  • 8
  • 22
JosephCenk
  • 327
  • 1
  • 5
  • 15
  • And this one: https://stackoverflow.com/questions/2281972/how-to-get-a-list-of-properties-with-a-given-attribute – J M Jan 10 '18 at 13:03
  • 1
    Your title is almost identical to the title of the duplicate. Did you even try to [search](https://www.google.co.il/search?rlz=1C1LENP_iwIL718IL718&ei=cQ5WWuW-EMKvkwX4kaTIAw&q=Get+all+properties+values+which+marked+certain+attribute+c%23&oq=Get+all+properties+values+which+marked+certain+attribute+c%23&gs_l=psy-ab.3...10318.12304.0.12708.4.4.0.0.0.0.179.601.0j4.4.0....0...1c.1.64.psy-ab..0.2.317...33i160k1j33i22i29i30k1.0.jHdzpnj6Hns) before posting the question? I guess not, because pasting your title to google and replacing `Reflection` with `c#` gave the duplicate as the first search result. – Zohar Peled Jan 10 '18 at 13:03
  • 1
    Do you want to list all properties with an Description attribute or do you want do use the Description value as key for the value of the property? – Phillip Jan 10 '18 at 13:04
  • @Phillip want to use Description value as key for the value of the property – JosephCenk Jan 10 '18 at 13:05
  • Or, does the op want the values of the properties that are marked by the attribute? Or the value of the properties marked with attributes with the value? Very unclear... I'm not actually sure we can say this is a duplicate yet as we have no idea what the op wants to do – J M Jan 10 '18 at 13:09
  • @JM Want to get value of the properties marked with attributes – JosephCenk Jan 10 '18 at 13:15
  • @JosephCenk: there u go. – Phillip Jan 10 '18 at 13:23
  • The question I linked to above has numerous solutions to this problem. It is a mature question to which several very experienced posters have contributed. The benefit of searching the previously asked questions is that a lot of knowledge has been packed into them. Check it out: https://stackoverflow.com/questions/2281972/how-to-get-a-list-of-properties-with-a-given-attribute?noredirect=1&lq=1 – J M Jan 10 '18 at 13:28

1 Answers1

1

Not shure what result you are trying to get but this should help:

        RspServer server = new RspServer();

        Type ClassType = server.GetType();

        Dictionary<string, string> Description2Value = new Dictionary<string, string>();

        foreach (PropertyInfo pi in ClassType.GetProperties().Where(pi => Attribute.IsDefined(pi, typeof(Description))))
        {
            Description d = (Description)pi.GetCustomAttributes(typeof(Description), false)[0];
            string PropVal = (string)pi.GetValue(server);

            Description2Value.Add(d.Value, PropVal);
        }
Phillip
  • 789
  • 4
  • 22