public static object SaveViewState(object obj)
{
List<PropertyInfo> result = GetProperties(obj);
List<object> states = new List<object>();
foreach (PropertyInfo property in result)
{
try
{
object value = property.GetValue(obj, null);
if (value == null)
{
continue;
}
IStateManager sm = value as IStateManager;
if (sm == null)
{
throw new InvalidOperationException("The property ".ConcatWith(property, " in the class ", obj.GetType().Name, " is not IStateManager but marked as ViewStateMember"));
}
object propertyState = ((IStateManager)value).SaveViewState();
if (propertyState != null)
{
states.Add(new Pair(property.Name, propertyState));
}
}
catch (Exception ex)
{
throw new Exception("Error during SaveViewState initialization. "
+ property.Name
+ " - "
+ ex.Message, ex);
}
}
return states.Count == 0 ? null : states.ToArray();
}