HelpText from resource string

Dec 8, 2014 at 5:33 AM
Hi,

I am looking for a way to use a localized string for the HelpText:

[Option('i', "input", Required = true, HelpText = "Input file to read.")]
public string InputFile { get; set; }

So I want to use a resource string for "Input file to read." text. How can I handle this, because resource strings cannot be used as attribute values?

Cheers,
cetama
Dec 11, 2014 at 7:21 AM
Hi cetama,

the problem are that the OptionAttribute Class are sealed and you can't inhariet from that. If not so you can inharient from that and override the HelpText Probperty with the reading function from a resource file.

Or is there any other solution?

Cheers
Thomas
Jul 22, 2016 at 11:40 AM
Hi,

You could implement you own version of the attribute by inherit from the base class "BaseOptionAttribute".

Then you add two properties, one for a type and the other for the property or method to call inside.
When the to properties are set, you could used the reflexion mechanism to call a method or get the property value and assign it to the property HelpText.

See a sample I use :
 #region Properties

        /// <summary>
        /// Gets or sets the help method type host.
        /// </summary>
        /// <value>
        /// The help method type host.
        /// </value>
        public Type HelpMethodTypeHost
        {
            get { return this._helpMethodTypeHost; }
            set
            {
                if (this._helpMethodTypeHost == null)
                {
                    this._helpMethodTypeHost = value;
                    CallHelpMethod();
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the help method.
        /// </summary>
        /// <remarks>
        /// To prevent compilation error you must use nameof
        /// </remarks>
        /// <value>
        /// The name of the help method.
        /// </value>
        public string HelpMethodName
        {
            get { return this._methodName; }
            set
            {
                if (this._methodName == null)
                {
                    this._methodName = value;
                    CallHelpMethod();
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Calls the help method.
        /// </summary>
        private void CallHelpMethod()
        {
            if (this._helpMethodTypeHost == null || this._methodName == null)
                return;

            var mthd = this._helpMethodTypeHost.GetMethod(this._methodName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
            if (mthd == null)
                throw new InvalidOperationException(Resources.Exceptions.ReflectionInfoWrong);

            var hlpMsg = mthd.Invoke(null, new object[0]);
            if (string.IsNullOrEmpty(this.HelpText))
            {
                this.HelpText = (string)hlpMsg;
            }
            else
            {
                this.HelpText += Environment.NewLine + hlpMsg;
            }
        }

        #endregion