<p>I imagine everyone has seen code like:</p>

<pre><code>public void Server2ClientEnumConvert( ServerEnum server)
{
    switch(server)
    {
       case ServerEnum.One:
           return ClientEnum.ABC
       //And so on.
</code></pre>

<p>Instead of this badness we could do somthing like:</p>

<pre><code>public enum ServerEnum
{
     [Enum2Enum(ClientEnum.ABC)]
     One,
}
</code></pre>

<p>Now we can use reflection to rip through ServerEnum and get the conversion mappings from the enum declaration itself.</p>

<p>The problem I am having here is in the declaration of the Enum2Enum attribute.</p>

<p>This works but replacing object o with Enum e does not. I do not want to be able to pass in objects to the constructor, only other enums.</p>

<pre><code>public class EnumToEnumAttribute : Attribute
{
    public EnumToEnumAttribute(object o){}
}
</code></pre>

<p>This fails to compile.</p>

<pre><code>public class EnumToEnumAttribute : Attribute
{
    public EnumToEnumAttribute(Enum e){}
}
</code></pre>

<p>Is there a reason for the compile error? How else could I pass in the information needed to map besides: </p>

<pre><code>EnumtoEnumAttribute(Type dest, string enumString)
</code></pre>

<p>This seems too verbose but if it is the only way then I guess I will use it.</p>

[{'answer_id': 40128, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 0, 'selected': False, 'text': "<p>I would probably use struct as the type, and then throw an exception if it isn't an Enum type.  I don't see how your (Type, string) option is any safer than using object or struct.</p>\n"}, {'answer_id': 40131, 'author': 'Daniel Jennings', 'author_id': 3641, 'author_profile': 'https://Stackoverflow.com/users/3641', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/aa664615%28VS.71%29.aspx" rel="nofollow noreferrer">Here are the rules</a> for the types that can be included as Attribute parameters:</p>\n'}, {'answer_id': 40160, 'author': 'Ben McNiel', 'author_id': 1455, 'author_profile': 'https://Stackoverflow.com/users/1455', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>@Danial Jennings I read through the rules there and found: "An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (Section 17.2).". </p>\n</blockquote>\n\n<p>How does trying for Enum e in the constructor fail based on the quoted rule? Is it because being of type enum does not guarantee that the enums passed in are publicly visibly? This seems right. Is there a way for force this rule at compile time?</p>\n\n<blockquote>\n  <p>@ bdukes You are exactly correct. I should have thought about that more.</p>\n</blockquote>\n\n<p>It looks like run time type checking is my only option to make sure I am only mapping enums to other enums.  </p>\n'}, {'answer_id': 40190, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 0, 'selected': False, 'text': '<p>Why not use a Dictionary? This could be a static property of your class, initialized with those fancy schmancy object initializers we got in 3.0? You would not be typing more code (the mapping has to be done even with the Attribute sollution).</p>\n'}, {'answer_id': 398461, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 2, 'selected': False, 'text': '<p>Using almost the same example, you can achieve this directly in the enum:</p>\n\n<pre><code>public enum ServerEnum\n{\n   One = ClientEnum.ABC,\n}\n</code></pre>\n\n<p>This has the benefit of not requiring Reflection, is easier to read (in my opinion), and overall requires less overhead.</p>\n'}]
['https://Stackoverflow.com/questions/40107', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1455/']