Skip to content

Commit

Permalink
update libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren committed May 16, 2024
1 parent 7993a05 commit daac90d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
24 changes: 20 additions & 4 deletions docs/core/whats-new/dotnet-9/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
title: What's new in .NET libraries for .NET 9
description: Learn about the new .NET libraries features introduced in .NET 9.
titleSuffix: ""
ms.date: 04/11/2024
ms.date: 05/21/2024
ms.topic: whats-new
---
# What's new in .NET libraries for .NET 9

This article describes new features in the .NET libraries for .NET 9. It's been updated for .NET 9 Preview 3.
This article describes new features in the .NET libraries for .NET 9. It's been updated for .NET 9 Preview 4.

## Serialization

Expand Down Expand Up @@ -84,20 +84,36 @@ KMAC is available on Linux with OpenSSL 3.0 or later, and on Windows 11 Build 26

## Reflection

### Persisted assemblies

In .NET Core versions and .NET 5-8, support for building an assembly and emitting reflection metadata for dynamically created types was limited to a runnable <xref:System.Reflection.Emit.AssemblyBuilder>. The lack of support for *saving* an assembly was often a blocker for customers migrating from .NET Framework to .NET. .NET 9 adds a new type, `PersistedAssemblyBuilder` <!--<xref:System.Reflection.Emit.AssemblyBuilder.PersistedAssemblyBuilder>-->, that you can use to save an emitted assembly.

To create a `PersistedAssemblyBuilder` instance, call its constructor and pass the assembly name, the core assembly, `System.Private.CoreLib`, to reference base runtime types, and optional custom attributes. After you emit all members to the assembly, call the `PersistedAssemblyBuilder.Save(string assemblyFileName)` <!--<xref:System.Reflection.Emit.AssemblyBuilder.PersistedAssemblyBuilder.Save(System.String)>--> method to create an assembly with default settings. If you want to set the entry point or other options, you can call `PersistedAssemblyBuilder.GenerateMetadata(System.Reflection.Metadata.BlobBuilder,System.Reflection.Metadata.BlobBuilder)` <!--<xref:System.Reflection.Emit.AssemblyBuilder.PersistedAssemblyBuilder.GenerateMetadata(System.Reflection.Metadata.BlobBuilder,System.Reflection.Metadata.BlobBuilder)>--> and use the metadata it returns to save the assembly. The following code shows an example of creating a persisted assembly and setting the entry point.

:::code language="csharp" source="../snippets/dotnet-9/csharp/Reflection.cs" id="SaveAssembly":::

