lundi 20 janvier 2020

Is golang have operator like 'as'(C#)

Code logic as Below:

func (c *Auth) ensureCredential() (azcore, error) {
    _, err = c.build(authData)
}

func (c *Auth) build(authData []byte) (*Client, error) {
return NewClient()
}

The C# code I want to copy

public async Task test()
{
            var auth = new Auth();
            var inner = await _cre(auth);

            // This part code I want to copy 
            Client client = inner as Client
            Assert some values equals Client's fileds
}

public async Task<azcore> _credential(Auth provider)
{
            await provider.EnsureCredential(IsAsync, default);
            return (azcore)typeof(Auth).GetField("_cre", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(provider);
}

Both type Auth and Client implement interface azcore. I want to let Auth as Client so that test some values in Auth whether equals Client's field. Those values in Auth would assign to Client's fields according my code logic.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire