gpt4 book ai didi

c# - 'customObject' 类型的对象无法转换为 'customObject' 类型

转载 作者:太空狗 更新时间:2023-10-29 23:19:09 24 4
gpt4 key购买 nike

调用自定义对象时出现以下错误
“‘customObject’类型的对象无法转换为‘customObject’类型。”

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

  • 我动态调用 dll 中的方法。
  • 加载程序集
  • 创建实例....

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

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

“‘customObject’类型的对象无法转换为‘customObject’类型。”

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

包含该方法的类文件在不同的项目中。此外,自定义对象是同一文件中的单独类。

我的代码中没有所谓的静态程序集。我正在尝试动态调用网络方法。此 web 方法将 customObject 类型作为输入参数。因此,当我调用 web 方法时,我正在动态创建代理程序集和所有内容。从同一个程序集,我试图创建一个 cusotm 对象的实例,将值分配给它的属性,然后将这个对象作为参数传递并调用该方法。一切都是动态的,没有什么是静态的。:(

添加引用未使用。以下是我尝试创建的示例代码

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;
}
}
}

我在我所做的工作中找不到任何static

非常感谢任何帮助。

问候,帕尼库马尔光伏

最佳答案

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

您正在使这比您需要的复杂得多。老实说。

编辑如果你像这样创建一个类:

public class SoapClient : SoapHttpClientProtocol
{

public SoapClient()
{

}

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

}

并这样调用它:

SoapClient soapClient = new SoapClient();
soapClient.Url = webServiceAsmxUrl;
soapClient.Invoke(methodName, args);

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

关于c# - 'customObject' 类型的对象无法转换为 'customObject' 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2622340/

24 4 0