Exemple #1
0
        public void TestReflectPluginNonPublicSerializableType()
        {
            var pluginBuilder = new PluginBuilder("Kittyfisto", "Test", "TestReflectPluginNonPublicSerializableType");
            var type          = pluginBuilder.DefineType("SomeSerializableType", TypeAttributes.Class | TypeAttributes.NotPublic);

            type.AddInterfaceImplementation(typeof(ISerializableType));
            var attribute = pluginBuilder.BuildCustomAttribute(new DataContractAttribute());

            type.SetCustomAttribute(attribute);
            var ctorBuilder = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[0]);
            var gen         = ctorBuilder.GetILGenerator();

            gen.Emit(OpCodes.Ret);

            var serialize = type.DefineMethod(nameof(ISerializableType.Serialize), MethodAttributes.Public | MethodAttributes.Virtual,
                                              CallingConventions.HasThis,
                                              typeof(void),
                                              new [] { typeof(IWriter) });

            serialize.GetILGenerator().Emit(OpCodes.Ret);

            var deserialize = type.DefineMethod(nameof(ISerializableType.Deserialize), MethodAttributes.Public | MethodAttributes.Virtual,
                                                CallingConventions.HasThis,
                                                typeof(void),
                                                new [] { typeof(IReader) });

            deserialize.GetILGenerator().Emit(OpCodes.Ret);

            type.CreateType();
            pluginBuilder.Save();

            var scanner = new PluginAssemblyLoader();

            var appender = Appender.CaptureEvents("Tailviewer.Archiver.Plugins.PluginAssemblyLoader", Level.Error);

            scanner.ReflectPlugin(pluginBuilder.FileName);
            appender.Events.Should().HaveCount(1, "because the serializable type's parameterless constructor is not publicly visible and this should have provoked an error");
            var error = appender.Events.First();

            error.RenderedMessage.Should().Contain(type.FullName);
            error.RenderedMessage.Should().Contain("must be set to public!");
        }