How to retrieve the specific value from Dictionary(key,value) in c#
<p>This is my method:</p>

<pre><code>/// &lt;summary&gt;
/// Uses Dictionary(Key,Value) where key is the property and value is the field name.
/// Matches the dictionary of mandatory fields with object properties
/// and checks whether the current object has values in it or
/// not.
/// &lt;/summary&gt;
/// &lt;param name="mandatoryFields"&gt;List of string - properties&lt;/param&gt;
/// &lt;param name="o"&gt;object of the current class&lt;/    
/// &lt;param name="message"&gt;holds the message for end user to display&lt;/param&gt;
/// &lt;returns&gt;The name of the property&lt;/returns&gt;   
public static bool CheckMandatoryFields(Dictionary&lt;string,string &gt; mandatoryFields, object o,out StringBuilder  message)
{
    message = new StringBuilder();
    if(mandatoryFields !=null &amp;&amp; mandatoryFields.Count&gt;0)
    {
        var sourceType = o.GetType();
        var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
        for (var i = 0; i &lt; properties.Length; i++)
        {
            if (mandatoryFields.Keys.Contains(properties[i].Name))
            {
                if (string.IsNullOrEmpty( properties[i].GetValue(o, null).ToString()))
                {
                    message.AppendLine(string.Format("{0} name is blank.", mandatoryFields.Values));
                }
            }
        }
        if(message.ToString().Trim().Length&gt;0)
        {
            return false;
        }
    }
    return true;
}
</code></pre>

<p>In this I have params Dictionary which will hold the property name of the class and its corresponding fieldname from the UI(manually fed by developer in the business layer or UI).
So what I want is that when the property is on the way to validate, if the property is found null or blank, then its corresponding fieldname, which is actually the value of the dictionary will get added to the stringbuilder message in the method above.</p>

<p>I hope i am clear.</p>
