示例#1
0
        public XLObjectMapping(Type t, Func <IXLObjectMapping> factory = null)
        {
            _t = t;

            if (factory == null && !typeof(IXLObjectMapping).IsAssignableFrom(t))
            {
                var fieldinfos = t.GetProperties()
                                 .Where(p => p.GetCustomAttributes(IgnorePropertyAttribute, false).Length == 0);

                var propnames = fieldinfos.Select(f => f.Name).ToArray();

                SetColnames(t, propnames, propnames);
            }
            else
            {
                IXLObjectMapping instance = (factory != null) ? factory() : (t.GetConstructor(Type.EmptyTypes) != null) ? (IXLObjectMapping)Activator.CreateInstance(t, new object[0])
                            : (IXLObjectMapping)Activator.CreateInstance(t);
                _columns = instance.ColumnCount();

                _setters = (o, i, v) => ((IXLObjectMapping)o).SetColumn(i, v);
                _getters = (o, i) => ((IXLObjectMapping)o).GetColumn(i);

                _colnames = new Lazy <string[]>(() =>
                {
                    var retval = new string[_columns];
                    for (int i = 0; i < _columns; i++)
                    {
                        retval[i] = instance.ColumnName(i);
                    }
                    return(retval);
                });
                _propnames = _colnames;
            }
        }
        public static XLObjectMapping GetObjectMapping <T>(T instance)
        {
            var t = typeof(T);
            IXLObjectMapping xlobj = instance as IXLObjectMapping;

            if (xlobj != null)
            {
                return(new XLObjectMapping(t, () => xlobj));
            }
            return(_types.GetOrAdd(t, f => new XLObjectMapping(t)));
        }