包含一般类型类的​​列表的类

时间:2018-05-26 02:18:58

标签: c# list generics

我有一个泛型类JSTable,它采用RowType类型。我希望有一个可以包含许多JSTables的类,每个类都有不同的RowType,这样我可以执行Func<>C#所做的事情,它有许多可选类型。这是唯一可能的,因为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>();

OR

var jsGridVM = new JSGridVM<SomeClass1>();

3 个答案:

答案 0 :(得分:1)

您应该声明泛型类型参数<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>();

答案 1 :(得分:0)

您可以滚动自己的类,但.NET DataSet类可能就是您所追求的。 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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters

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

答案 2 :(得分:0)

为什么不:

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类型。