"customObject"类型的对象无法转换为"customObject"类型

Pha*_* PV 5 c# asp.net reflection web-services web-applications

我调用自定义对象时收到以下错误
"Object of type 'customObject' cannot be converted to type 'customObject'."

以下是我收到此错误的情况:

  • 我动态调用dll中的方法.
  • 加载装配
  • 的CreateInstance ....

当调用MethodInfo.Invoke()传递int时,string作为我的方法的参数工作正常=>没有抛出异常.

但是如果我尝试将我自己的一个自定义类对象作为参数传递,那么我得到一个ArgumentException异常,它不是一个ArgumentOutOfRangeException或者ArgumentNullException.

"Object of type 'customObject' cannot be converted to type 'customObject'."

我在网络应用程序中这样做.

包含该方法的类文件位于不同的项目中.自定义对象也是同一文件中的单独类.

static assembly在我的代码中没有称为a的东西.我试图动态调用web方法.此webmethod将customObject类型作为输入参数.因此,当我调用webmethod时,我动态创建代理程序集和所有.从相同的组件我试图创建cusotm对象assinging的值其属性的一个实例,然后使此对象作为参数并调用方法.一切都是动态的,没有任何东西是静态创造.. :(

不使用添加引用.以下是我试图创建它的示例代码

public static object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args) 
    { 
        System.Net.WebClient client = new System.Net.WebClient(); 
        //-Connect To the web service 
        using (System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl")) 
        { 
            //--Now read the WSDL file describing a service. 
            ServiceDescription description = ServiceDescription.Read(stream); 
            ///// LOAD THE DOM ///////// 
            //--Initialize a service description importer. 
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); 
            importer.ProtocolName = "Soap12"; // Use SOAP 1.2. 
            importer.AddServiceDescription(description, null, null); 
            //--Generate a proxy client. importer.Style = ServiceDescriptionImportStyle.Client; 
            //--Generate properties to represent primitive values. 
            importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; 
            //--Initialize a Code-DOM tree into which we will import the service. 
            CodeNamespace nmspace = new CodeNamespace(); 
            CodeCompileUnit unit1 = new CodeCompileUnit(); 
            unit1.Namespaces.Add(nmspace); 
            //--Import the service into the Code-DOM tree. This creates proxy code 
            //--that uses the service. 
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1); 
            if (warning == 0) //--If zero then we are good to go 
            { 
                //--Generate the proxy code  
                CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp"); 
                //--Compile the assembly proxy with the appropriate references 
                string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" }; 
                CompilerParameters parms = new CompilerParameters(assemblyReferences); 
                CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1); 
                //-Check For Errors 
                if (results.Errors.Count > 0) 
                { 
                    StringBuilder sb = new StringBuilder(); 
                    foreach (CompilerError oops in results.Errors) 
                    { 
                        sb.AppendLine("========Compiler error============"); 
                        sb.AppendLine(oops.ErrorText); 
                    } 
                    throw new System.ApplicationException("Compile Error Occured calling webservice. " + sb.ToString()); 
                } 
                //--Finally, Invoke the web service method  
                Type foundType = null; 
                Type[] types = results.CompiledAssembly.GetTypes(); 
                foreach (Type type in types) 
                { 
                    if (type.BaseType == typeof(System.Web.Services.Protocols.SoapHttpClientProtocol)) 
                    { 
                        Console.WriteLine(type.ToString()); 
                        foundType = type; 
                    } 
                } 

                object wsvcClass = results.CompiledAssembly.CreateInstance(foundType.ToString()); 
                MethodInfo mi = wsvcClass.GetType().GetMethod(methodName); 
                return mi.Invoke(wsvcClass, args); 
            } 
            else 
            { 
                return null; 
            } 
        } 
    } 
Run Code Online (Sandbox Code Playgroud)

我找不到任何static事情.

任何帮助是极大的赞赏.

此致,Phani Kumar PV

Rya*_*ldi 2

您是否看过生成的代理类是什么样子的?您不需要代理来调用 Web 服务。只需创建一个继承自SoapHttpClientProtocol的类并调用 Invoke(methodName, params) 即可。

你让这件事变得比你需要的复杂得多。诚实地。

编辑 如果您创建这样的类:

public class SoapClient : SoapHttpClientProtocol
{

    public SoapClient()
    {

    }

    public object[] Invoke(string method, object[] args)
    {
        return base.Invoke(method, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

SoapClient soapClient = new SoapClient();
soapClient.Url = webServiceAsmxUrl;
soapClient.Invoke(methodName, args);
Run Code Online (Sandbox Code Playgroud)

我想您会发现它与您正在做的结果完全相同。