C# 泛型
有两个类:
第一个:
internal abstract class PropertyAccessorBase<T>
{
protected PropertyAccessorBase()
{
}
public abstract object GetValue(T obj, int index);
public abstract string[] Columns { get; }
}
第二个:
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
internal class SinglePropertyAccessor<T> : PropertyAccessorBase<T>
{
private List<string> _columns;
private List< SinglePropertyAcccessorConvert<T>> _convers;
private MultiPropertyAccessortConvert<T> _convert;
public delegate object MultiPropertyAccessortConvert(int index, T obj);
public delegate object SinglePropertyAcccessorConvert(T obj);
public SinglePropertyAccessor()
{
this._columns = new List<string>();
this._convers = new List<SinglePropertyAcccessorConvert<T>>();
}
public SinglePropertyAccessor(string columname) : this(columname, null)
{
}
public SinglePropertyAccessor(MultiPropertyAccessortConvert<T> convert, params string[] columns)
{
this._columns = new List<string>();
this._convers = new List<SinglePropertyAcccessorConvert<T>>();
this._convert = convert;
this._columns.AddRange(columns);
}
public SinglePropertyAccessor(string columnname, SinglePropertyAcccessorConvert<T> convert)
{
this._columns = new List<string>();
this._convers = new List<SinglePropertyAcccessorConvert<T>>();
this.AddColumn(columnname, convert);
}
public SinglePropertyAccessor<T> AddColumn(string column, SinglePropertyAcccessorConvert<T> convert)
{
this._columns.Add(column);
this._convers.Add(convert);
return (SinglePropertyAccessor<T>) this;
}
public override object GetValue(T obj, int index)
{
if (((index >= 0) && (index < this._convers.Count)) && (this._convers[index] != null))
{
return this._convers[index](obj);
}
if (this._convert != null)
{
return this._convert(index, obj);
}
return obj;
}
public override string[] Columns
{
get
{
return this._columns.ToArray();
}
}
}
哪位达人给俺说一下,为啥第二个会报错,如何处理。小弟感激涕零啊。