I’m trying to create an array of the class DataTableColumn, but unfortunately I’m going crazy with that.
By now that’s what I tried to do without any success
public interface IDataTableColumn<out TValue> {
string Expression { get; }
DataTableFilterType FilterType { get; }
TValue Cast(string value);
}
public class DataTableColumn<TValue> : IDataTableColumn<TValue> {
public DataTableColumn(string expression, DataTableFilterType filterType = DataTableFilterType.Equal) {
Expression = expression;
FilterType = filterType;
}
public string Expression { get; private set; }
public DataTableFilterType FilterType { get; private set; }
public TValue Cast(string value) {
return value.As<TValue>();
}
}
My array SHOULD be like
private readonly IDataTableColumn<object>[] _columns = {
new DataTableColumn<int>("Id"), // ERROR
new DataTableColumn<string>("Description", DataTableFilterType.StartsWith), // SUCCESS
new DataTableColumn<DateTime?>("Date"), // ERROR
};
Actually working like that
private readonly dynamic[] _columns = {
new DataTableColumn<int>("Id"),
new DataTableColumn<string>("Description", DataTableFilterType.StartsWith),
new DataTableColumn<DateTime?>("Date"),
};
I think that using dynamic isn’t the best way to do that.. someone shed light, please!
EDIT
damn I forgot the error
Cannot implicitly convert type
DataTableColumn< int >’ to
‘IDataTableColumn< object >’. An
explicit conversion exists (are you
missing a cast?)Cannot implicitly convert type
DataTableColumn< System.DateTime? >’ to
‘IDataTableColumn< object >’. An
explicit conversion exists (are you
missing a cast?)
I believe it is because
intandDateTimeare value types which are not covariant withobject(they require boxing). I receive the following error when I compile your code:Per MSDN, “Variance in Generic Interfaces (C# and Visual Basic)”:
DataTableColumn<string>works because it derives directly fromobject.