0

我在 VB.NET 中有以下代码,现在运行良好。我需要将其转换为 C#。它无法编译,因为编译器不知道代理方法。您能否让我知道如何将参数(Byref 代理作为对象)转换为 C#。太感谢了。

Public Shared Function SetupProxy(ByRef proxy As Object) As Boolean
    Dim token As New UsernameToken(Var.sHTNGUsername, Var.sHTNGPassword, PasswordOption.SendPlainText)
    Dim clientPolicy As New Policy

    clientPolicy.Assertions.Add(New UsernameOverTransportAssertion())

    proxy.SetPolicy(clientPolicy)
    proxy.SetClientCredential(token)

    Return True
End Function
4

1 回答 1

0

要在 C# 中动态调用,可以使用反射:

public static bool SetupProxy(ref object proxy)
{
    UsernameToken token = new UsernameToken(Var.sHTNGUsername, Var.sHTNGPassword, PasswordOption.SendPlainText);
    Policy clientPolicy = new ClientPolicy();

    clientPolicy.Assertions.Add(new UsernameOverTransportAssertion());

    proxy.GetType().InvokeMember("SetProxy", BindingFlags.InvokeMethod, null, proxy, new object[] { clientPolicy });
    proxy.GetType().InvokeMember("SetClientCredential", BindingFlags.InvokeMethod, null, proxy, new object[] { token });
    return true;
}
于 2013-10-15T08:47:19.603 回答