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

WPF0071 should figure out types here #192

Open
JohanLarsson opened this issue May 15, 2018 · 1 comment
Open

WPF0071 should figure out types here #192

JohanLarsson opened this issue May 15, 2018 · 1 comment

Comments

@JohanLarsson
Copy link
Collaborator

namespace RoslynSandbox
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    public class StringComparisonToBoolConverter : IValueConverter
    {
        public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
        public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

        private readonly StringComparison trueWhen;

        private StringComparisonToBoolConverter(StringComparison trueWhen)
        {
            this.trueWhen = trueWhen;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Equals(value, this.trueWhen);
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException($"{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings");
        }
    }
}
@JohanLarsson
Copy link
Collaborator Author

[Test]
public void Issue192()
{
    var testCode = @"
namespace RoslynSandbox
{
using System;
using System.Globalization;
using System.Windows.Data;

public class StringComparisonToBoolConverter : IValueConverter
{
public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

private readonly StringComparison trueWhen;

private StringComparisonToBoolConverter(StringComparison trueWhen)
{
    this.trueWhen = trueWhen;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return Equals(value, this.trueWhen);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotSupportedException($""{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings"");
}
}
}";

    var fixedCode = @"
namespace RoslynSandbox
{
using System;
using System.Globalization;
using System.Windows.Data;

[ValueConversion(typeof(StringComparison), typeof(bool))]
public class StringComparisonToBoolConverter : IValueConverter
{
public static readonly StringComparisonToBoolConverter TrueWhenOrdinal = new StringComparisonToBoolConverter(StringComparison.Ordinal);
public static readonly StringComparisonToBoolConverter TrueWhenOrdinalIgnoreCase = new StringComparisonToBoolConverter(StringComparison.OrdinalIgnoreCase);

private readonly StringComparison trueWhen;

private StringComparisonToBoolConverter(StringComparison trueWhen)
{
    this.trueWhen = trueWhen;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return Equals(value, this.trueWhen);
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotSupportedException($""{nameof(StringComparisonToBoolConverter)} can only be used in OneWay bindings"");
}
}
}";
    AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, testCode, fixedCode, allowCompilationErrors: AllowCompilationErrors.Yes);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant