简体   繁体   中英

Convert Object parameter from VB.NET to C#

I have below code in VB.NET, it is working well now. I need to convert it to C#. It cannot be compiled because complier dont know about methods of proxy. Can you please let me know how I can convert paramater (Byref proxy as Object) to C#. Thank you so much.

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

To invoke dynamically in C#, you can use reflection:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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