janmohammadi / MikrotikDotNet

Mikrotik C# API - MikrotikDotNet is a lightweight and easy to use ADO.NET like library for Mikrotik Api with extensibility and performance in mind.
34 stars 13 forks source link

cmd.ExecuteReader(<class>) returns only first key/value pair when mikrotik returns multiple "keys" in row #16

Open uom42 opened 1 year ago

uom42 commented 1 year ago

...

conn.Open();
var cmd = conn.CreateCommand("ip firewall address-list print");

Result (Raw api response):

!re=.id=*CC997=list=list1=address=192.168.1.1=creation-time=feb/13/2023 02:26:04=dynamic=false=dynamic=false=disabled=false=comment=aaa
!re=.id=*CC998=list=list1=address=webaddress.com=creation-time=feb/13/2023 02:26:04=dynamic=false=dynamic=true=disabled=false=comment=bbb

we create class:

class AddressListItem 
{
    public string MKID { get; set; }  //MKID always referce to .id field in response.
    public string List { get; set; }
    public string Address { get; set; }
    public string CreationTime { get; set; }
    public bool Disabled { get; set; }
    public bool @Dynamic { get; set; }
    public string Comment { get; set; }

        public override string ToString()
        {
            string[] allProps = this.GetType()
                .GetProperties()
                .Select(prop =>
                {
                    object? val = prop.GetValue(this);
                    string sVal = (null == val) ? "NULL" : val!.ToString()!;
                    return $"{prop.Name}={sVal}";
                }).ToArray();
            return string.Join(", ", allProps);
        }
}

And when we trying to ExecuteReader():

var result = cmd.ExecuteReader<AddressListItem>();
var f = result.Where(r => r.Dynamic == false);
foreach (var ip in f)
{
    Debug.WriteLine(ip.ToString());
}

we found that each row got only first "dynamic" property from mikrotik responce.

uom42 commented 1 year ago

may be we must to use class property attribure to specify which index we need?

something like:

[IndexedValue(1)]
public bool @Dynamic { get; set; }