简单泛型——这可能吗 Method()?

发布于 2024-09-16 00:58:24 字数 1575 浏览 3 评论 0原文

我正在尝试创建一个通用映射函数,该函数获取字典及其子类的值,并使用反射将它们映射到 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 技术交流群。

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

发布评论

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

评论(2

温柔戏命师 2024-09-23 00:58:24

为什么不包起来呢?无论如何,它正在使用反射......

public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : class, new()
{
    return (T)MapStructInternal(typeof(T), xmlRpcStruct);
}

private static object MapStructInternal(Type t, XmlRpcStruct xmlRpcStruct)
{
    object map = t.GetConstructor(new Type[0] ).Invoke(new object[0]);
    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());
                prop.SetValue(map, MapStructInternal(map.GetType(),(XmlRpcStruct)subEntry.Value));
            }
        }
    }
    return map;
}    

Why not wrap it? It's using reflection anyway...

public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : class, new()
{
    return (T)MapStructInternal(typeof(T), xmlRpcStruct);
}

private static object MapStructInternal(Type t, XmlRpcStruct xmlRpcStruct)
{
    object map = t.GetConstructor(new Type[0] ).Invoke(new object[0]);
    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());
                prop.SetValue(map, MapStructInternal(map.GetType(),(XmlRpcStruct)subEntry.Value));
            }
        }
    }
    return map;
}    
凉薄对峙 2024-09-23 00:58:24

通过反射,您可以在运行时创建通用(以及该类的对象),但是,我不相信您可以创建通用方法。所以,如果您将代码从 更改

 public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : new()  {...}

 static class Mapper<T> where T : new() 
 {
      public T MapStruct(XmlRpcStruct xmlRpcStruct) {....}
 }

  var mapper = new Mapper<OrderDetails>();
  OrderDetails readyOrder = mapper.MapStruct<OrderDetails>(order);  

那么您应该能够使其工作。

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

 public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : new()  {...}

to

 static class Mapper<T> where T : new() 
 {
      public T MapStruct(XmlRpcStruct xmlRpcStruct) {....}
 }

  var mapper = new Mapper<OrderDetails>();
  OrderDetails readyOrder = mapper.MapStruct<OrderDetails>(order);  

Then you should be able to make it work.

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