Skip to content
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

EnvBinder.Bind throws the following error: Invalid cast from 'System.String' to 'System.Enum' #117

Closed
MrDave1999 opened this issue Jan 20, 2023 · 0 comments · Fixed by #118
Labels
bug Something isn't working

Comments

@MrDave1999
Copy link
Owner

MrDave1999 commented Jan 20, 2023

Given the following code:

new EnvLoader().Load();
var settings = new EnvBinder().Bind<AppSettings>();
Console.WriteLine("Provider=" + settings.Provider);

public enum DirectLineProviders
{
    InDirectLine,
    DirectLineAzure
}

public class AppSettings
{
    public DirectLineProviders Provider { get; set; }
}

And the .env file:

PROVIDER=InDirectLine

The following exception is thrown when running the application:

Unhandled exception. System.InvalidCastException: Invalid cast from 'System.String' to 'DirectLineProviders'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at DotEnv.Core.EnvBinder.Bind[TSettings](EnvValidationResult& result)
   at DotEnv.Core.EnvBinder.Bind[TSettings]()
   at Program.<Main>$(String[] args) in C:\Users\syslan\Documents\Visual Studio 2022\DotNet-Env-Example\Program.cs:line 22

Convert.ChangeType does not support conversion to enums and that is why this error occurs.

@MrDave1999 MrDave1999 added the bug Something isn't working label Jan 20, 2023
@MrDave1999 MrDave1999 changed the title EnvBinder.Bind throws the following error: Invalid cast from System.String to enum. EnvBinder.Bind throws the following error: Invalid cast from System.String to enum. Jan 20, 2023
@MrDave1999 MrDave1999 changed the title EnvBinder.Bind throws the following error: Invalid cast from System.String to enum. EnvBinder.Bind throws the following error: Invalid cast from System.String to enum Jan 20, 2023
@MrDave1999 MrDave1999 changed the title EnvBinder.Bind throws the following error: Invalid cast from System.String to enum EnvBinder.Bind throws the following error: Invalid cast from 'System.String' to 'System.Enum' Jan 20, 2023
@MrDave1999 MrDave1999 linked a pull request Jan 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant