如何访问通用属性,而不知道封闭式泛型类型 [英] How to access generic property without knowing the closed generic type

查看:107
本文介绍了如何访问通用属性,而不知道封闭式泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个泛型类型如下:

I have a generic Type as follows

public class TestGeneric<T>
{
    public T Data { get; set; }
    public TestGeneric(T data)
    {
        this.Data = data;
    }
}

如果我现在有一个对象(这是从哪里来的一些外部源)从中我知道它的类型是一些封闭TestGeneric<>,但我不知道TypeParameter T.现在我需要访问我的对象的数据。问题是,我不能投的对象,因为我不知道到底哪个关闭TestGeneric。

If i have now an object (which is coming from some external source) from which i know that it's type is of some closed TestGeneric<>, but i don't know the TypeParameter T. Now I need to access the Data of my object. Problem is that i can't cast the object, since i don't know exactly to which closed TestGeneric.

我用

// thx to http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class
private static bool IsSubclassOfRawGeneric(Type rawGeneric, Type subclass)
{
    while (subclass != typeof(object))
    {
        var cur = subclass.IsGenericType ? subclass.GetGenericTypeDefinition() : subclass;
        if (rawGeneric == cur)
        {
            return true;
        }
        subclass = subclass.BaseType;
    }
    return false;
}

要确保,我的对象是通用型的。有问题的代码如下:

to make sure, my object is of the generic type. The code in question is as follows:

public static void Main()
{
    object myObject = new TestGeneric<string>("test"); // or from another source
    if (IsSubclassOfRawGeneric(typeof(TestGeneric<>), myObject.GetType()))
    {
        // the following gives an InvalidCastException
        // var data = ((TestGeneric<object>)myObject).Data;

        // if i try to access the property with reflection
        // i get an InvalidOperationException
        var dataProperty = typeof(TestGeneric<>).GetProperty("Data");
        object data = dataProperty.GetValue(myObject, new object[] { });
    }
}



我需要的数据,无论其类型(当然,如果我可以要求使用的GetType其类型()将被罚款,但不是必要的),因为我只是想用的ToString()转储它的XML。

I need the Data regardless of its type (well, if i could ask for its type using GetType() would be fine, but not necessary) since i just want to dump it in xml using ToString().

任何建议?感谢名单。

推荐答案

您需要知道封闭式泛型类的,然后才能访问它的通用成员。使用 TestGeneric<> 为您提供了开放式的定义,不能在不指定通用参数调用

You need to know the closed type of a generic class before you can access its generic members. The use of TestGeneric<> gives you the open type definition, which cannot be invoked without specifying the generic arguments.

为您获得的属性值的最简单方法是,以反映在直接使用闭式:

The simplest way for you to get the value of the property is to reflect on the closed type in use directly:

public static void Main()
{
    object myObject = new TestGeneric<string>("test"); // or from another source

    var type = myObject.GetType();

    if (IsSubclassOfRawGeneric(typeof(TestGeneric<>), type))
    {
        var dataProperty = type.GetProperty("Data");
        object data = dataProperty.GetValue(myObject, new object[] { });
    }
}

这篇关于如何访问通用属性,而不知道封闭式泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