title: c# Dynamic web service call returns custom class
body: <p>I have a class that provides dynamic calls to a web service.</p>
  
  <pre><code>private System.Xml.XmlTextReader _XmlReader;
  private System.Web.Services.Description.ServiceDescription _ServiceDescription;
  private System.Web.Services.Description.ServiceDescriptionImporter _DescriptionImporter;
  private System.CodeDom.CodeNamespace _CodeNamespace;
  private System.CodeDom.CodeCompileUnit _CodeUnit;
  private System.Web.Services.Description.ServiceDescriptionImportWarnings _ImportWarnings;
  private System.CodeDom.Compiler.CodeDomProvider _Compiler;
  private System.CodeDom.Compiler.CompilerParameters _Parameters;
  private System.CodeDom.Compiler.CompilerResults _Results;
  private System.Reflection.Assembly _WebServiceAssembly;
  private string _Url;
  
  public WebServiceInvoker(string url)
  {
    _Url = url;
    System.Net.ServicePoint servicePoint = System.Net.ServicePointManager.FindServicePoint(new Uri(_Url));
    servicePoint.Expect100Continue = false;
    _XmlReader = new System.Xml.XmlTextReader(_Url);
    _ServiceDescription = System.Web.Services.Description.ServiceDescription.Read(_XmlReader);
    _DescriptionImporter = new System.Web.Services.Description.ServiceDescriptionImporter();
    _DescriptionImporter.ProtocolName = "Soap";
    _DescriptionImporter.AddServiceDescription(_ServiceDescription, null, null);
    _DescriptionImporter.Style = System.Web.Services.Description.ServiceDescriptionImportStyle.Client;
    _DescriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
    _CodeNamespace = new System.CodeDom.CodeNamespace();
    _CodeUnit = new System.CodeDom.CodeCompileUnit();
    _CodeUnit.Namespaces.Add(_CodeNamespace);
    _ImportWarnings = _DescriptionImporter.Import(_CodeNamespace, _CodeUnit);
    if (_ImportWarnings != 0) throw new Exception("Can not resolve registration service.");
    _Compiler = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
    string[] references = new string[] { "System.Web.Services.dll", "System.Xml.dll" };
    _Parameters = new System.CodeDom.Compiler.CompilerParameters(references);
    _Results = _Compiler.CompileAssemblyFromDom(_Parameters, _CodeUnit);
    foreach (System.CodeDom.Compiler.CompilerError oops in _Results.Errors) { throw new Exception("Compile Error Creating Assembly"); }
    _WebServiceAssembly = _Results.CompiledAssembly;
  }
  
  public LYSProducts.ProductObject GetProduct(string productCode)
  {
    LYSProducts.ProductObject p;
    object obj = _WebServiceAssembly.CreateInstance("RegisteryService");
    Type typ = obj.GetType();
    p = (LYSProducts.ProductObject)typ.InvokeMember("GetProduct", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { productCode });
    return p;
  }
  </code></pre>
  
  <p>The GetProduct method returns a custom class.</p>
  
  <pre><code>    namespace LYSProducts
  {
    [Serializable()]
    class ProductObject
    {
      public string ProductCode
      {
        get { return _ProductCode; }
        set { _ProductCode = value; }
      }
      public string ProductName
      {
        get { return _ProductName; }
        set { _ProductName = value; }
      }
      public string MajorVer
      {
        get { return _MajorVer; }
        set { _MajorVer = value; }
      }
      public string MinorVer
      {
        get { return _MinorVer; }
        set { _MinorVer = value; }
      }
  private string _ProductCode;
  private string _ProductName;
  private string _MajorVer;
  private string _MinorVer;
  
  public ProductObject(string productCode, string productName)
  {
    _ProductCode = productCode;
    _ProductName = productName;
    _MajorVer = "";
    _MinorVer = "";
  }
  public ProductObject(string productCode, string productName, string majorVer, string minorVer)
  {
    _ProductCode = productCode;
    _ProductName = productName;
    _MajorVer = majorVer;
    _MinorVer = minorVer;
  }
    }
  }
  </code></pre>
  
  <p>The code works fine. But "typ.InvokeMember" method returning an object and i don't know how to cast it to LYSProducts.ProdutcObject class. I have the same class deployed on the client side but when I try to cast i am receiving "Unable to cats object of type 'ProductObject' to 'LYSProducts.ProductObject'" error. I know the service must give some schema for "ProductObject" type and I can use reflection to create a ProductObject from that info, but i don't know how to do. </p>