title: Web Services Dynamic Invoker question
body: <p>i've been playing around with Attributes over web services, and i've seen that The SoapHttpClientProtocol class need to define a WebServiceBinding Attribute.</p>
  
  <p>Watching this <a href="https://stackoverflow.com/questions/417275/-net-attributes-why-does-getcustomattributes-make-a-new-attribute-instance-eve">question</a> there seems one cannot modify the attributes on runtime, how can I achieve a dynamic web service invocation changing this attribute on runtime? is it possible?</p>
  
  <p>[Edit]
  I'm searching for an approach to do a dynamic invoke "generic style", rather to modify the WebServiceBinding attribute.</p>
  
  <p>This is, in a nutshell, my class:</p>
  
  <pre><code>using System.Web.Services;
  using System.Web.Services.Protocols;
  
  namespace Whatever {
      [WebServiceBinding(Name = "", Namespace = "")]
      public class WebServiceInvoker : SoapHttpClientProtocol {
          public WebServiceInvoker(string url, string ns, string bindingName) {
  
              ChangeNamespace(ns);
              ChangeBinding(bindingName);
              Url = url;
              // credentials, etc
      }
          public void ChangeNamespace(string ns) {
              var att = GetType().GetCustomAttributes(typeof (WebServiceBindingAttribute), true);         
              if (att.Length &gt; 0) {
                  // doesn't work
                  ((WebServiceBindingAttribute)att[0]).Namespace = ns;
              }
          }
          private void ChangeBinding(string bindingName) {
              var att = GetType().GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
  
              if (att.Length &gt; 0) {
                  // doesn't work
                  ((WebServiceBindingAttribute)att[0]).Name = bindingName;
              }
          }
          public object[] MakeInvoke(string method, object[] args) {
              var res = Invoke(method, method);
              return res;
          }
  
          public TRet InvokeFunction&lt;TRet&gt;(string method) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var res = Invoke(method, null);
              return MyUtils.ForceCast&lt;TRet&gt;(res);
          }
          public TRet InvokeFunction&lt;T1, TRet&gt;(string method, T1 par1) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] { par1 };
              var res = Invoke(method, args);
              return MyUtils.ForceCast&lt;TRet&gt;(res);
          }
          public TRet InvokeFunction&lt;T1, T2, TRet&gt;(string method, T1 par1, T2 par2) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] { par1, par2 };
              var res = Invoke(method, args);
              return MyUtils.ForceCast&lt;TRet&gt;(res);
          }
          public TRet InvokeFunction&lt;T1, T2, T3, TRet&gt;(string method, T1 par1, T2 par2, T3 par3) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] {par1, par2, par3};
              var res = Invoke(method, args);
              return MyUtils.ForceCast&lt;TRet&gt;(res);
          }
  
          public void InvokeAction(string metodo) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              Invoke(method, null);
          }
          public void InvokeAction&lt;T1&gt;(string method, T1 par1) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] { par1 };
              Invoke(method, args);
          }
          public void InvokeAction&lt;T1, T2&gt;(string method, T1 par1, T2 par2) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] { par1, par2 };
              Invoke(method, args);
          }
          public void InvokeAction&lt;T1, T2, T3&gt;(string method, T1 par1, T2 par2, T3 par3) {
              //Funcion&lt;T1, T2, T3, TRet&gt;
              var args = new object[] { par1, par2, par3 };
              Invoke(method, args);
          }
      }
  }
  </code></pre>
  
  <p>[Edit]
  I would like to call my class like so:</p>
  
  <pre><code>var miProxy = new WebServiceInvoker("http://webServiceLocation", "ns", "Binding");
  var res = miProxy.InvokeFunction&lt;string, string, Entity&gt;("MyWebMethod", stringPar1, stringPar2);
  </code></pre>