)
而不是“pretty” “ typeof(文章)
.我有许多想要序列化和反序列化的类。我正在尝试创建一个函数,给定类型(“用户”、“管理员”、“文章”等),该函数将使用这些项目的列表对文件进行反序列化。例如:
/* I want to be able to do this */
List<Article> allArticles = GetAllItems(typeof(Article));
我不知道如何实现上述目标,但我设法让它工作:
/* BAD: clumsy method - have to pass a (typeof(List<Article>))
instead of typeof(Article) */
List<Article> allArticles = (List<Article>)GetAllItems(typeof(List<Article>));
/* Then later in the code... */
public static IList GetAllItems(System.Type T)
{
XmlSerializer deSerializer = new XmlSerializer(T);
TextReader tr = new StreamReader(GetPathBasedOnType(T));
IList items = (IList) deSerializer.Deserialize(tr);
tr.Close();
return items;
}
问题是我必须传递“ugly”typeof(List
)
而不是“pretty” “ typeof(文章)
.当我尝试这样做时:
List<User> people = (List<User>)MasterContactLists.GetAllItems(typeof(User));
/* Followed by later in the code...*/
public static IList GetAllItems(System.Type T)
{
XmlSerializer deSerializer = new XmlSerializer(typeof(List<T>));
TextReader tr = new StreamReader(GetPathBasedOnType(T));
IList items = (IList)deSerializer.Deserialize(tr);
tr.Close();
return items;
}
...我收到错误
/*Error 3
The type or namespace name 'T' could not be found
(are you missing a using directive or an assembly reference?)
on this line: ... = new XmlSerializer(typeof(List<T>)); */
问题:如何修复我的 GetAllItems()
以便能够调用这样的函数并让它返回一个列表:
List<Article> allArticles = GetAllItems(typeof(Article));
谢谢!
I have a number of classes that I want to serialize and de-serialize. I am trying to create a function that, given a type ("User", "Administrator", "Article", etc) will de-serialize the file with the list of those items. For example:
/* I want to be able to do this */
List<Article> allArticles = GetAllItems(typeof(Article));
I cannot figure out how to achieve the above, but I managed to get this working:
/* BAD: clumsy method - have to pass a (typeof(List<Article>))
instead of typeof(Article) */
List<Article> allArticles = (List<Article>)GetAllItems(typeof(List<Article>));
/* Then later in the code... */
public static IList GetAllItems(System.Type T)
{
XmlSerializer deSerializer = new XmlSerializer(T);
TextReader tr = new StreamReader(GetPathBasedOnType(T));
IList items = (IList) deSerializer.Deserialize(tr);
tr.Close();
return items;
}
The problem is that I have to pass "ugly" typeof(List<Article>)
instead of "pretty" typeof(Article)
.
When I try this:
List<User> people = (List<User>)MasterContactLists.GetAllItems(typeof(User));
/* Followed by later in the code...*/
public static IList GetAllItems(System.Type T)
{
XmlSerializer deSerializer = new XmlSerializer(typeof(List<T>));
TextReader tr = new StreamReader(GetPathBasedOnType(T));
IList items = (IList)deSerializer.Deserialize(tr);
tr.Close();
return items;
}
... I get an error
/*Error 3
The type or namespace name 'T' could not be found
(are you missing a using directive or an assembly reference?)
on this line: ... = new XmlSerializer(typeof(List<T>)); */
Question: how can I fix my GetAllItems()
to be able to call the function like this and have it return a list:
List<Article> allArticles = GetAllItems(typeof(Article));
Thanks!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
你就快到了......你需要声明一个通用方法:
You're almost there... you need to declare a generic method: