Skip to content

Commit

Permalink
add net6.0 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Nov 5, 2022
1 parent 38af979 commit 3fd8406
Show file tree
Hide file tree
Showing 34 changed files with 126 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/SampleApp.Mef1/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected override void OnStartup(StartupEventArgs e)
Resources.MergedDictionaries.Insert(0, WpfStyles.GetDefaultStyles().RegisterDefaultWindowStyle());
Resources.MergedDictionaries.Add(DataTemplateManager.CreateDynamicDataTemplates(_exportProvider));

MainWindow = _compositionContainer.GetExportedValue<MainWindow>()!;
MainWindow = _compositionContainer.GetExportedValue<MainWindow>();
MainWindow.Show();
}

Expand All @@ -86,4 +86,4 @@ public void Dispose()
_compositionCatalog.Dispose();
_compositionContainer.Dispose();
}
}
}
6 changes: 4 additions & 2 deletions src/SampleApp.Mef1/Map/MapSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ private void OnLoaded()

var uri = _owner.GetImageUri(_mapTile);

using var webClient = new WebClient();

var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = WebHelper.Download(uri);
bitmap.StreamSource = new MemoryStream(webClient.DownloadData(uri));
bitmap.EndInit();
bitmap.Freeze();

Expand All @@ -176,4 +178,4 @@ private void OnLoaded()
}
}
}
}
}
7 changes: 5 additions & 2 deletions src/SampleApp/Map/MapSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
Expand Down Expand Up @@ -156,9 +157,11 @@ private void OnLoaded()

var uri = _owner.GetImageUri(_mapTile);

using var webClient = new HttpClient();

var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = WebHelper.Download(uri);
bitmap.StreamSource = new MemoryStream(webClient.GetByteArrayAsync(uri).Result);
bitmap.EndInit();
bitmap.Freeze();

Expand All @@ -175,4 +178,4 @@ private void OnLoaded()
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/TargetFrameworks.Desktop.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net462;netcoreapp3.1;net5.0-windows7.0</TargetFrameworks>
<TargetFrameworks>net462;netcoreapp3.1;net5.0-windows7.0;net6.0-windows7.0</TargetFrameworks>
<WarningsAsErrors Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">nullable</WarningsAsErrors>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/TargetFrameworks.Portable.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net5.0;net6.0</TargetFrameworks>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions src/TomsToolbox.Composition.Mef2/ExportProviderAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ IEnumerable<IExport<object>> IExportProvider.GetExports(Type contractType, strin
{
var exportMethod = _getExportsAsObjectMethod.MakeGenericMethod(contractType);

return (IEnumerable<IExport<object>>)exportMethod.Invoke(this, new object?[] { contractName });
return (IEnumerable<IExport<object>>)exportMethod.Invoke(this, new object?[] { contractName })!;
}

