public void SetUp() { #if !NETSTANDARD1_1 && !NETSTANDARD1_3 //SerializerDebugging.TraceEnabled = true; //SerializerDebugging.DumpEnabled = true; if (SerializerDebugging.TraceEnabled) { Tracer.Emit.Listeners.Clear(); Tracer.Emit.Switch.Level = SourceLevels.All; Tracer.Emit.Listeners.Add(new ConsoleTraceListener()); } SerializerDebugging.DependentAssemblyManager = new TempFileDependentAssemblyManager(TestContext.CurrentContext.TestDirectory); SerializerDebugging.DeletePastTemporaries(); SerializerDebugging.OnTheFlyCodeGenerationEnabled = true; #if NETFX_35 SerializerDebugging.SetCodeCompiler(CodeDomCodeGeneration.Compile); #else SerializerDebugging.SetCodeCompiler(RoslyCodeGeneration.Compile); #endif // NETFX_35 SerializerDebugging.DumpDirectory = TestContext.CurrentContext.TestDirectory; SerializerDebugging.AddRuntimeAssembly(this.GetType().Assembly.Location); if (this.GetType().Assembly != typeof(NilImplicationTestTargetForValueTypeMemberDefault).Assembly) { SerializerDebugging.AddRuntimeAssembly(typeof(NilImplicationTestTargetForValueTypeMemberDefault).Assembly.Location); } #endif // !NETSTANDARD1_1 && !NETSTANDARD1_3 }
/// <summary> /// Packs enum value as its underlying value asynchronously. /// </summary> /// <param name="packer">The packer.</param> /// <param name="enumValue">The enum value to be packed.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> protected internal virtual Task PackUnderlyingValueToAsync(Packer packer, TEnum enumValue, CancellationToken cancellationToken) { #if DEBUG SerializerDebugging.EnsureNaiveAsyncAllowed(this); #endif // DEBUG return(Task.Run(() => this.PackUnderlyingValueTo(packer, enumValue), cancellationToken)); }
public void SetUp() { #if !NETSTANDARD1_1 && !NETSTANDARD1_3 && !NETSTANDARD1_6 //SerializerDebugging.TraceEnabled = true; //SerializerDebugging.DumpEnabled = true; if (SerializerDebugging.TraceEnabled) { Tracer.Emit.Listeners.Clear(); Tracer.Emit.Switch.Level = SourceLevels.All; #if NETSTANDARD2_0 Tracer.Emit.Listeners.Add(new TextWriterTraceListener(Console.Out)); #else // NETSTANDRD2_0 Tracer.Emit.Listeners.Add(new ConsoleTraceListener()); #endif // NETSTANDRD2_0 } SerializerDebugging.DependentAssemblyManager = new TempFileDependentAssemblyManager(TestContext.CurrentContext.TestDirectory); SerializerDebugging.DeletePastTemporaries(); SerializerDebugging.OnTheFlyCodeGenerationEnabled = true; #if NET35 SerializerDebugging.SetCodeCompiler(CodeDomCodeGeneration.Compile); #else SerializerDebugging.SetCodeCompiler(RoslynCodeGeneration.Compile); #endif // NET35 SerializerDebugging.DumpDirectory = TestContext.CurrentContext.TestDirectory; SerializerDebugging.AddRuntimeAssembly(typeof(ImmutableList).Assembly.Location); #endif // !NETSTANDARD1_1 && !NETSTANDARD1_3 && !NETSTANDARD1_6 }
public void TearDown() { #if !NETSTANDARD1_1 && !NETSTANDARD1_3 && !NETSTANDARD1_6 if (SerializerDebugging.DumpEnabled && this.CanDump) { #if !NETSTANDARD2_0 try { SerializerDebugging.Dump(); } catch (NotSupportedException ex) { Console.Error.WriteLine(ex); } finally { SerializationMethodGeneratorManager.Refresh(); } #else // !NETSTANDARD2_0 SerializationMethodGeneratorManager.Refresh(); #endif // !NETSTANDARD2_0 } SerializerDebugging.Reset(); SerializerDebugging.OnTheFlyCodeGenerationEnabled = false; #endif // !NETSTANDARD1_1 && !NETSTANDARD1_3 && !NETSTANDARD1_6 }
public void SetUp() { SerializerDebugging.DeletePastTemporaries(); //SerializerDebugging.TraceEnabled = true; //SerializerDebugging.DumpEnabled = true; if (SerializerDebugging.TraceEnabled) { Tracer.Emit.Listeners.Clear(); Tracer.Emit.Switch.Level = SourceLevels.All; Tracer.Emit.Listeners.Add(new ConsoleTraceListener()); } SerializerDebugging.OnTheFlyCodeDomEnabled = true; SerializerDebugging.AddRuntimeAssembly(typeof(ImmutableList).Assembly.Location); }
public void TearDown() { if (SerializerDebugging.DumpEnabled) { try { SerializerDebugging.Dump(); } finally { DefaultSerializationMethodGeneratorManager.Refresh(); } } SerializerDebugging.Reset(); SerializerDebugging.OnTheFlyCodeDomEnabled = false; }
public void SetUp() { SerializerDebugging.DeletePastTemporaries(); //SerializerDebugging.TraceEnabled = true; //SerializerDebugging.DumpEnabled = true; if (SerializerDebugging.TraceEnabled) { Tracer.Emit.Listeners.Clear(); Tracer.Emit.Switch.Level = SourceLevels.All; Tracer.Emit.Listeners.Add(new ConsoleTraceListener()); } SerializerDebugging.OnTheFlyCodeDomEnabled = true; SerializerDebugging.AddRuntimeAssembly(this.GetType().Assembly.Location); if (this.GetType().Assembly != typeof(NilImplicationTestTargetForValueTypeMemberDefault).Assembly) { SerializerDebugging.AddRuntimeAssembly(typeof(NilImplicationTestTargetForValueTypeMemberDefault).Assembly.Location); } }
public void TearDown() { #if !NETSTANDARD1_1 && !NETSTANDARD1_3 if (SerializerDebugging.DumpEnabled) { try { SerializerDebugging.Dump(); } finally { SerializationMethodGeneratorManager.Refresh(); } } SerializerDebugging.Reset(); SerializerDebugging.OnTheFlyCodeGenerationEnabled = false; #endif // !NETSTANDARD1_1 && !NETSTANDARD1_3 }
public void TearDown() { if (SerializerDebugging.DumpEnabled && this.CanDump) { try { SerializerDebugging.Dump(); } catch (NotSupportedException ex) { Console.Error.WriteLine(ex); } finally { SerializationMethodGeneratorManager.Refresh(); } } SerializerDebugging.Reset(); SerializerDebugging.OnTheFlyCodeDomEnabled = false; }
public static void Compile(string code, bool isDebug, out Assembly compiledAssembly, out IList <string> errors, out IList <string> warnings) { var assemblyName = "CodeGenerationAssembly" + Interlocked.Increment(ref _suffix); var assemblyPath = Path.Combine(SerializerDebugging.DumpDirectory ?? Path.GetTempPath(), assemblyName + ".dll"); using (var provider = CodeDomProvider.CreateProvider("C#")) { var parameters = new CompilerParameters { GenerateInMemory = false, OutputAssembly = assemblyPath }; foreach (var assembly in SerializerDebugging.CodeSerializerDependentAssemblies) { parameters.ReferencedAssemblies.Add(( string )assembly); } var result = provider.CompileAssemblyFromSource( parameters, code ); errors = BuildCompilationError(result.Errors.OfType <CompilerError>().Where(d => !d.IsWarning)); warnings = BuildCompilationError(result.Errors.OfType <CompilerError>().Where(d => d.IsWarning)); if (!errors.Any()) { SerializerDebugging.AddCompiledCodeAssembly(result.PathToAssembly); compiledAssembly = result.CompiledAssembly; } else { compiledAssembly = null; } } }
public void TestPerformance() { SerializerDebugging.OnTheFlyCodeDomEnabled = true; SerializerDebugging.AddRuntimeAssembly(Assembly.GetExecutingAssembly().Location); Console.WriteLine( "Flavor\tMethod\tEnumMethod\tMin(usec,gen)\tMax(usec,gen)\tAvg(usec,gen)\tStdDiv(gen)\tMin(usec,pack)\tMax(usec,pack)\tAvg(usec,pack)\tStdDiv(usec,pack)\tMin(usec,unpack)\tMax(usec,unpack)\tAvg(usec,unpack)\tStdDiv(usec,unpack)" ); foreach (var flavor in new[] { EmitterFlavor.FieldBased, EmitterFlavor.ContextBased, EmitterFlavor.ExpressionBased, EmitterFlavor.CodeDomBased, EmitterFlavor.ReflectionBased }) { foreach (var serializationMethod in new[] { SerializationMethod.Array, SerializationMethod.Map }) { foreach (var enumSerializationMethod in new[] { EnumSerializationMethod.ByName, EnumSerializationMethod.ByUnderlyingValue }) { var result = Test(flavor, serializationMethod, enumSerializationMethod); Console.WriteLine( "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}", flavor, serializationMethod, enumSerializationMethod, result[0].MinElapsedTicks / 10.0, result[0].MaxElapsedTicks / 10.0, result[0].AverageElapsedTicks / 10.0, result[0].StandardDeviation / 10.0, result[1].MinElapsedTicks / 10.0, result[1].MaxElapsedTicks / 10.0, result[1].AverageElapsedTicks / 10.0, result[1].StandardDeviation / 10.0, result[2].MinElapsedTicks / 10.0, result[2].MaxElapsedTicks / 10.0, result[2].AverageElapsedTicks / 10.0, result[2].StandardDeviation / 10.0 ); } } } }
internal static MessagePackSerializer <T> CreateInternal <T>(SerializationContext context, PolymorphismSchema schema) { #if DEBUG Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null); #endif // DEBUG #if DEBUG && !AOT && !SILVERLIGHT && !NETSTANDARD1_1 SerializerDebugging.TraceEmitEvent( "SerializationContext::CreateInternal<{0}>(@{1}, {2})", typeof(T), context.GetHashCode(), schema == null ? "null" : schema.DebugString ); #endif // DEBUG && !AOT && !SILVERLIGHT && !NETSTANDARD1_1 Type concreteType = null; CollectionTraits collectionTraits = #if AOT typeof(T).GetCollectionTraits(CollectionTraitOptions.None, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes); #else typeof(T).GetCollectionTraits(CollectionTraitOptions.Full, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes); #endif // AOT if (typeof(T).GetIsAbstract() || typeof(T).GetIsInterface()) { // Abstract collection types will be handled correctly. if (collectionTraits.CollectionType != CollectionKind.NotCollection) { concreteType = context.DefaultCollectionTypes.GetConcreteType(typeof(T)); } if (concreteType == null) { // return null for polymoirphic provider. return(null); } ValidateType(concreteType); } else { ValidateType(typeof(T)); } #if FEATURE_EMIT ISerializerBuilder builder; switch (context.SerializerOptions.EmitterFlavor) { #if !NETSTANDARD1_3 case EmitterFlavor.CodeDomBased: { #if DEBUG if (!SerializerDebugging.OnTheFlyCodeGenerationEnabled) { throw new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "Flavor '{0:G}'({0:D}) is not supported for serializer instance creation.", context.SerializerOptions.EmitterFlavor ) ); } builder = new CodeDomSerializerBuilder(typeof(T), collectionTraits); break; #else // DEBUG throw new NotSupportedException(); #endif // DEBUG } #endif // !NETSTANDARD1_3 case EmitterFlavor.FieldBased: { builder = new AssemblyBuilderSerializerBuilder(typeof(T), collectionTraits); break; } default: // EmitterFlavor.ReflectionBased { #endif // FEATURE_EMIT return (GenericSerializer.TryCreateAbstractCollectionSerializer(context, typeof(T), concreteType, schema) as MessagePackSerializer <T> ?? CreateReflectionInternal <T>(context, concreteType ?? typeof(T), schema)); #if FEATURE_EMIT } } return((MessagePackSerializer <T>)builder.BuildSerializerInstance(context, concreteType, schema == null ? null : schema.FilterSelf())); #endif // FEATURE_EMIT }
public static void Compile(string code, bool isDebug, out Assembly compiledAssembly, out IList <string> errors, out IList <string> warnings) { var assemblyName = "CodeGenerationAssembly" + Interlocked.Increment(ref _suffix); var metadataList = SerializerDebugging.CodeSerializerDependentAssemblies.Select( a => a is string ?AssemblyMetadata.CreateFromFile(a as string) : AssemblyMetadata.CreateFromImage(a as byte[]) ).ToArray(); try { var compilation = CSharpCompilation.Create( assemblyName, new[] { CSharpSyntaxTree.ParseText(code) }, metadataList.Select(m => m.GetReference()), new CSharpCompilationOptions( OutputKind.DynamicallyLinkedLibrary, optimizationLevel: isDebug?OptimizationLevel.Debug: OptimizationLevel.Release, // Suppress CS0436 because gen/*.cs will conflict with testing serializers. specificDiagnosticOptions: new[] { new KeyValuePair <string, ReportDiagnostic>("CS0436", ReportDiagnostic.Suppress) } ) ); var emitOptions = new EmitOptions(runtimeMetadataVersion: "v4.0.30319"); EmitResult result; if (SerializerDebugging.DumpEnabled) { var assemblyPath = Path.Combine(SerializerDebugging.DumpDirectory, assemblyName + ".dll"); using (var fileStream = File.OpenWrite(assemblyPath)) { result = compilation.Emit(fileStream, options: emitOptions); fileStream.Flush(); } if (result.Success) { compiledAssembly = Assembly.LoadFrom(assemblyPath); SerializerDebugging.AddCompiledCodeAssembly(assemblyPath); } else { compiledAssembly = null; } } else { using (var buffer = new MemoryStream()) { result = compilation.Emit(buffer, options: emitOptions); if (result.Success) { var image = buffer.ToArray(); compiledAssembly = Assembly.Load(image); SerializerDebugging.AddCompiledCodeAssembly(assemblyName, image); } else { compiledAssembly = null; } } } errors = BuildCompilationError(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error)); warnings = BuildCompilationError(result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning)); } finally { foreach (var metadata in metadataList) { metadata.Dispose(); } } }
internal static MessagePackSerializer <T> CreateInternal <T>(SerializationContext context, PolymorphismSchema schema) { #if !XAMIOS && !XAMDROID && !UNITY Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null); #endif // !XAMIOS && !XAMDROID && !UNITY #if DEBUG && !UNITY && !XAMDROID && !XAMIOS SerializerDebugging.TraceEvent( "SerializationContext::CreateInternal<{0}>(@{1}, {2})", typeof(T), context.GetHashCode(), schema == null ? "null" : schema.DebugString ); #endif // DEBUG && !UNITY && !XAMDROID && !XAMIOS Type concreteType = null; if (typeof(T).GetIsAbstract() || typeof(T).GetIsInterface()) { // Abstract collection types will be handled correctly. if (typeof(T).GetCollectionTraits().CollectionType != CollectionKind.NotCollection) { concreteType = context.DefaultCollectionTypes.GetConcreteType(typeof(T)); } if (concreteType == null) { // return null for polymoirphic provider. return(null); } ValidateType(concreteType); } else { ValidateType(typeof(T)); } #if !XAMIOS && !XAMDROID && !UNITY ISerializerBuilder <T> builder; #endif // !XAMIOS && !XAMDROID && !UNITY #if NETFX_CORE || WINDOWS_PHONE builder = new ExpressionTreeSerializerBuilder <T>(); #elif SILVERLIGHT builder = new DynamicMethodSerializerBuilder <T>(); #else #if !XAMIOS && !XAMDROID && !UNITY switch (context.EmitterFlavor) { case EmitterFlavor.ReflectionBased: { #endif // !XAMIOS && !XAMDROID && !UNITY return (DefaultSerializers.GenericSerializer.TryCreateAbstractCollectionSerializer(context, typeof(T), concreteType, schema) as MessagePackSerializer <T> ?? CreateReflectionInternal <T>(context, concreteType ?? typeof(T), schema)); #if !XAMIOS && !XAMDROID && !UNITY } #if !WINDOWS_PHONE && !NETFX_35 case EmitterFlavor.ExpressionBased: { builder = new ExpressionTreeSerializerBuilder <T>(); break; } #endif // if !WINDOWS_PHONE && !NETFX_35 case EmitterFlavor.FieldBased: { builder = new AssemblyBuilderSerializerBuilder <T>(); break; } case EmitterFlavor.ContextBased: { builder = new DynamicMethodSerializerBuilder <T>(); break; } default: { #if !NETFX_35 if (!SerializerDebugging.OnTheFlyCodeDomEnabled) { throw new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "Flavor '{0:G}'({0:D}) is not supported for serializer instance creation.", context.EmitterFlavor ) ); } #endif // if !NETFX_35 builder = new CodeDomSerializerBuilder <T>(); break; } } #endif // !XAMIOS && !XAMDROID && !UNITY #endif // NETFX_CORE else #if !XAMIOS && !XAMDROID && !UNITY return(builder.BuildSerializerInstance(context, concreteType, schema == null ? null : schema.FilterSelf())); #endif // !XAMIOS && !XAMDROID && !UNITY }