包含泛型类型类列表的类



我有一个泛型类,JSTable,它采用RowType类型。我想有一个可以包含许多JSTables的类,每个类都有不同的 RowType,这样我就可以做一些类似于C#中的Func<>的事情,其中它有许多可选类型。这是否可能,因为有许多Func<>表示?我想要一个无限的选项,以便我可以潜在地声明一个包含数百个表或一个表的JSGridVM

public class JSGridVM<?>//where ? is as many types as I want
{
public List<JSTable<?>> Tables { get; set; };
}
public class JSTable<RowType>
{
public JSTable() { }
public JSTable(List<RowType> rows, List<JSGridColumn> columns, bool allowEditingRows, bool allowDeletingRows, string updateURL, string deleteURL)
{
Rows = rows;
Columns = columns;
AllowEditingRows = allowEditingRows;
AllowDeletingRows = allowDeletingRows;
UpdateURL = updateURL;
DeleteURL = deleteURL;
}
public List<RowType> Rows { get; set; }
public List<JSGridColumn> Columns { get; set; }
public bool AllowEditingRows { get; set; }
public bool AllowDeletingRows { get; set; }
public string UpdateURL { get; set; }
public string DeleteURL { get; set; }
}
public class JSGridColumn
{
public string PropertyName { get; set; }
public ColumnType Type { get; set; }
}
public enum ColumnType
{
Text,
Hidden,
}

然后声明喜欢

var jsGridVM = new JSGridVM<SomeClass1, SomeClass2, SomeClass3>();

var jsGridVM = new JSGridVM<SomeClass1>();

你应该声明泛型类型参数<RowType>不是在class级别,而是在方法:AddTableGetTable

public class JSGridVM
{
private Dictionary<Type, object> Tables = new Dictionary<Type, object>();
public JSGridVM AddJSTable<RowType>()
{
Tables.Add(typeof(RowType), new JSTable<RowType>());
return this;
}
public JSTable<RowType> GetJSTable<RowType>()
{
Tables.TryGetValue(typeof(RowType), out object temp);
return (JSTable<RowType>)temp;
}
}

用法:

var sample = new JSGridVM();
sample.AddJSTable<RowTypeA>().AddJSTable<RowTypeB>();
var test = a.GetJSTable<RowTypeA>();

您可以滚动自己的类,但 .NET DataSet 类可能是您所追求的。 数据集已经存在了很长时间,并且仍然非常有用。

var ds = new DataSet();
var dataTable = new DataTable("DataTable");
var stringCol = new DataColumn("String Column", typeof(string));
var intCol = new DataColumn("Integer Column", typeof(int));
var decimalCol = new DataColumn("Decimal Column", typeof(decimal));
dataTable.Columns.AddRange(new [] {stringCol, intCol, decimalCol});
var newRow = new object[]
{
"String item",
1,
100.08m
};
dataTable.Rows.Add(newRow);
ds.Tables.Add(dataTable);
var row = ds.Tables["DataTable"].Rows[0];
var stringRowValue = row["String Column"];
var intRowValue = row["Integer Column"];
var decimalRowValue = row["Decimal Column"];
Console.WriteLine($"String value: {stringRowValue}nInteger value: {intRowValue}nDecimal Value: {decimalRowValue}");
var rowArr = new DataRow[ds.Tables["DataTable"].Rows.Count];
ds.Tables["DataTable"].Rows.CopyTo(rowArr, 0);
var list = rowArr.ToList();
foreach (DataRow rowitem in list)
{
Console.WriteLine($"nFrom List: String value: {rowitem["String Column"]}nInteger value: {rowitem["String Column"]}nDecimal Value: {rowitem["String Column"]}");
}
Console.ReadKey();

不确定您要完成什么,但这已经非常强大了。 如果您尝试通过创建泛型类来完成特定用例,请查看 MSDN 以获取更多帮助。

使用泛型: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters

功能: https://msdn.microsoft.com/en-us/library/bb549151(v=vs.110(.aspx

为什么不:

public class JSGridVM<JSTable>
{
public List<JSTable> Tables { get; set; };
}
public class JSTable<?>
{
}
var rowTypeAnimals = AnimalRowType();
var rowTypeBirds = BirdRowType();
var animalTable = new JSTable<AnimalRowType>(){rowTypeAnimals };
var birdTable = new JSTable<BirdRowType>(){rowTypeBirds };
var grid = new JSGridVM(){animalTable,birdTable};

保持表的行类型泛型,网格将始终具有相同的 JSTable 类型。

最新更新