sobota 9. prosince 2017

Jak se vyhnout chybě CA1303: Do not pass literals as localized parameters

 Pokud je zapnuta analýza kódu a nastavena většina pravidel, stane se při zavedení nové metody, která používá jako parameter string a jejím volání, že kompilace kódu skončí chybou.

Ono je to technicky vzato varování, ale lze si v projeku nastavit, že tato varování budou považovány za chybu.


Mějme podobnou metodu:

public static object ThrowIfNull<TException>(this object value, string message) where TException : Exception
{
    if (value == null)
    {
        throw (TException)Activator.CreateInstance(typeof(TException), message);
    }
 
    return value;
}

A pokud ji voláme:

course.ThrowIfNull<ResourceNotFoundException>("The requested course is not found in the Course database.");

Dostaneme následující  chybu při kompilaci:

CA1303 Method 'xxx' passes a literal string as parameter 'message' of a call to 'GuardHelperExtensions.ThrowIfNull<ResourceNotFoundException>(this object, string)'. Retrieve the following string(s) from a resource table instead: "The requested course is not found in the Course database.".

Hlášku dostanete, když se pokusíme přes parametr přímo předat řetězec a něco z tohoto je splněno:
  • LocalizableAttribute parameteru je nastaven na true 
  • Parametr má v názvu slova jako Text, Message nebo Caption 

Na výběr je pak buď potlačit toto hlášení přes SuppressMessage atribut na dotčeném místě a nebo, pokud volaná metoda je upravitelná a jste si jisti, že žádnou lokalizaci vlastně nechcete, použitím již zmíněného LocalizableAttributu:
public static object ThrowIfNull<TException>(this object value, [Localizable(false)] string message) where TException : Exception
{
    if (value == null)
    {
        throw (TException)Activator.CreateInstance(typeof(TException), message);
    }
 
    return value;
}
A je to, při všech voláních této metody můžeme používat přímo řetězec a to bez následného chybového hlášení.

14 komentářů:


  1. This is so informative blog and i have used this 5 minutes thing very useful too happy with this blog........


    MSBI Training in Chennai
    Base SAS Training in Chennai
    Hadoop Training in Chennai

    OdpovědětVymazat
  2. This is very nce one.. really spend time with good thing.... i will share this to my frends...thank you so much for this post....
    waiting for the next blog.....

    Data Warehousing Training in Chennai


    OdpovědětVymazat
  3. It is more useful and knowledgeable. I hope it will help a lot for all. Thanks for sharing.
    SAP ABAP Training in Chennai
    SAP Training in Chennai

    OdpovědětVymazat
  4. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content.

    3D printing in Chennai

    3D printing companies in Chennai

    3D printing service Chennai

    3D printing service in Chennai

    OdpovědětVymazat
  5. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    UGC Approved Journals
    UGC Recognized Journals

    OdpovědětVymazat
  6. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.

    Indian Sweets Online USA

    Order Indian Sweets Online

    Best Indian Sweets

    Indian Sweets Near Me

    OdpovědětVymazat
  7. Very nice post here thanks for it I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive re like this.

    Best Laser Dental Clinic

    Best Laser Dental Clinic in Chennai

    Dental Clinic in Chennai

    Best Dental Hospital in Chennai

    OdpovědětVymazat
  8. also use it to promote your latest products and offers or inform people about what your business does. event marketing and thank you email for help

    OdpovědětVymazat