Avatar
0
monkey Enlightened
monkey Enlightened
EzyFox Server CSharp Client: Convert EzyObject, EzyArray to C<h1>Object</h1>
EzyFox Server CSharp Client: Convert EzyObject, EzyArray to C

Object?

  • Answer
ezyfox-server binding
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Ví dụ em có 1 lớp thế này:

public class ChannelInfo
{
    public String appName { get; set; }
    public int userNumber { get; set; }
}

Em có thể tạo ra 1 lớp proxy thế này:

<span>public</span> <span>sealed</span> <span>class</span> <span>BindingProxy</span>
{
    <span>private</span> <span>readonly</span> EzyBinding ezyBinding;
    <span>private</span> <span>static</span> <span>readonly</span> BindingProxy INSTANCE = <span>new</span> BindingProxy();

    <span><span>private</span> <span>BindingProxy</span>(<span></span>)
    </span>{

        ezyBinding = EzyBinding.builder()
           .addReflectionMapConverter<ChannelInfo>()
           .build();
    }

    <span><span>public</span> <span>static</span> BindingProxy <span>getInstance</span>(<span></span>)
    </span>{
        <span>return</span> INSTANCE;
    }

    <span><span>public</span> EzyBinding <span>getBinding</span>(<span></span>)
    </span>{
        <span>return</span> ezyBinding;
    }
}

Sau đó em có thể sử dụng lớp BindingProxy ở bất cứ đâu, ví dụ:

EzyArray channelInfoArray = EzyEntityFactory.newArrayBuilder()
<span>    .append</span>(
        EzyEntityFactory.newObjectBuilder()
<span>            .append</span>(<span>"appName"</span>, <span>"a"</span>)
<span>            .append</span>(<span>"userNumber"</span>, <span>1</span>)
<span>            .build</span>()
    )
<span>    .append</span>(
        EzyEntityFactory.newObjectBuilder()
<span>            .append</span>(<span>"appName"</span>, <span>"b"</span>)
<span>            .append</span>(<span>"userNumber"</span>, <span>2</span>)
<span>            .build</span>()
    )
<span>    .build</span>()<span>;</span>
List<ChannelInfo> channelInfoList = BindingProxy.getInstance()
<span>    .getBinding</span>()
<span>    .unmarshall</span><List<ChannelInfo>>(channelInfoArray)<span>;</span>
Console.WriteLine(<span>"channelInfoList: "</span> + channelInfoList)<span>;</span>
  • 0
  • Reply