-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to port code in legacy nuget package to this package TypeConversion. #4
Comments
Hi @zydjohnHotmail ! using using deniszykov.TypeConversion;
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddSingleton<ITypeConversionProvider, TypeConversionProvider>();
// also you could configure it
services.Configure<TypeConversionProviderConfiguration>(options => {});
}); And used from any injectable context: using using deniszykov.TypeConversion;
public static class MyController
{
public MyController(ITypeConversionProvider conversionProvider)
{
object value = 1;
var nullableIntValue = conversionProvider.Convert<object, int?>(value);
}
} Or you can just expose it as static member: public static TypeConvert {
public static readonly Default = new TypeConversionProvider();
} This approach is not recommended because it impede testing and increases code cohesion. |
Hello: |
Ok, then you could use static class approach: // this is compatible with old package's signature
public static class TypeConvert
{
public static TypeConversionProvider Default = new TypeConversionProvider();
public static ToType Convert<FromType, ToType>(FromType value, string format = null, IFormatProvider formatProvider = null)
{
return Default.Convert<FromType, ToType>(value, format, formatProvider);
}
public static bool TryConvert<FromType, ToType>(FromType value, out ToType result, string format = null, IFormatProvider formatProvider = null)
{
return Default.TryConvert<FromType, ToType>(value, out result, format, formatProvider);
}
public static string ToString<FromType>(FromType value, string format = null, IFormatProvider formatProvider = null)
{
return Default.ConvertToString(value, format, formatProvider);
}
public static object Convert(object value, Type toType, string format = null, IFormatProvider formatProvider = null)
{
return Default.Convert(value?.GetType() ?? typeof(object), toType, value, format, formatProvider);
}
public static bool TryConvert(ref object value, Type toType, string format = null, IFormatProvider formatProvider = null)
{
return Default.TryConvert(value?.GetType() ?? typeof(object), toType, value, out value, format, formatProvider);
}
public static string ToString(object value, string format = null, IFormatProvider formatProvider = null)
{
return Default.ConvertToString(value, format, formatProvider);
}
} or continue to use old package. It is not broken or something, just obsolete. |
Hi, |
Hello:
I want to port an old C# class library to target .NET 5.0 for Windows 10.
The old class library used TypeConvert nuget version 2.1.6, but when I install this nuget package, I got warning for the TypeConvert nuget package is legacy. As I don’t want to use the legacy package, but I don’t quite understand how to use other nuget package or use C# built-in library like:
Convert.ChangeType Method from System.Runtime.dll.
Let me know if I can use this package TypeConversion (version 3.0.1) to replace TypeConvert package version 2.1.6 to replace the following C# code.
Thanks,
The text was updated successfully, but these errors were encountered: