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

26 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
  15. 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
  16. Your site is amazing and your blogs are informative and knlowledgeble to my websites.This is one of the best tips in my life.I have in quite some time.Nicely written and great info.Thanks to share the more informations.

    Seo Experts
    Seo Company
    Web Designing Company
    Digital Marketing
    Web Development Company
    App Development

    OdpovědětVymazat
  17. SQream Technologies provides you with a state of the art software which combines modern GPU technology (Graphic Processing Units) with the best practices in today’s Big Data platforms, providing up to 100x faster insights from data.
    Bigdata Training in Chennai OMR

    OdpovědětVymazat
  18. Robotic Process Automation (RPA) is one of the most exciting developments in Business Process Management (BPM) in recent history. Some industry experts believe it may be even more transformational than cloud computing transformational than cloud Automationminds team. (RPA)Automationminds lets you program in (RPA),
    Robotic Process Automation course

    OdpovědětVymazat
  19. excellent oracle dba training i got in AUTOMATIONMINDS..hi this is rADHA last month i have did oracle dba training in AUTOMATIONMINDS really there coaching was very good and there fees is very controlled manner i just refer many of my frds too ,if you want to do oracle dba or pl/sql just refer them they may give some guidances too thanks you. best oracle institute with real time working professionals. I love AUTOMATIONMINDS
    Oracle SQL/PLSQL course

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

    OdpovědětVymazat
  21. 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
  22. All the latest updates from the Python Automationminds team. Python Automationminds lets you program in Python, in your browser. No need to install any software, just start coding straight away. There's a fully-functional web-based console and a programmer's text-editor
    Phyton training in Chennai

    OdpovědětVymazat
  23. Robotic Process Automation (RPA) is one of the most exciting developments in Business Process Management (BPM) in recent history. Some industry experts believe it may be even more transformational than cloud computing transformational than cloud Automationminds team. (RPA)Automationminds lets you program in (RPA),
    Robotic Process Automation course bluePrism

    OdpovědětVymazat