给定 System.Type T,反序列化 List

发布于 2024-10-02 00:44:14 字数 1733 浏览 2 评论 0原文

我有许多想要序列化和反序列化的类。我正在尝试创建一个函数,给定类型(“用户”、“管理员”、“文章”等),该函数将使用这些项目的列表对文件进行反序列化。例如:

/* 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-09 00:44:14

你就快到了......你需要声明一个通用方法:

public static IList<T> GetAllItems<T>()
{
    XmlSerializer deSerializer = new XmlSerializer(typeof(List<T>));

    using(TextReader tr = new StreamReader(GetPathBasedOnType(typeof(T))))
    {
        IList<T> items = (IList<T>)deSerializer.Deserialize(tr);
    }

    return items;
}

You're almost there... you need to declare a generic method:

public static IList<T> GetAllItems<T>()
{
    XmlSerializer deSerializer = new XmlSerializer(typeof(List<T>));

    using(TextReader tr = new StreamReader(GetPathBasedOnType(typeof(T))))
    {
        IList<T> items = (IList<T>)deSerializer.Deserialize(tr);
    }

    return items;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文