## New TimeSpan.From\* overloads
The new <xref:System.Reflection.Emit.PersistedAssemblyBuilder> class includes PDB support. You can emit symbol info and use it to debug a generated assembly. The API has a similar shape to the .NET Framework implementation. For more information, see [Emit symbols and generate PDB](../../../fundamentals/runtime-libraries/system-reflection-emit-persistedassemblybuilder.md#emit-symbols-and-generate-pdb).

## Date and time

### New TimeSpan.From\* overloads

The <xref:System.TimeSpan> class offers several `From*` methods that let you create a `TimeSpan` object using a `double`. However, since `double` is a binary-based floating-point format, [inherent imprecision can lead to errors](https://github.com/dotnet/runtime/issues/93890). For instance, `TimeSpan.FromSeconds(101.832)` might not precisely represent `101 seconds, 832 milliseconds`, but rather approximately `101 seconds, 831.9999999999936335370875895023345947265625 milliseconds`. This discrepancy has caused frequent confusion, and it's also not the most efficient way to represent such data. To address this, .NET 9 adds new overloads that let you create `TimeSpan` objects from integers. There are new overloads from `FromDays`, `FromHours`, `FromMinutes`, `FromSeconds`, `FromMilliseconds`, and `FromMicroseconds`.

The following code shows an example of calling the `double` and one of the new integer overloads.

:::code language="csharp" source="../snippets/dotnet-9/csharp/TimeSpan.cs" id="TimeSpan.From":::

## `ActivatorUtilities.CreateInstance` constructor
## Dependency injection

### `ActivatorUtilities.CreateInstance` constructor

The constructor resolution for <xref:Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance%2A?displayProperty=nameWithType> has changed in .NET 9. Previously, a constructor that was explicitly marked using the <xref:Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute> attribute might not be called, depending on the ordering of constructors and the number of constructor parameters. The logic has changed in .NET 9 such that a constructor that has the attribute is always called.

## Diagnostics

### New Activity.AddLink method

Previously, you could only link a tracing <xref:System.Diagnostics.Activity> to other tracing contexts when you [created the `Activity`](xref:System.Diagnostics.ActivitySource.CreateActivity(System.String,System.Diagnostics.ActivityKind,System.Diagnostics.ActivityContext,System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{System.String,System.Object}},System.Collections.Generic.IEnumerable{System.Diagnostics.ActivityLink},System.Diagnostics.ActivityIdFormat)?displayProperty=nameWithType). New in .NET 9, the `Activity.AddLink(System.Diagnostics.ActivityLink)` <!--<xref:System.Diagnostics.Activity.AddLink(System.Diagnostics.ActivityLink)>--> API lets you link an `Activity` object to other tracing contexts after it's created. This change aligns with the [OpenTelemetry specifications](https://github.com/open-telemetry/opentelemetry-specification/blob/6360b49d20ae451b28f7ba0be168ed9a799ac9e1/specification/trace/api.md?plain=1#L804) as well.

:::code language="csharp" source="../snippets/dotnet-9/csharp/Diagnostics.cs" id="AddLink":::
2 changes: 1 addition & 1 deletion docs/core/whats-new/dotnet-9/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ms.topic: whats-new
---
# What's new in the .NET 9 runtime

This article describes the performance enhancements in the .NET runtime for .NET 9. It's been updated for .NET 9 Preview 3.
This article describes the performance enhancements in the .NET runtime for .NET 9. It's been updated for .NET 9 Preview 4.

## Loop optimizations

Expand Down
2 changes: 1 addition & 1 deletion docs/core/whats-new/dotnet-9/sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ms.topic: whats-new
---
# What's new in the SDK for .NET 9

This article describes new features in the .NET SDK for .NET 9. It's been updated for .NET 9 Preview 3.
This article describes new features in the .NET SDK for .NET 9. It's been updated for .NET 9 Preview 4.

## Unit testing

Expand Down
16 changes: 16 additions & 0 deletions docs/core/whats-new/snippets/dotnet-9/csharp/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

using System.Diagnostics;

internal class Diagnostics
{
public static void RunIt()
{
// <AddLink>
ActivityContext activityContext = new(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None);
ActivityLink activityLink = new(activityContext);

Activity activity = new("LinkTest");
activity.AddLink(activityLink);
// </AddLink>
}
}
6 changes: 3 additions & 3 deletions docs/core/whats-new/snippets/dotnet-9/csharp/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ out BlobBuilder fieldData
public static void UseAssembly(string assemblyPath)
{
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Type type = assembly.GetType("MyType");
MethodInfo method = type.GetMethod("SumMethod");
Console.WriteLine(method.Invoke(null, [5, 10]));
Type? type = assembly.GetType("MyType");
MethodInfo? method = type?.GetMethod("SumMethod");
Console.WriteLine(method?.Invoke(null, [5, 10]));
}
// </SaveAssembly>
}
6 changes: 3 additions & 3 deletions docs/machine-learning/whats-new/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ The following examples show how to use the `Tiktoken` text tokenizer.

The following examples show how to use the `Llama` text tokenizer.

:::code language="csharp" source="./snippets/csharp/LlamaTokenizer.cs" id="Llama":::
:::code language="csharp" source="./snippets/csharp/Llama.cs" id="Llama":::

The following examples show how to use the `CodeGen` tokenizer.

:::code language="csharp" source="./snippets/csharp/CodeGenTokenizer.cs" id="CodeGen":::
:::code language="csharp" source="./snippets/csharp/CodeGen.cs" id="CodeGen":::

The following example demonstrates how to use the tokenizer with `Span<char>` and how to disable normalization or pretokenization on the encoding calls.

:::code language="csharp" source="./snippets/csharp/LlamaTokenizer.cs" id="Span":::
:::code language="csharp" source="./snippets/csharp/Llama.cs" id="Span":::

## Model Builder (Visual Studio extension)

Expand Down

0 comments on commit daac90d

Please sign in to comment.