title: Serializing(WCF) custom attributes. Is it possible and advisable?
body: <p>I have searched both(Google &amp; here on SO) and the issue is that most questions that do show up have to do with serializing using attributes (XML attributes or DataContract), not the actual serialization of custom attributes. See below for the few questions that were similar</p>
  
  <p>We are in the planning stages of a certain project.</p>
  
  <p>One of the requirements has led us to question <strong>if possible and advisable to serialize(for WCF) custom attributes.</strong></p>
  
  <p><strong>If so, do we also need KnownType attributes decorating the service contract?</strong></p>
  
  <p>For example, we have the custom attribute, used in our DTO objects:</p>
  
  <pre><code>  [DataContract] // &lt;- is this allowed/adviseable
    [AttributeUsage(AttributeTargets.Property)]
    public class DtoPropertyAttribute : Attribute
    {
      [DataMember] // &lt;- this too
      public int MaximumLength { get; set; }
  
      public DtoPropertyAttribute(int maximumLength)
      {
        this.MaximumLength = maximumLength;
      }   
  
    }
  </code></pre>
  
  <p>At some point we then use... </p>
  
  <pre><code>Object[] GetCustomAttributes(bool inherit);
  </code></pre>
  
  <p>...from namespace:</p>
  
  <pre><code>System.Reflection.MemberInfo
  </code></pre>
  
  <p>...as follows on (the WCF service end):</p>
  
  <pre><code> //property is of type System.Reflection.ProperInfo
   var attributes property.GetCustomAttributes(false);
  </code></pre>
  
  <p>Then we check if the property has the attribute</p>
  
  <pre><code> //..
   //some logic here that iterates/gets a single attribute 'attribute'
   //...
  
   if(attribute is DtoPropertyAttribute
   {
      //do stuff
   }
  </code></pre>
  
  <p>We noted that Attribute is serializable:</p>
  
  <pre><code>namespace System {
  // ==++== 
  //
  //   Copyright (c) Microsoft Corporation.  All rights reserved.
  //
  // ==--== 
  
  namespace System { 
  
  using System;
  using System.Reflection; 
  using System.Collections.Generic;
  using System.Runtime.InteropServices;
  using System.Globalization;
  using System.Diagnostics.Contracts; 
  using System.Security;
  using System.Security.Permissions; 
  
      [Serializable] //&lt;--- See me please!!
      [AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple=false)] 
      [ClassInterface(ClassInterfaceType.None)]
      [ComDefaultInterface(typeof(_Attribute))]
      [System.Runtime.InteropServices.ComVisible(true)]
      public abstract class Attribute : _Attribute 
      {
          //...
      }
  </code></pre>
  
  <p><strong>FOOTNOTE: Similar questions that don't answer my question:</strong></p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/40211364/serialize-custom-attribute-values">Serialize Custom Attribute Values</a> (JSON-related)</li>
  </ol>