gpt4 book ai didi

c# - 将对象类型作为参数传递

转载 作者:行者123 更新时间:2023-11-30 19:10:03 27 4
gpt4 key购买 nike

我想替换以下函数,我的项目中存在该函数的几乎完全相同的副本,只是对象类型不同。我想做的是传递一个对象类型作为参数。

private void reloadBooks()
{
var books = session.Query.All<Book>();
boklist = new System.ComponentModel.BindingList<Book>(books.ToList<Book>());

lstBooks.ItemsSource = boklist;
}

我在想这样的事情:

private void getRecordsOfType(object objObject)
{
System.ComponentModel.BindingList<objObject.GetType()> recordList = new System.ComponentModel.BindingList<objObject.GetType()>();

var records = session.Query.All<objObject.GetType()>();
recordList = new System.ComponentModel.BindingList<objObject.GetType()>(records.ToList<bjObject.GetType()>());

return recordList;
}

但这似乎不起作用,我收到以下消息:

"Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'"

我读过一些关于使用泛型的类似内容,但我认为它在这种情况下没有用。

最佳答案

泛型正是您想要的:

private BindingList<T> getRecordsOfType<T>()
{
var records = session.Query.All<T>();
return new System.ComponentModel.BindingList<T>(records.ToList<T>());
}

关于c# - 将对象类型作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20489423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com