简体   繁体   中英

How to retrieve the specific value from Dictionary(key,value) in c#

This is my method:

/// <summary>
/// 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.
/// </summary>
/// <param name="mandatoryFields">List of string - properties</param>
/// <param name="o">object of the current class</    
/// <param name="message">holds the message for end user to display</param>
/// <returns>The name of the property</returns>   
public static bool CheckMandatoryFields(Dictionary<string,string > mandatoryFields, object o,out StringBuilder  message)
{
    message = new StringBuilder();
    if(mandatoryFields !=null && mandatoryFields.Count>0)
    {
        var sourceType = o.GetType();
        var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
        for (var i = 0; i < 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>0)
        {
            return false;
        }
    }
    return true;
}

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.

I hope i am clear.

Do the loop the other way:

public static bool CheckMandatoryFields(Dictionary<string,string > mandatoryFields, object o,out StringBuilder  message)
{
    message = new StringBuilder();
    if(mandatoryFields == null || mandatoryFields.Count == 0)
    {
        return true;
    }

    var sourceType = o.GetType();
    foreach (var mandatoryField in mandatoryFields) {
        var property = sourceType.GetProperty(mandatoryField.Key, BindingFlags.Public | BindingFlags.Static);
        if (property == null) {
            continue;
        }

        if (string.IsNullOrEmpty(property.GetValue(o, null).ToString()))
        {
            message.AppendLine(string.Format("{0} name is blank.", mandatoryField.Value));
        }
    }

    return message.ToString().Trim().Length == 0;
}

This way you iterate over the properties you want to check, so you always have a handle on the "current" property and know the corresponding key and value from the dictionary.

The snippet

if (property == null) {
    continue;
}

causes the function to treat properties that exist as names in the dictionary but not as actual properties on the type to be treated as valid, to mirror what your original code does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM