Skip to content

Commit d4599b1

Browse files
authored
Fix trimming warnings in Microsoft.Data.Sqlite (#30728)
1 parent cf290d3 commit d4599b1

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Diagnostics;
99
using System.Diagnostics.CodeAnalysis;
1010
using System.Reflection;
11+
using System.Runtime.CompilerServices;
1112
using Microsoft.Data.Sqlite.Properties;
1213
using SQLitePCL;
1314
using static SQLitePCL.raw;
@@ -52,36 +53,40 @@ static SqliteConnection()
5253
?.GetRuntimeMethod("Init", Type.EmptyTypes)
5354
?.Invoke(null, null);
5455

56+
var appDataType = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
57+
?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET");
58+
59+
var storageFolderType = Type.GetType("Windows.Storage.StorageFolder, Windows, ContentType=WindowsRuntime")
60+
?? Type.GetType("Windows.Storage.StorageFolder, Microsoft.Windows.SDK.NET");
61+
62+
object? currentAppData = null;
5563
try
5664
{
57-
var currentAppData = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
58-
?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET")
59-
?.GetRuntimeProperty("Current")?.GetValue(null);
65+
currentAppData = appDataType?.GetRuntimeProperty("Current")?.GetValue(null);
66+
}
67+
catch (TargetInvocationException)
68+
{
69+
// Ignore "The process has no package identity."
70+
}
6071

61-
var localFolder = currentAppData?.GetType()
62-
.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData);
63-
var localFolderPath = (string?)localFolder?.GetType()
64-
.GetRuntimeProperty("Path")?.GetValue(localFolder);
72+
if (currentAppData != null)
73+
{
74+
var localFolder = appDataType?.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData);
75+
var localFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(localFolder);
6576
if (localFolderPath != null)
6677
{
6778
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_DATA_DIRECTORY_TYPE, localFolderPath);
6879
Debug.Assert(rc == SQLITE_OK);
6980
}
7081

71-
var tempFolder = currentAppData?.GetType()
72-
.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData);
73-
var tempFolderPath = (string?)tempFolder?.GetType()
74-
.GetRuntimeProperty("Path")?.GetValue(tempFolder);
82+
var tempFolder = appDataType?.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData);
83+
var tempFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(tempFolder);
7584
if (tempFolderPath != null)
7685
{
7786
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_TEMP_DIRECTORY_TYPE, tempFolderPath);
7887
Debug.Assert(rc == SQLITE_OK);
7988
}
8089
}
81-
catch
82-
{
83-
// Ignore "The process has no package identity."
84-
}
8590
}
8691

8792
/// <summary>

src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Data;
88
using System.Data.Common;
99
using System.Diagnostics;
10+
using System.Diagnostics.CodeAnalysis;
1011
using System.IO;
1112
using System.Text;
1213
using System.Threading;
@@ -311,6 +312,9 @@ public override string GetDataTypeName(int ordinal)
311312
/// </summary>
312313
/// <param name="ordinal">The zero-based column ordinal.</param>
313314
/// <returns>The data type of the column.</returns>
315+
#if NET6_0_OR_GREATER
316+
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
317+
#endif
314318
public override Type GetFieldType(int ordinal)
315319
=> _closed
316320
? throw new InvalidOperationException(Resources.DataReaderClosed(nameof(GetFieldType)))

src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
@@ -191,6 +192,9 @@ public virtual string GetDataTypeName(int ordinal)
191192
}
192193
}
193194

195+
#if NET6_0_OR_GREATER
196+
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
197+
#endif
194198
public virtual Type GetFieldType(int ordinal)
195199
{
196200
var sqliteType = GetSqliteType(ordinal);
@@ -207,6 +211,9 @@ public virtual Type GetFieldType(int ordinal)
207211
return GetFieldTypeFromSqliteType(sqliteType);
208212
}
209213

214+
#if NET6_0_OR_GREATER
215+
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
216+
#endif
210217
internal static Type GetFieldTypeFromSqliteType(int sqliteType)
211218
{
212219
switch (sqliteType)
@@ -228,6 +235,9 @@ internal static Type GetFieldTypeFromSqliteType(int sqliteType)
228235
}
229236
}
230237

238+
#if NET6_0_OR_GREATER
239+
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
240+
#endif
231241
public static Type GetFieldType(string type)
232242
{
233243
switch (type)

0 commit comments

Comments
 (0)