public void TestAllAvailableTypesInLoadedAssemblies() { StringBuilder builder = new StringBuilder(); SosTypeSerializationVerifier verifier = new SosTypeSerializationVerifier(); Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < loadedAssemblies.Length; i++) { Assembly assembly = loadedAssemblies[i]; PortableExecutableKinds peKind; ImageFileMachine imageFileMachine; assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine); Console.WriteLine("Assembly {0} PEKind {1} ImageFileMachine {2}", assembly, peKind, imageFileMachine); Type[] types = assembly.GetTypes(); for (int j = 0; j < types.Length; j++) { Type type = types[j]; if (type == typeof(String)) { continue; } if (!type.IsVisible) { continue; } if (type.FullName == "System.Net.Mail.MSAdminBase") { continue; } /* * Console.WriteLine("Testing '{0}' {1}", type.FullName, type.Serialize()); * Console.WriteLine(" type.AssemblyQualifiedName = " + type.AssemblyQualifiedName); * Console.WriteLine(" type.Attributes = " + type.Attributes); * Console.WriteLine(" type.IsCOMObject = " + type.IsCOMObject); * Console.WriteLine(" type.IsContextful = " + type.IsContextful); * Console.WriteLine(" type.IsNested = " + type.IsNested); */ String cannotBeSerializedBecause = verifier.CannotBeSerializedBecause(type); if (cannotBeSerializedBecause == null) { Console.WriteLine("{0} {1}", type.SosTypeName(), type.SosTypeDefinition()); Util.TestSerializer(builder, FormatterServices.GetUninitializedObject(type)); } else { Console.WriteLine("{0} Cannot be serialized because {1}", type.FullName, cannotBeSerializedBecause); } } } }
public void VerificationTests() { SosTypeSerializationVerifier verifier = new SosTypeSerializationVerifier(); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Boolean))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(SByte))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Byte))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Int16))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(UInt16))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Int16))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Int32))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(UInt32))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Int64))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(UInt64))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Char))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(String))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(Object))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(DayOfWeek))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(NoEmptyConstructor))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(SubclassHasNoEmptyConstructor))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(ClassWithPrimitiveTypes))); Assert.IsNull(verifier.CannotBeSerializedBecause(typeof(StructWithPrimitiveTypes))); Assert.IsNotNull(verifier.CannotBeSerializedBecause(typeof(AbstractClass))); Assert.IsNotNull(verifier.CannotBeSerializedBecause(typeof(GenericClass <Int32>))); Assert.IsNotNull(verifier.CannotBeSerializedBecause(typeof(ClassWithWeirdTypes))); }
// // Every function parameter type and return type must be registered. // Registration checks that the type can be serialized and also saves the type information to send // to the client. // void RegisterType(SosTypeSerializationVerifier verifier, Type type) { if (type == typeof(void) || type.IsSosPrimitive()) { return; } if (type.IsArray) { RegisterType(verifier, type.GetElementType()); return; } Type alreadyRegisteredType; if (enumAndObjectTypesDictionary.TryGetValue(type.FullName, out alreadyRegisteredType)) { if (alreadyRegisteredType != type) { throw new InvalidOperationException(String.Format("Error: there are 2 different types with the same name '{0}'", type.FullName)); } return; } String because = verifier.CannotBeSerializedBecause(type); if (because != null) { throw new InvalidOperationException(String.Format( "The type '{0}' cannot be serialized because {1}", type.FullName, because)); } enumAndObjectTypesDictionary.Add(type.FullName, type); OneOrMoreTypes shortNameTypeGroup; if (enumAndObjectTypesWithUniqueShortNamesDictionary.TryGetValue(type.Name, out shortNameTypeGroup)) { if (shortNameTypeGroup.otherTypes == null) { shortNameTypeGroup.otherTypes = new List <Type>(); } shortNameTypeGroup.otherTypes.Add(type); } else { enumAndObjectTypesWithUniqueShortNamesDictionary.Add(type.Name, new OneOrMoreTypes(type)); } if (!type.IsEnum) { // // Register the user defined types fields // FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (fieldInfos == null || fieldInfos.Length <= 0) { return; } for (int i = 0; i < fieldInfos.Length; i++) { RegisterType(verifier, fieldInfos[i].FieldType); } } }