简体   繁体   English

在XML-RPC.NET中,如何处理类型直到运行时才知道的返回值?

[英]In XML-RPC.NET how to handle a return value whose type is not known until runtime?

I'm using the library XML-RPC.NET in C# to call an XML-RPC method on my server. 我在C#中使用库XML-RPC.NET来在服务器上调用XML-RPC方法。 The method returns a string when everything is ok, but when an error occurs it returns an XmlRpcStruct. 一切正常时,该方法返回一个字符串,但是当发生错误时,它返回一个XmlRpcStruct。

XML-RPC.NET threw an exception when I got a different return type than I specified in my method signature: 当我得到的返回类型与方法签名中指定的返回类型不同时,XML-RPC.NET引发异常:

string login(string username, string password);

Exception message: "response contains struct value where string expected" 异常消息:“响应包含结构值(在期望字符串的地方)”

In the docs of XML-RPC.NET it says this: 在XML-RPC.NET的文档中说:

"How do I specify data whose type is not known until runtime? “如何指定直到运行时才知道其类型的数据?

Sometimes the type of a parameter, return value, or struct member is not known until runtime. 有时,直到运行时才知道参数的类型,返回值或结构成员。 In this scenario the System.Object datatype should be used. 在这种情况下,应使用System.Object数据类型。 For return values the actual type can be determined at runtime and handled appropriately." 对于返回值,可以在运行时确定实际类型并进行适当处理。”

So I changed my return type to object and now it works. 因此,我将返回类型更改为object,现在可以使用了。 However now I don't know how to handle the return value. 但是现在我不知道如何处理返回值。 If it's of type XmlRpcStruct I wan't to convert it to my Error class. 如果它是XmlRpcStruct类型,我将不将其转换为我的Error类。 Otherwise I treat it as a string. 否则,我将其视为字符串。 How do I convert it to my Error class? 如何将其转换为我的Error类? Does XML-RPC-NET have a convert method or something I can call? XML-RPC-NET是否具有转换方法或我可以调用的方法?

public interface Proxy : IXmlRpcProxy
{
    [XmlRpcMethod("login")]
    object login(string username, string password);
}

// When the login method fails I get an XmlRpcStruct that has a
// key "status" with a string value. I'd like to cast the returned
// XmlRpcStruct to my Error class. How?
public class Error : XmlRpcStruct
{
    public string status;
}

And then when I call the method: 然后当我调用该方法时:

object ret = proxy.login("admin", "1234");
Type t = ret.GetType();

if (t == typeof(XmlRpcStruct))
{
    // This will set err to null even though ret is not null
    // How do I convert it?
    Error err = ret as Error;
}
else
{
    string result = (string)ret;
}

Is there an easier method of doing this? 有更简单的方法吗? I could just set my method to a return type of string and then do try/catch around the method-call, but then I lose the status message returned in the Error. 我可以将我的方法设置为字符串的返回类型,然后在方法调用周围进行尝试/捕获,但是随后我丢失了错误中返回的状态消息。

What if your Error class were to hold a XmlRpcStruct variable: 如果您的Error类包含XmlRpcStruct变量,该怎么办:

public class Error
{
    public string status;
    public XmlRpcStruct xmlstr;
}

Then you can assign your XmlRpcStruct to the Error object's variable: 然后,您可以将XmlRpcStruct分配给Error对象的变量:

object ret = proxy.login("admin", "1234");
Type t = ret.GetType();

if (t == typeof(XmlRpcStruct))
{
    Error err = new Error();
    err.xmlstr = (XmlRpcStruct)ret;
}
else
{
    string result = (string)ret;
}

And you'll still have access to the original XmlRpcStruct through the Error class's variable without losing it to a try statement. 而且您仍然可以通过Error类的变量访问原始XmlRpcStruct,而不会将其丢失到try语句中。 There might be a better way to achieve this with inheritance, but this is a quick fix. 可以通过继承来实现此目的的更好方法,但这是一个快速解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM