English 中文(简体)
1. 对继承的一般类型进行反思
原标题:Reflection over inherited generic types

我面临问题,我想到答案。

我有一个从普通类继承的阶层,而Im试图从这一类中取回T类,但我要说,我可以 t!

例如:

class Products : List<Product>
{}

问题是,从现在起,我不知道T类。 因此,我试图照此办理:

Type itemsType = destObject.GetType().GetGenericArguments()[0]

It didn t work out.

我的方法是:

public static object Deserialize(Type destType, XmlNode xmlNode)
    {         
        object destObject = Activator.CreateInstance(destType);

        foreach (PropertyInfo property in destType.GetProperties())
            foreach (object att in property.GetCustomAttributes(false))
                if (att is XmlAttributeAttribute)
                    property.SetValue(destObject, xmlNode.Attributes[property.Name].Value, null);
                else if (att is XmlNodeAttribute)
                {
                    object retObject = Deserialize(property.PropertyType, xmlNode.Nodes[property.Name]);
                    property.SetValue(destObject, retObject, null);
                }

        if (destObject is IList)
        {
            Type itemsType = destObject.GetType().GetGenericArguments()[0];
            foreach (XmlNode xmlChildNode in xmlNode.Nodes)
            {
                object retObject = Deserialize(itemsType, xmlNode);
                ((IList)destObject).Add(retObject);
            }
        }

        return destObject;
    }        

想法是阅读Xml文档,将其改为:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SETTINGS>
  <PRODUCTS>
    <PRODUCT NAME="ANY" VERSION="ANY" ISCURRENT="TRUE" />
    <PRODUCT NAME="TEST1" VERSION="ANY" ISCURRENT="FALSE" />
    <PRODUCT NAME="TEST2" VERSION="ANY" ISCURRENT="FALSE" />
  </PRODUCTS>
  <DISTRIBUTIONS>
    <DISTRIBUTION NAME="5.32.22" />
  </DISTRIBUTIONS>
</SETTINGS>

在此情况下,我从名单上收集的物品就属于我。

关于如何这样做的想法?

tks guys

最佳回答

你们需要获得基础类型的一般论点,例如:

Type itemType = destObject.GetType().BaseType.GetGenericArguments()[0];

However, this is not resilient; if an intermediary non-generic base type is introduced, it will fail.
Instead, you should find the type parameter of the IList<T> implementation.

例如:

Type listImplementation = destObject.GetType().GetInterface(typeof(IList<>).Name);
if (listImplementation != null) {
    Type itemType = listImplementation.GetGenericArguments()[0];
    ...
}
问题回答

如果你只是试图说明<代码”的类型。 IList 是:

<编码> 类型项目 类型= destType.GetInterface(类型(IList<>);Name.GetGeneric Arguments(0);

在此,你将如何在法典中使用:

var interface = destType.GetInterface(typeof(IList<>).Name);
var destList = destObject as IList;
// make sure that the destination is both IList and IList<T>
if (interface != null && destList != null)
{
    Type itemsType = interface.GetGenericArguments()[0];
    foreach (XmlNode xmlChildNode in xmlNode.Nodes) 
    { 
        object retObject = Deserialize(itemsType, xmlNode); 
        destList.Add(retObject); 
    } 
} 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签