Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Attributes - Arrays or Duplicates?
    primarykey
    data
    text
    <p>I am creating a network chat client in C# as a side project. In addition to simple text messages, I also have slash-prefixed commands that can be entered into the input TextBox. I used a modular approach by creating an enum that contains all the various commands, and then decorating those commands with attributes.</p> <p>The attributes specify what slash-prefixed command can be entered to trigger the command, as well as any aliases to the primary command identifier and the command's usage.</p> <p>Example:</p> <pre><code>public enum CommandType : byte { [PrimaryIdentifier("file"), AdditionalIdentifier("f"), CommandUsage("[&lt;recipient&gt;] [&lt;filelocation&gt;]")] FileTransferInitiation, [PrimaryIdentifier("accept"), AdditionalIdentifier("a")] AcceptFileTransfer, // ... } </code></pre> <p>My problem arises when I try to allow multiple aliases to the primary command. I have attempted this two ways: by allowing duplicates of the <code>AdditionalIdentifier</code> attribute, or by making the constructor argument in <code>AdditionalIdentifier</code> a <code>params string[]</code>.</p> <p>With the former, I implemented it by decorating the attribute class with <code>AttributeUsage</code> and setting <code>AllowMultiple</code> to true. While this does indeed achieve what I'm looking for, I'm feeling like it could get really noisy really fast to have several lines of aliases, in addition to the other attributes.</p> <p>The latter also works, however, it generates the <a href="http://msdn.microsoft.com/en-us/library/h9f682z5%28VS.80%29.aspx" rel="nofollow noreferrer">compiler warning CS3016</a>, and says that that approach is not CLS-compliant. Obviously, this doesn't necessarily stop me from still using it, but I've learned to always treat warnings as errors.</p> <p>My actual question is should I ignore my objections with duplicates and just go ahead and use them, or is there some other solution that could be used?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload