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í.

17 komentářů:

  1. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

    Selenium Training in Chennai

    OdpovědětVymazat

  2. 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
  3. Thanks for sharing, Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this.
    Web design company in chennai

    OdpovědětVymazat
  4. Thank you for sharing such a nice and interesting blog with us. hope it might be much useful for us. keep on updating...

    SAP Training in Chennai

    OdpovědětVymazat
  5. 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
  6. 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
  7. 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
  8. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    OFFERS IN CHENNAI

    OdpovědětVymazat
  9. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Wellnesscentres in Chennai
    Weightloss in Chennai
    Weightgain in Chennai

    OdpovědětVymazat
  10. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Wellnesscentres in Chennai
    Herbalife in Chennai
    Weightloss in Chennai
    Weightgain in Chennai

    OdpovědětVymazat
  11. 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
  12. Benefits of reading your post. This will help in the maintenance of our health.The daily routine will help you lose weight quickly and safely.My life is totally transformed after I followed this diet.I feeling great about myself.

    Herbalife in Chennai
    Herbalife Nutrition Products
    Herbalife Shakes
    Herbalife Weight Loss Shake
    Herbalife Weight Gain Shake

    OdpovědětVymazat
  13. As you have now understood the usage of ‘Record and Playback’ tool, the following are the different posts using which you can explore the functioning of ‘Selenium IDE’
    selenium Training in chennai

    OdpovědětVymazat

  14. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Best Astrologer In India

    Astrologer In India

    Top Astrologer In India

    Best Numerologist In India

    OdpovědětVymazat