Tuesday, May 22, 2012

ArrayList-Utils


using System;
public class ArrayUtils<T>
    {
        System.Collections.Generic.Dictionary<string, T> registrationList;
        public void setList(System.Collections.ArrayList list, string key)
        {
            int k = -1, n = list.Count;
            registrationList = new System.Collections.Generic.Dictionary<string, T>();
            System.Reflection.PropertyInfo property = typeof(T).GetProperty(key);
            while (++k < n)
            {
                if (!Utils.isNull(property))
                {
                    registrationList.Add(property.GetValue((T)list[k], null).ToString(), (T)list[k]);
                }
            }
        }
        public int ListLength { get { return registrationList.Count; } }
        public T getByKey(string key)
        {
            return (T)registrationList[key];
        }
    }


///////////////////////// Foydalanish


//class1 bizni class imiz
public class class1{
int id;
int name;
inr code;
public int ID{get{return id;}set{id=value;}}
public string Name{get{return name;}set{name=value;}}
public string Code {get{return code;}set{code=value;}}
}

//bizga ArrayList bor uning tarkibi class1 lardan tashkil topgan
ArrayList list=new ArrayList()
for(int i=0;i<10;i++)
{ class1 c=new class1();
   c.id=1; c.name="bir"; c.code="BIR";
}

//biz ArrayUtils class tool imizga kiritamiz, e`tibor bering "Name" so`ziga- ya`ni By Name set qilingan. Name property bo`yicha search qilishimiz mumkin bu yerda
Utils.ArrayUtils<ColumnItem> utils = new Utils.ArrayUtils<ColumnItem>();

utils.setList(list, "Name");

//va uni ichidan "Rahim" so`zi yozilgan elementni Code ni olishimiz mumkin
col = ((ColumnItem)utils.getByKey("Rahim")).Code




______________________________________________________________
   Kind Regards
   Rakhim Turdiev

No comments:

Post a Comment