ricevo il seguente errore quando invoco un oggetto personalizzato
"Object of type 'customObject' cannot be converted to type 'customObject'."
oggetto di tipo 'customObject' non può essere convertito nel tipo 'customObject'
seguito è lo scenario quando sono ottenere questo errore:
- Invoco un metodo in una DLL dinamicamente.
- caricare un assembly
- CreateInstance ....
Quando si chiama MethodInfo.Invoke() passando int, string come parametro per il mio metodo funziona bene => Non ci sono eccezioni sono gettati.
Ma se provo a passare uno dei miei oggetti classe personalizzati come parametro, ottengo un'eccezione ArgumentException
e non è né ArgumentOutOfRangeException
né ArgumentNullException
.
"Object of type 'customObject' cannot be converted to type 'customObject'."
sto facendo questo in un'applicazione web.
Il file di classe contenente il metodo si trova in un progetto diverso. Anche l'oggetto personalizzato è una classe separata nello stesso file.
Non esiste una cosa chiamata static assembly
nel mio codice. Sto cercando di invocare un metodo web in modo dinamico. questo webmethod sta avendo il tipo CustomObject come parametro di input. Quindi quando invoco il webmethod sto creando dinamicamente l'assembly proxy e tutto. Dallo stesso assembly sto provando a creare un'istanza dell'oggetto cusotm asserendo i valori alle sue proprietà e quindi passando questo oggetto come parametro e invocando il metodo. tutto è dinamico e nulla si crea statica .. :(
di riferimento aggiuntivo non viene utilizzato. seguito è un codice di esempio ho provato a crearlo
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;
}
}
}
io non riesco a trovare nulla static
in quello che ho fare.
Qualsiasi aiuto è molto apprezzato.
saluti, Phani Kumar fotovoltaici
* Perché * lo faresti? Perché non solo "aggiungi riferimento web" al progetto? – Aaronaught
Sto sviluppando un'applicazione in cui l'URL WSDL è necessario per richiamare un metodo web in esso. come dovrei usare diversi metodi web non posso fare un riferimento web. –