title: Complex attribute declaration for methods in CodeDom
body: <p>I'm trying to generate some methods using CodeDom, I have a problem generating custom attributes for these methods.</p>
  
  <p>I can manage simple empty attributes, such as</p>
  
  <p><code>[DataMember()]</code></p>
  
  <p>or attributes with string value arguments,</p>
  
  <p><code>[DataContract(Namespace = "http://somenamespace")]</code></p>
  
  <p>But there are more complex attributes I need to generate, such as</p>
  
  <p><code>[WebInvoke(Method = "POST", UriTemplate = "SomeTemplate", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]</code></p>
  
  <p>and</p>
  
  <p><code>[FaultContract(typeof(Collection&lt;MyFault&gt;))]</code></p>
  
  <p>For the arguments with enum values <code>ResponseFormat = WebMessageFormat.Json</code> I've tried similar approach to what I did for strings, creating a CodePrimitiveExpression instance:</p>
  
  <pre><code>CodeAttributeDeclaration webInvoke = new CodeAttributeDeclaration()
  {
  
      Name = "WebInvoke",
      Arguments =
              {
                  new CodeAttributeArgument
                  {
                      Name = "Method",
                      Value = new  CodePrimitiveExpression("POST")
                  },
                  new CodeAttributeArgument
                  {
                      Name = "UriTemplate",
                      Value = new  CodePrimitiveExpression(method.Name)
                  },
                   new CodeAttributeArgument
                  {
                      Name = "RequestFormat",
                      Value = new CodePrimitiveExpression(WebMessageFormat.Json)
                  },
                    new CodeAttributeArgument
                  {
                      Name = "ResponseFormat",
                      Value = new  CodePrimitiveExpression(WebMessageFormat.Json)
                  }
              }
  };
  </code></pre>
  
  <p>However, this does not work, I get an exception saying </p>
  
  <blockquote>
    <p>Invalid Primitive Type: System.ServiceModel.Web.WebMessageFormat. Consider using CodeObjectCreateExpression.</p>
  </blockquote>
  
  <p>I did consider using CodeObjectCreateExpression, but I don't know how. It expects either a string or a CodeTypeReference, and as a second parameter an array of CodeExpression parameters. I have no idea what parameters to put there.</p>
  
  <p>As for the other attribute, the one with <code>typeof(Colleciton&lt;MyFault&gt;)</code>, I don't even know where to start. Any help would be appreciated.</p>
  
  <p>EDIT: Someone suggested I call CustomAttributeData.GetCustomAttributes on the methods I'm trying to mimic, so I did that. For the sake of efficiency and clarity, I'll provide the data in a screenshot. This gave me a somewhat better idea of what I'm dealing with, but I'm still unsure about how to implement it.
  <img src="https://i.stack.imgur.com/xfGEl.png" alt="enter image description here"></p>