private static string GetContextProperty(SageContext context, string propName, string propKey)
{
const BindingFlags BindingFlags =
BindingFlags.IgnoreCase |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.Static;
PropertyInfo property = context.GetType().GetProperty(propName, BindingFlags);
if (property == null)
{
log.ErrorFormat(
"Property name '{0}' is invalid. Please make sure the name matches a property of SageContext",
propName);
return null;
}
object value = property.GetValue(context, null);
if (value != null)
{
if (!string.IsNullOrEmpty(propKey))
{
if (value is NameValueCollection)
return ((NameValueCollection) value)[propKey];
object subProperty = value.GetType().GetProperty(propKey, BindingFlags);
if (subProperty != null)
{
return subProperty.ToString();
}
log.ErrorFormat(
"The context property '{0}' is not a NameValue collection and it doesn't have a property named '{1}'.",
propName, propKey);
return null;
}
return value.ToString();
}
return null;
}