C
C#2w ago
eduardoA

✅ Unsafe code

Can somebody explain why this needs UNSAFE I am getting a json back from firebase firestore
public async Task<List<T>> ReadAllAsync<T>(string collectionName) {
var token = await firebaseAuth.User!.GetIdTokenAsync(forceRefresh: true);
_httpClient!.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);

var requestUrl = $"{_firebaseBaseUrl}/{collectionName}";
var response = await _httpClient.GetAsync(requestUrl);

var usersList = new List<T>();

if(response.IsSuccessStatusCode) {
var jsonResponse = await response.Content.ReadAsStringAsync();
var jsonDoc = JsonSerializer.Deserialize<JsonElement>(jsonResponse);

if(jsonDoc.TryGetProperty("documents", out var documents)) {
foreach(var document in documents.EnumerateArray()) {
var jsonString = document.GetRawText(); // Get the raw JSON string of the document
var mappedObject = JsonSerializer.Deserialize<T>(jsonString); // Deserialize directly to T

if(mappedObject != null) {
usersList.Add(mappedObject);
}
}
}
}
return usersList;
}
public async Task<List<T>> ReadAllAsync<T>(string collectionName) {
var token = await firebaseAuth.User!.GetIdTokenAsync(forceRefresh: true);
_httpClient!.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);

var requestUrl = $"{_firebaseBaseUrl}/{collectionName}";
var response = await _httpClient.GetAsync(requestUrl);

var usersList = new List<T>();

if(response.IsSuccessStatusCode) {
var jsonResponse = await response.Content.ReadAsStringAsync();
var jsonDoc = JsonSerializer.Deserialize<JsonElement>(jsonResponse);

if(jsonDoc.TryGetProperty("documents", out var documents)) {
foreach(var document in documents.EnumerateArray()) {
var jsonString = document.GetRawText(); // Get the raw JSON string of the document
var mappedObject = JsonSerializer.Deserialize<T>(jsonString); // Deserialize directly to T

if(mappedObject != null) {
usersList.Add(mappedObject);
}
}
}
}
return usersList;
}
No description
9 Replies
SleepWellPupper
Are you targeting WinRT? Could you share the csproj file? I am unfamiliar with WinRT but my cursory research suggests that you're targeting <TargetFramework>net8.0-windows10.0.26100.0</TargetFramework> or similar, and as this method is public and (I am speculating) in a public class, the cswinrt interop codegen requires unsafe code to create the glue for that list returning method to WinRT. I would think you have a couple of options: - not target winrt - add <AllowUnsafeBlocks>true</AllowUnsafeBlocks> to your project - change the return type, e.g. to IEnumerable<T> As a side note, adding AllowUnsafeBlocks to your csproj just enables use of unsafe blocks, so it does not impact you unless you actively use them.
Aaron
Aaron2w ago
what does your csproj look like
eduardoA
eduardoAOP2w ago
<PropertyGroup>
<TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>FireChat</RootNamespace>
<UseMaui>true</UseMaui>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<MauiStrictXamlCompilation>true</MauiStrictXamlCompilation>
<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

<MauiVersion>9.0.21</MauiVersion>
<PropertyGroup>
<TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>FireChat</RootNamespace>
<UseMaui>true</UseMaui>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<MauiStrictXamlCompilation>true</MauiStrictXamlCompilation>
<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

<MauiVersion>9.0.21</MauiVersion>
Aaron
Aaron2w ago
then yeah, it is that targetting of a specific windows version, it assumes youll be interacting with some WinRT component the generator for which currently emits unsafe code directly into your project
eduardoA
eduardoAOP2w ago
but is safe to use tthat code
Aaron
Aaron2w ago
yes, all it requires is you have unsafe code enabled in your project so the generator can use it you don't deal with unsafe at all
eduardoA
eduardoAOP2w ago
so if I will make a nugget package in the future this is safe to use>
Aaron
Aaron2w ago
yes
eduardoA
eduardoAOP2w ago
how do I edit the tag as answed red

Did you find this page helpful?