Generic,reflection

来源:互联网 发布:linux建立用户组 编辑:程序博客网 时间:2024/04/28 22:31

protected BaseInstagramWriter<Posts> postWriter = new BaseInstagramWriter<Posts>();

 postWriter.AddItemToWriteQueue(new Post);


public class BaseInstagramWriter<T>

 {

   BlockingCollection<T> _queue1;
        BlockingCollection<T> _queue2;
        BlockingCollection<T>[] _queues;

   List<string> _keyNames;
        List<PropertyInfo> _keys;
        EntityCompare<T> _entityCompare;



public BaseInstagramWriter()
        {
            _queue1 = new BlockingCollection<T>();
            _queue2 = new BlockingCollection<T>();
            _queues = new BlockingCollection<T>[2];
            _queues[0] = _queue1;
            _queues[1] = _queue2;
            _keyNames = new List<string>();


            //setup here
            TableAttribute tableAttribute = (TableAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(TableAttribute));
            //get the specified table name or use the name of the class
            _tableName = tableAttribute != null ? tableAttribute.Name : typeof(T).Name;
            _connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["InstagramV3DB"].ConnectionString;
            _columns = typeof(T).GetProperties();



            foreach (var col in _columns)
            {
                Type propType = col.PropertyType;
                if (Attribute.GetCustomAttribute(col, typeof(KeyAttribute)) != null)
                {
                    _keyNames.Add(col.Name);
                }
            }


            _keys = _columns.Where(i => _keyNames.Contains(i.Name)).ToList();
            _entityCompare = new EntityCompare<T>(_keys, true);


            //single threaded;
            Task.Run(() => this.WriterThread());
        }










}

0 0
原创粉丝点击