【发布时间】:2016-07-01 23:32:45
【问题描述】:
你好,这是我的第一篇文章,如果我做错了什么,请不要难过:D
我正在为一个大型程序编写反序列化器, 为此,我有一个自己的类
public class DeSerializeableElement
{
public Func<Object> action;
public Type type;
public DeSerializeableElement( Func<Object> p_action,Type p_type)
{
type = p_type;
action = p_action;
}
我读了一个字符串,然后它总是以 0XXX 一个 4 位数字开头。 有了这个数字,我可以从我的
中获得正确的方法Dictionary<int,DeSerializableElement>
字典的初始化是自动生成的,有 300 个元素
deSerializableObjectDictionary.Add(276, new DeSerializeableElement(GetString, typeof(System.String)));
GetString 是一个没有参数的方法,返回一个字符串
现在我的问题是,如果我反序列化一个列表,那么在我创建一个 DeSerializableElement 的那一刻,Func 会丢失其返回值信息。因为我把它保存为 Func 所以我得到了一个 List 但是在 GetString 的情况下获取 List 很重要 还有 GetInt 或 GetDouble 等等
所以如果我调用 GetList(GetString) 作为返回值,我想要一个 List 如果我调用 GetList(GetInt) 我想要一个 List 等等。 但我总是得到一个列表,因为我的 SerializableElement 具有 Func 作为属性
GetList 的调用看起来像
GetList(deSerializableObjectDictionary[objectIdent].action);
GetList 看起来像
public IList<T> GetList<T>(Func<T> p_action) //T is always Object because of Func<Object> here I the right Type
{
IList<T> list = new List<T>();
ExpectToken('['); //The start Token of a serialized List
while (!IsNextToken(']')) //End Token of serialized List
{
list.Add(p_action());
}
return lst;
}
【问题讨论】:
-
我无法理解你的问题。您能否更明确地说明什么不起作用以及您想要做什么?
-
所以你想为
int返回一个List<int>,为string返回一个List<string>? -
是的,这就是我想要的
-
很怀疑这就是你想要的,抛弃类型安全几乎从来都不是一个错误,但除非你尝试过,否则你不会发现这一点。您必须使用 Delegate 或
Func<dynamic>。 -
无论您如何看待它,您都需要某种运行时强制转换来完成这项工作。谨慎使用。