- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
调用自定义对象时出现以下错误“‘customObject’类型的对象无法转换为‘customObject’类型。”
以下是我遇到此错误时的情况:
当调用 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/
调用自定义对象时出现以下错误 “‘customObject’类型的对象无法转换为‘customObject’类型。” 以下是我遇到此错误时的情况: 我动态调用 dll 中的方法。 加载程序集 创建实例
我这周开始使用 ObjectMapper,我正在尝试将 JSON 映射到 2 个 CustomClasses,但我不知道 ObjectMapper 是否具有某些功能来执行我想要的操作。第一个 Cust
我创建了一个名为“Item”的对象,并且我想序列化其中包含 Items 的 ArrayList。我的程序与 ArrayList 完美配合,但它不适用于 ArrayList 。我相信这与我的对象有关。这
我的 ArrayList 变量有问题,该变量包含 Videos 的对象我创建的类。 ArrayList我创建的似乎总是空的,即使我 add给它一个对象。这是我声明并使用此 ArrayList 的代码:
我想对自定义类的条目执行文本搜索。该文本搜索不仅应匹配输入的字符串,还应检查拼写错误的单词。所以我正在寻找一个可以为我做这件事的图书馆。 更多背景信息:假设我从 SQLServer2008 数据库生成
我正在尝试更新多条记录权限。当我只更新一条记录的权限时,一切正常,但当更新多条记录时,没有任何反应。 List qbCustomObjectList = new LinkedList(); QBPer
我有这两个对象: public class BaseModule { public long BaseModuleID { get; set; } public string Modu
我在尝试对自定义对象的 ArrayList 进行排序时遇到问题。事实上,在排序之后,我的 ArrayList 中没有任何变化。我的方法有问题吗? 这是我的艺术家自定义对象属性: public clas
这个问题在这里已经有了答案: Getting an NSArray of a single attribute from an NSArray (3 个答案) 关闭 9 年前。
我得到了一个包含图片信息的类,比如文件路径、哈希值、字节。在另一个类中,我得到了一个通用列表,我在其中放置了包含图片信息的类中的对象。 那个类看起来像这样: [Serializable()]
我正在尝试保留一个假设为 xyz 的类的对象列表。当我在 NodeEntity 类中执行此操作时: @Property List listOfConditions 当通过 Neo4jOperation
我有一个如下的插件 class AbstractConfigExtension { public static final String NAME = "abstract_extension"
这是一个简单的问题,比任何事情都更具理论性。 Powershell 中的自定义对象和 PSObject 之间的真正区别是什么?您为什么以及何时创建它们? 谢谢! 最佳答案 PSCustomObject
我正在通过应用程序上下文将数据从 iOS 应用程序传输到其 Watch 扩展。我想发送我创建的自定义对象(名为WeatherReport)。 let context = ["report" : Wea
我在 Quickblox 控制台中创建了一个自定义对象,我可以检索这些对象,但现在需要访问原始 JSON 对象。 我可以通过字段手动迭代重建对象,并希望我可以直接引用和原始 JSON,从而节省大型数据
我有以下代码: ActiveModule resultObject = null; StringBuilder sqlStringBuilder = new Strin
有没有一种简单快速的方法来对 Java 中的 ArrayList 中的元素进行分组?我在问是否已经有办法不用我自己实现了?类似 Collections.sort(monuments,new objec
我创建了一个通用函数来检索JSON并对其进行解析: public IList RetrieveView(string result) { JavaScriptSeria
首先,如果我错了,请指正。我想从 ArrayList 中找到项目的索引(即字符串值)不使用 For 循环。 POJO: id; name; 代码: ArrayList list = new Array
嗨我正在尝试序列化 FileStream fileStream = new FileStream("batches.xml", FileMode.Open); XmlSerializer serial
我是一名优秀的程序员,十分优秀!