문제

Is there a way to make the C# compiler automatically apply ToLower() (or any other manipulating method invoke) to a particular method parameter before it gets used inside the method?

//additional information: its purpose is to use a Dictionary with a case-insensitive key. Apparently, my first approach was completely wrong as I've already found a totally different approach that addresses the Dictionary itself, not the key it is accessed with.

My bad! I should have provided you with that info. So, no further answers needed. Anyway, thanks a lot!

Better approach in this particular case: c# Dictionary: making the Key case-insensitive through declarations

도움이 되었습니까?

해결책

No, the C# compiler will not do this for you (why should it be tailored to such a specific requirement?), but you could get this done by writing a simple wrapper struct around string:

struct LowerCaseString
{
    public LowerCaseString(string value)
    {
        this.value = value.ToLower();
    }

    private readonly string value;

    public static implicit operator LowerCaseString(string value)
    {
        return new LowerCaseString(value);
    }

    public override string ToString()
    {
        return value;
    }

    … // perhaps implement IEquatable<>, IComparable<>, etc.
}

The implicit conversion operator allows you to then write code like this:

Foo("Hello world.");

void Foo(LowerCaseString text)
{
    Console.WriteLine(text);
}

While this works as you would expect, there are some drawbacks with this approach:

  • a tiny (possibly negligible) performance hit, since a wrapper object must be instantiated around your string.

  • It might not be obvious to other users of your code that an implicit conversion operator exists, so they end up writing new LowerCaseString("Hello world.") instead. Looking at the class with Visual Studio's Object Browser would possibly resolve this issue if your team makes regular use of it.

  • this wrapper does not allow you to specify the CultureInfo used for .ToLower(). Do you want to use the CurrentCulture, or InvariantCulture, or some other?

다른 팁

Without using some sort of AOP, no.

PostSharp is one such tool, but it comes with added complexity, hidden code and much higher compile times.

It is much simpler to apply ToLower on the parameter directly in the method, or wrap all calls to the method with a method that would call ToLower beforehand.

Some AOP tools, like PostSharp, can do that.

But check twice that this is really what you want, and there is no easier and cleaner way.

You may have this done as suggested by others - trough AOP. However, this will get too complex and not worth the effort (in my opinion) if the case is only to pass lowercase strings.

You should provide more information on the matter, but I believe there are easier ways to achieve your goal. Take a look at the built-in string comparer classes - maybe StringComparer.OrdinalIgnoreCase will suit your purpose, if you need lowercase strings for comparison. If you need to display the strings in lowercase format to the user, then this is a responsibility of the code that displays the strings, not the business logic.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top