public void TestSDKExceptions()
{
var allTypes = new List<Type>();
foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
{
allTypes.AddRange(ass.GetExportedTypes());
}
Assert.AreNotEqual(0, allTypes.Count);
var exceptionType = typeof(Exception);
var binding = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
var inputTypes = new Type[] { typeof(SerializationInfo), typeof(StreamingContext) };
var errors = new List<string>();
foreach (var type in allTypes)
{
var typeName = type.FullName;
if (type.FullName.IndexOf("Amazon.", StringComparison.OrdinalIgnoreCase) >= 0 &&
exceptionType.IsAssignableFrom(type))
{
var serializableAttributes = type.GetCustomAttributes(typeof(SerializableAttribute), false).ToList();
// exceptions must have [Serializable] attribute
if (serializableAttributes.Count == 0)
{
//exceptionsWithoutSerializeable.Add(type);
errors.Add("Exception " + type.FullName + " exception types are not serializeable");
}
// exceptions must have serialization constructor
var serializationConstructor = type.GetConstructor(binding, null, inputTypes, null);
if (serializationConstructor == null)
errors.Add("Type " + type.FullName + " missing serialization constructor");
// exceptions which have extra fields on them, must overload GetObjectData
var declaredFields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance);
if (declaredFields.Length > 0)
{
var getObjectDataMethod = type.GetMethod("GetObjectData", binding, null, inputTypes, null);
if (getObjectDataMethod == null)
errors.Add("Type " + type.FullName + " missing serialization GetObjectData method");
}
}
}
// report all errors
if (errors.Count > 0)
Assert.Fail("Errors found:" + string.Join(Environment.NewLine, errors.ToArray()));
}