将Object参数从VB.NET转换为C#

时间:2013-10-15 08:35:07

标签: c# vb.net

我在VB.NET中有以下代码,现在运行良好。我需要将其转换为C#。它无法编译,因为编译器不知道代理方法。你能告诉我如何将paramater(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

1 个答案:

答案 0 :(得分: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;
}