IEnumerable<object> IExportProvider.GetExportedValues(Type contractType, string? contractName)
Expand All @@ -73,4 +73,4 @@ private IEnumerable<IExport<object>> GetExportsAsObject<T>(string? contractName)
.GetExports<ExportFactory<T, IDictionary<string, object?>>>(contractName)
.Select(item => new ExportAdapter<object>(() => item.CreateExport().Value ?? throw new InvalidOperationException("Export did not return a value."), new MetadataAdapter(item.Metadata)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private IEnumerable<IExport<object>> GetExports(Type contractType, string? contr
{
var exportMethod = _getExportsAsObjectMethod.MakeGenericMethod(contractType);

return (IEnumerable<IExport<object>>)exportMethod.Invoke(this, new object?[] { contractName });
return (IEnumerable<IExport<object>>)exportMethod.Invoke(this, new object?[] { contractName })!;
}

private IEnumerable<IExport<object>> GetExportsAsObject<T>(string? contractName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
/// </summary>
public static class ExtensionMethods
{
private static readonly MethodInfo _addMetaDataExportMethod = typeof(ExtensionMethods).GetMethod(nameof(AddMetadataExportT), BindingFlags.NonPublic | BindingFlags.Static);
private static readonly MethodInfo _getMetaDataMethod = typeof(ExtensionMethods).GetMethod(nameof(GetMetadataT), BindingFlags.NonPublic | BindingFlags.Static);
private static readonly MethodInfo _addMetaDataExportMethod = typeof(ExtensionMethods).GetMethod(nameof(AddMetadataExportT), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo _getMetaDataMethod = typeof(ExtensionMethods).GetMethod(nameof(GetMetadataT), BindingFlags.NonPublic | BindingFlags.Static)!;

private static readonly Type _exportType = typeof(IExport<>);

Expand Down Expand Up @@ -75,9 +75,9 @@ public static IServiceCollection BindExports(this IServiceCollection serviceColl
if (contractType != null)
{
if (exportInfo.IsShared)
serviceCollection.AddSingleton(contractType, sp => sp.GetService(type));
serviceCollection.AddSingleton(contractType, sp => sp.GetService(type)!);
else
serviceCollection.AddTransient(contractType, sp => sp.GetService(type));
serviceCollection.AddTransient(contractType, sp => sp.GetService(type)!);
}

serviceCollection.AddMetadataExport(contractType ?? type, type, new MetadataAdapter(export.Metadata));
Expand Down Expand Up @@ -137,7 +137,7 @@ public static IServiceCollection BindMetadata(this IServiceCollection serviceCol

private static void AddMetadataExportT<T>(IServiceCollection serviceCollection, Type implementationType, IMetadata metadata) where T : class
{
serviceCollection.AddTransient<IExport<T>>(sp => new ExportAdapter<T>(() => (T)sp.GetService(implementationType), metadata));
serviceCollection.AddTransient<IExport<T>>(sp => new ExportAdapter<T>(() => (T?)sp.GetService(implementationType), metadata));
}

private static IServiceCollection AddMetadataExport(this IServiceCollection serviceCollection, Type serviceType, Type implementationType, IMetadata metadata)
Expand Down
8 changes: 4 additions & 4 deletions src/TomsToolbox.Composition/ExportAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
public class ExportAdapter<T> : IExport<T>
where T : class
{
private readonly Func<T> _valueFactory;
private readonly Func<T?> _valueFactory;

private readonly IMetadata? _metadata;

/// <summary>Initializes a new instance of the <see cref="ExportAdapter{T}"/> class.</summary>
/// <param name="valueFactory">The value factory.</param>
/// <param name="metadata">The metadata.</param>
public ExportAdapter(Func<T> valueFactory, IMetadata? metadata)
public ExportAdapter(Func<T?> valueFactory, IMetadata? metadata)
{
_valueFactory = valueFactory;
_metadata = metadata;
}

T IExport<T, IMetadata>.Value => _valueFactory();
T? IExport<T, IMetadata>.Value => _valueFactory();

IMetadata? IExport<T, IMetadata>.Metadata => _metadata;
}
}
6 changes: 3 additions & 3 deletions src/TomsToolbox.Composition/ExportInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void EvaluatePartCreationPolicy(Type type)

if (partCreationPolicyAttribute != null)
{
var value = partCreationPolicyAttribute.ConstructorArguments.Select(arg => (int)arg.Value).FirstOrDefault();
var value = partCreationPolicyAttribute.ConstructorArguments.Select(arg => (int?)arg.Value).FirstOrDefault();
IsShared = value != 2;
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ private static void GetNamedArguments(CustomAttributeData exportAttribute, IDict

foreach (var namedArgument in namedArguments.OrderBy(item => item.MemberName))
{
metadata[namedArgument.MemberName] = ConvertValue(namedArgument.TypedValue)!;
metadata[namedArgument.MemberName] = ConvertValue(namedArgument.TypedValue);
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ private static void GetNamedArguments(CustomAttributeData exportAttribute, IDict

if (argumentType.IsEnum)
{
return Enum.Parse(argumentType, value.ToString());
return Enum.Parse(argumentType, value.ToString()!);
}

throw new InvalidOperationException($"Argument type conversion from {valueType} to {argumentType} is not supported.");
Expand Down
4 changes: 2 additions & 2 deletions src/TomsToolbox.Composition/IExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IExport<out T, out TMetadata>
/// <summary>
/// Gets the value.
/// </summary>
T Value { get; }
T? Value { get; }

/// <summary>
/// Gets the metadata.
Expand All @@ -27,4 +27,4 @@ public interface IExport<out T, out TMetadata>
public interface IExport<out T> : IExport<T, IMetadata>
where T : class
{
}
}
7 changes: 4 additions & 3 deletions src/TomsToolbox.Composition/IExportProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public static IEnumerable<IExport<object, TMetadata>> GetExports<TMetadata>(this

}

private class ExportAdapter<TObject, TMetadata> : IExport<TObject, TMetadata> where TObject : class
private class ExportAdapter<TObject, TMetadata> : IExport<TObject, TMetadata>
where TObject : class
where TMetadata : class
{
private readonly IExport<TObject> _source;
Expand All @@ -153,8 +154,8 @@ public ExportAdapter(IExport<TObject> source, Func<IMetadata?, TMetadata?> metad
Metadata = metadataFactory(source.Metadata);
}

public TObject Value => _source.Value;
public TObject? Value => _source.Value;

public TMetadata? Metadata { get; }
}
}
}
2 changes: 1 addition & 1 deletion src/TomsToolbox.Composition/MetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void ReadType(TypeInfo typeInfo, IList<ExportInfo> result)

var anyExportAttributeType = exportAttributes.First();

var isMef1 = anyExportAttributeType.ExportAttributeType.Namespace == "System.ComponentModel.Composition";
var isMef1 = anyExportAttributeType.ExportAttributeType?.Namespace == "System.ComponentModel.Composition";

result.Add(new ExportInfo(type, isMef1, exportAttributes.Select(item => item.Attribute)));
}
Expand Down
3 changes: 2 additions & 1 deletion src/TomsToolbox.Desktop/LegacyV2RuntimeActivationPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class LegacyV2RuntimeActivationPolicy
/// <para/>
/// Useful e.g. for unit tests where you have no access to the app.config of the test runner process.
/// </summary>
[Obsolete("LegacyV2RuntimeActivationPolicy is obsolete")]
public static void Activate()
{
var clrRuntimeInfo = (ICLRRuntimeInfo?)RuntimeEnvironment.GetRuntimeInterfaceAsObject(Guid.Empty, typeof(ICLRRuntimeInfo).GUID);
Expand All @@ -41,4 +42,4 @@ private interface ICLRRuntimeInfo
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void BindAsLegacyV2Runtime();
}
}
}
4 changes: 3 additions & 1 deletion src/TomsToolbox.Desktop/WebHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class WebHelper
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns>The request.</returns>
[Obsolete("WebRequest is obsolete")]
public static WebRequest CreateHttpWebRequest(Uri uri)
{
var webRequest = WebRequest.Create(uri);
Expand All @@ -29,6 +30,7 @@ public static WebRequest CreateHttpWebRequest(Uri uri)
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns>A stream containing the downloaded data.</returns>
[Obsolete("WebRequest is obsolete")]
public static MemoryStream Download(Uri uri)
{
var webRequest = CreateHttpWebRequest(uri);
Expand All @@ -45,4 +47,4 @@ public static MemoryStream Download(Uri uri)
localStream.Position = 0;
return localStream;
}
}
}
13 changes: 8 additions & 5 deletions src/TomsToolbox.Essentials/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public static class AssemblyExtensions
/// <returns>The directory in which the given assembly is stored.</returns>
public static DirectoryInfo GetAssemblyDirectory(this Assembly assembly)
{
var codeBase = assembly.CodeBase;
var location = assembly.Location;

var assemblyLocation = Path.GetDirectoryName(new Uri(codeBase).LocalPath)
?? throw new InvalidOperationException("Can't evaluate assembly code base: " + codeBase);
var assemblyLocation = Path.GetDirectoryName(location)
?? throw new InvalidOperationException("Can't evaluate assembly code base: " + location);

return new DirectoryInfo(assemblyLocation);
}
Expand All @@ -34,6 +34,8 @@ public static DirectoryInfo GetAssemblyDirectory(this Assembly assembly)
public static DirectoryInfo GetAssemblyDirectory(this AssemblyName assemblyName)
{
var codeBase = assemblyName.CodeBase;
if (codeBase == null)
throw new InvalidOperationException("Can't evaluate assembly code base: " + assemblyName);

var assemblyLocation = Path.GetDirectoryName(new Uri(codeBase).LocalPath)
?? throw new InvalidOperationException("Can't evaluate assembly code base: " + codeBase);
Expand All @@ -51,7 +53,8 @@ public static DirectoryInfo GetAssemblyDirectory(this AssemblyName assemblyName)
/// </remarks>
public static Uri GeneratePackUri(this Assembly assembly)
{
var name = new AssemblyName(assembly.FullName).Name;
var assemblyFullName = assembly.FullName ?? throw new ArgumentException("Assembly does not have a full name", nameof(assembly));
var name = new AssemblyName(assemblyFullName).Name;

return new Uri(string.Format(CultureInfo.InvariantCulture, "pack://application:,,,/{0};component/", name), UriKind.Absolute);
}
Expand Down Expand Up @@ -87,4 +90,4 @@ public static Uri GeneratePackUri(this Assembly assembly, Uri relativeUri)
{
return new Uri(assembly.GeneratePackUri(), relativeUri);
}
}
}
3 changes: 2 additions & 1 deletion src/TomsToolbox.Essentials/AutoWeakIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// This implementation is thread safe; the draw back is that generating new items is slow, so this type is not suitable for caching a large amount of items.
/// </remarks>
public sealed class AutoWeakIndexer<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : notnull
where TValue : class
{
private readonly object _sync = new();
Expand Down Expand Up @@ -179,4 +180,4 @@ public void Clear()
{
_items = new Dictionary<TKey, WeakReference<TValue>>(_items.Comparer);
}
}
}
6 changes: 5 additions & 1 deletion src/TomsToolbox.Essentials/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public static IEnumerable<KeyValuePair<TValue, TKey>> Transpose<TKey, TValue>(th
/// <exception cref="ArgumentNullException">Any of the keys is null.</exception>
/// <exception cref="ArgumentException">Any of the keys is duplicate.</exception>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> items)
where TKey : notnull
{
return items.ToDictionary(item => item.Key, item => item.Value);
}
Expand All @@ -310,6 +311,7 @@ public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerab
/// <exception cref="ArgumentNullException">Any of the keys is null.</exception>
/// <exception cref="ArgumentException">Any of the keys is duplicate.</exception>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> items, IEqualityComparer<TKey>? comparer)
where TKey: notnull
{
return items.ToDictionary(item => item.Key, item => item.Value, comparer);
}
Expand Down Expand Up @@ -344,6 +346,7 @@ public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerab
/// </returns>
[return: NotNullIfNotNull("defaultValue")]
public static TValue? GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue? defaultValue)
where TKey : notnull
{
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
}
Expand Down Expand Up @@ -391,6 +394,7 @@ public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerab
/// The value from the dictionary, or the default value of <typeparamref name="TValue"/> if no item with the specified key exists.
/// </returns>
public static TValue? GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
where TKey : notnull
{
return dictionary.GetValueOrDefault(key, default);
}
Expand Down Expand Up @@ -506,4 +510,4 @@ public static int FindIndex<T>(this IEnumerable<T> source, Predicate<T> match)
.Select((item, index) => new { item, index })
.FirstOrDefault(item => match(item.item))?.index ?? -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvide
.Select(attr => attr.ConverterTypeName)
.ToList().Intercept(i => logBuilder.AppendLine($"Type names: {string.Join("; ", i)}"))
.Select(typeName => Type.GetType(typeName, true))
.ExceptNullItems()
.ToList().Intercept(i => logBuilder.AppendLine($"Types: {string.Join("; ", i)}"))
.Where(type => typeof(TypeConverter).IsAssignableFrom(type))
.ToList().Intercept(i => logBuilder.AppendLine($"Type converters: {string.Join("; ", i)}"))
.Select(type => (TypeConverter)Activator.CreateInstance(type))
.Select(type => (TypeConverter?)Activator.CreateInstance(type))
.FirstOrDefault();

log = logBuilder.ToString();

return result;
}
}
}
Loading

0 comments on commit 3fd8406

Please sign in to comment.