简单泛型——这可能吗 Method()?
我正在尝试创建一个通用映射函数,该函数获取字典及其子类的值,并使用反射将它们映射到 T 中的字段。在对象T内,有必须深入的子对象,通过递归,这看起来是一个非常简单的概念。然而我陷入了困境——我不确定这是否是泛型的限制,或者是否只是我做错了。这是我的函数:
我使用以下命令调用第一个实例。
OrderDetails readyOrder = Tools.MapStruct<OrderDetails>(order);
*XmlRpcStruct 是一个字典,子类总是 XmlRpcStruct 的 -- 其中包含
public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : new()
{
T map = new T();
foreach (DictionaryEntry entry in xmlRpcStruct)
{
XmlRpcStruct entryStruct = (XmlRpcStruct)entry.Value;
foreach (DictionaryEntry subEntry in entryStruct)
{
if (subEntry.Value.GetType() != typeof(XmlRpcStruct))
{
var prop = map.GetType().GetField(subEntry.Key.ToString());
prop.SetValue(map, subEntry.Value);
}
else
{
var prop = map.GetType().GetField(subEntry.Key.ToString());
ERROR -->prop.SetValue(map, MapStruct<prop.GetType()> (subEntry.Value));
}
}
}
return map;
}
例如,我可以有一个包含以下数据的字典:
Key----Value
First--John
Last---Smith
Age----45
地址字典对象
...和一个对象:
obj.First (string)
obj.Last (string)
obj.Age (int )
obj.Address (AddressType)
我使用对象中的类型来确定名称值对中的 Dictionary 对象应转换为什么。
我需要能够动态获取字典中子项的类型并递归调用相同的函数。我哪里出错了?
I am attempting to create a generic mapping function that takes the values of a dictionary and its sub classes and maps them to the fields in T using reflection. Within object T, there are sub objects that must be drilled into, and through recursion, it seems like a pretty simple concept. However I am stuck -- and I'm not sure if it's a limitation of generics, or if it's something I am simply doing wrong. Here's my function:
I call the first instance with the following.
OrderDetails readyOrder = Tools.MapStruct<OrderDetails>(order);
*XmlRpcStruct is a dictionary, and the sub classes are always XmlRpcStruct's -- which conta
public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : new()
{
T map = new T();
foreach (DictionaryEntry entry in xmlRpcStruct)
{
XmlRpcStruct entryStruct = (XmlRpcStruct)entry.Value;
foreach (DictionaryEntry subEntry in entryStruct)
{
if (subEntry.Value.GetType() != typeof(XmlRpcStruct))
{
var prop = map.GetType().GetField(subEntry.Key.ToString());
prop.SetValue(map, subEntry.Value);
}
else
{
var prop = map.GetType().GetField(subEntry.Key.ToString());
ERROR -->prop.SetValue(map, MapStruct<prop.GetType()> (subEntry.Value));
}
}
}
return map;
}
For example, I could have a dictionary with the following data:
Key----Value
First--John
Last---Smith
Age----45
Address-Dictionary Object
...and an object:
obj.First (string)
obj.Last (string)
obj.Age (int)
obj.Address (AddressType)
I'm using the type in the object to determine what the Dictionary object from the name value pair should be cast as.
I need to be able to dynamically get the type of the sub item within my dictionary and recursively call the same function. Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不包起来呢?无论如何,它正在使用反射......
Why not wrap it? It's using reflection anyway...
通过反射,您可以在运行时创建通用类(以及该类的对象),但是,我不相信您可以创建通用方法。所以,如果您将代码从 更改
为
那么您应该能够使其工作。
Throught Reflection, you can create a generic class (and an object of the class) at runtime, however, I don't believe you can create a generic method. SO, if you change you code from
to
Then you should be able to make it work.