Hidden [LocalizationRequired(required=true)] attribute on `MessageBox.Show`

68 views
Skip to first unread message

taodongl

unread,
Jun 12, 2017, 3:57:48 AM6/12/17
to resharper-plugins
Hello,
As for C# code: MessageBox.Show("Hello"),  Resharper will add  hidden `LocalizationRequiredAttribute`.

Which API implement the functionality? I want to declare some specific methods/classes localizable automatically without any source code modification.

Thanks.

Matt Ellis

unread,
Jun 12, 2017, 5:55:36 AM6/12/17
to resharper-plugins
This is ReSharper's "external annotations" feature. It's intended to add attributes to already compiled code. For this example, JetBrains doesn't have the source to MessageBox.Show, so it's not possible to add the LocalizationRequiredAttribute. So ReSharper supports applying attributes via .xml files named after the assembly, or living in a folder named after the assembly.

You can find out more in the webhelp.

Regards
Matt

taodongl

unread,
Jun 13, 2017, 3:11:00 AM6/13/17
to resharper-plugins
Is there API to insert "external annotations" dynamically? 
"xml file" isn't proper to my scenario: 
1) I don't know the location of assembly, I just want to add attributes to methods (namespace and method, parameter are known).
2) For current opened project, there isn't assembly to be generated before building, I want to add attributes to some methods implemented in current project without any modification.

在 2017年6月12日星期一 UTC+8下午5:55:36,Matt Ellis写道:

Matt Ellis

unread,
Jun 14, 2017, 9:02:21 AM6/14/17
to resharper-plugins
External annotations are for already compiled assemblies only. The files can live either side-by-side with the assembly (MyAssembly.ExternalAnnotations.xml) or packaged in an extension (DotFiles\Extensions\{MyExtension.PackageId}\annotations\MyAssembly.xml). And of course, ReSharper ships with a number of annotations in the install dir, which should be treated as read only.

External annotations cannot be used to add annotations to source code, only pre-compiled assemblies. Could you give an example of what you want to achieve by adding attributes to a method without changing the source?

Regards
Matt

taodongl

unread,
Jun 19, 2017, 10:33:19 PM6/19/17
to resharper-plugins
Please see the source code:

namespace A.B.C
{
    public partial class D
    {
        public D()
        {

            this.Print("Hello");
        }
        
        private void Print(string text)
        {
            Log.Print(text);
        }
    }
}

Currently, R# thinks "Hello" is localizable string. But the method (A.B.C.Print) just record "Hello" as a log, "Hello" should be extracted to resources file.
So I want to add hidden [Localizable(false)] annotations, to avoid R# highlight "Hello". 

在 2017年6月14日星期三 UTC+8下午9:02:21,Matt Ellis写道:

Matt Ellis

unread,
Jun 22, 2017, 11:12:43 AM6/22/17
to resharper-plugins
OK. This can't be done with external annotations. If you don't want to use attribute in the source, then the easiest way to do this is to make it a verbatim string, e.g. @"Hello", and set the ReSharper options to not localise verbatim strings (ReSharper -> Options -> Localisation)

I should point out that if you do use attributes in the source, this does NOT mean you will have a binary dependency on JetBrains.Annotations. All of the attributes are themselves marked with the [Conditional("JETBRAINS_ANNOTATIONS")] attribute, which means that ReSharper can see that they're applied in the source, but the compiler sees that the symbol in the Conditional attribute hasn't been set and doesn't compile them into your output assembly. So you can use attributes in your code without having to ship JetBrains.Annotations.dll.

Alternatively, you can also set a custom annotation programmatically, by implementing ICustomCodeAnnotationProvider.GetSpecialAttributeInstances. This will allow you to add arbitrary attributes to code elements, so you should be able to add a LocalizationRequiredAttribute instance.

Regards
Matt

taodongl

unread,
Jun 23, 2017, 12:07:10 AM6/23/17
to resharper-plugins
Thanks, I will take a look custom annotation programmatically.

在 2017年6月22日星期四 UTC+8下午11:12:43,Matt Ellis写道:
Reply all
Reply to author
Forward
0 new messages