Пример #1
0
        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)));
        }
        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);
                    }
                }
            }
        }
Пример #3
0
        public NpcReflector(params Object [] executionObjects)
        {
            if (executionObjects == null)
            {
                throw new ArgumentNullException("executionObjects");
            }
            if (executionObjects.Length <= 0)
            {
                throw new ArgumentException("exeuctionObjects must have at least one object", "executionObjects");
            }

            this.npcExecutionObjects = new NpcExecutionObject[executionObjects.Length];

            this.interfaceSet = new NpcInterfaceInfo.Set(new Dictionary <Type, NpcInterfaceInfo>());
            this.methodList   = new List <NpcMethodOverloadable>();
            this.withObjectMethodDictionary   = new Dictionary <String, NpcMethodOverloadable>(StringComparer.OrdinalIgnoreCase);
            this.noObjectDictionary           = new Dictionary <String, List <NpcMethodOverloadable> >(StringComparer.OrdinalIgnoreCase);
            this.enumAndObjectTypesDictionary = new Dictionary <String, Type>();
            this.enumAndObjectTypesWithUniqueShortNamesDictionary = new Dictionary <String, OneOrMoreTypes>();

            //
            // Find all methods that are apart of an [NpcInterface]
            //
            SosTypeSerializationVerifier verifier = new SosTypeSerializationVerifier();

            for (int objectIndex = 0; objectIndex < executionObjects.Length; objectIndex++)
            {
                Object             executionObject    = executionObjects[objectIndex];
                NpcExecutionObject npcExecutionObject = executionObject as NpcExecutionObject;
                if (npcExecutionObject == null)
                {
                    npcExecutionObject = new NpcExecutionObject(executionObject);
                }
                npcExecutionObject.InitializeInterfaces(this.interfaceSet);
                npcExecutionObjects[objectIndex] = npcExecutionObject;

                foreach (NpcInterfaceInfo interfaceInfo in npcExecutionObject.ancestorNpcInterfaces)
                {
                    //
                    // Add all the methods
                    //
                    NpcMethodInfo[] npcMethodInfos = interfaceInfo.npcMethods;
                    for (int methodIndex = 0; methodIndex < npcMethodInfos.Length; methodIndex++)
                    {
                        NpcMethodInfo npcMethodInfo = npcMethodInfos[methodIndex];
                        //Console.WriteLine("   [NpcDebug] Registering types for method '{0}'", npcMethodInfo.methodName);

                        //
                        // Check that all parameter types can be parsed
                        //
                        for (UInt16 k = 0; k < npcMethodInfo.parametersLength; k++)
                        {
                            RegisterType(verifier, npcMethodInfo.parameters[k].ParameterType);
                        }

                        //
                        // Find the appropriate ToString method for the return type
                        //
                        RegisterType(verifier, npcMethodInfo.methodInfo.ReturnType);

                        //
                        // Add method info to dictionary
                        //
                        String objectMethodName = npcExecutionObject.objectName + "." + npcMethodInfo.methodName;
                        NpcMethodOverloadable overloadableMethod;
                        if (withObjectMethodDictionary.TryGetValue(objectMethodName, out overloadableMethod))
                        {
                            overloadableMethod.AddOverload(npcMethodInfo);
                        }
                        else
                        {
                            overloadableMethod = new NpcMethodOverloadable(npcExecutionObject, npcMethodInfo);
                            methodList.Add(overloadableMethod);
                            withObjectMethodDictionary.Add(objectMethodName, overloadableMethod);
                        }

                        List <NpcMethodOverloadable> methodsWithSameShortName;
                        if (!noObjectDictionary.TryGetValue(npcMethodInfo.methodName, out methodsWithSameShortName))
                        {
                            methodsWithSameShortName = new List <NpcMethodOverloadable>();
                            noObjectDictionary.Add(npcMethodInfo.methodName, methodsWithSameShortName);
                        }
                        methodsWithSameShortName.Add(overloadableMethod);
                    }
                }
            }
        }
Пример #4
0
        //
        // 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);
                }
            }
        }