为什么 Assembly.GetTypes() 不会返回封闭的泛型类型?

Why would a closed generic type not be returned by Assembly.GetTypes()?

我为自定义通用 class (TabularList<>) 创建了通用 JavaScriptConverter 派生,我将其命名为 ITabularConverter<>ITabularConverter 使用反射来检索从 TabularList<> 通用类型定义派生的所有封闭类型,以通知 JavaScriptSerializer 它能够转换 ITabularConverter<> 的所有封闭类型。该代码如下所示:

public override IEnumerable<Type> SupportedTypes
{
    get
    {
        var type = typeof (TabularList<>);
        var itabulars = Assembly.GetAssembly(type).GetTypes()
            .Where(t => t.IsGenericType 
                && t.GetGenericTypeDefinition() == type);
        return itabulars;
    }
}

问题是,即使在这段代码执行时至少存在一个封闭类型的 TabularList<>,上面的代码也只返回开放泛型类型定义。当我将搜索扩展到所有当前加载的程序集时也是如此。

更奇怪的是,如果我检查调用堆栈,我可以看到 JavaScriptSerializer.Serialize 方法在哪里被调用,并使用立即数 window 检查被序列化的对象并证明存在通用定义的封闭版本。然而,当我在 Window 中执行以下代码时,结果是 false:

Assembly.GetAssembly(obj.TabularListProp.GetType())
    .GetTypes()
    .Contains(obj.TabularListProp.GetType());

所以我检索了其中定义了封闭泛型的程序集,然后在该程序集定义的类型中查找封闭泛型类型,但没有找到封闭类型。这有什么意义?

这里是TabularList<>的声明:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;

namespace Central.Claims.UX.ClaimWFMgrViewModel
{
    [Serializable]
    public class TabularList<T> : List<T>, ITabular
    {

        private List<List<object>> _tableView;

        [XmlIgnore]
        public List<List<object>> TableView
        {
            get { return GetTableView(); }
        }

        private List<KeyValuePair<string, Func<object, object>>> Schema { get; set; }  
        public TabularList()
        {
            Initialize();
        }

        public TabularList(IEnumerable<T> source) : base(source)
        {
            Initialize();
        }

        private void Initialize()
        {
            RefreshTableView = true;
            var type = typeof(T);

            if (Schemas.ContainsKey(type.Name))
            {
                Schema = Schemas[type.Name];
            }
            else
            {
                Schema = new List<KeyValuePair<string, Func<object, object>>>();
            }
        }

        protected List<List<object>> GetTableView()
        {    
            GetSchema();
            BuildTable();

            return _tableView;
        }

        private void GetSchema()
        {
            if (this.Any())
            {
                var properties = this.First().GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

                foreach (var property in properties)
                {
                    var getter = property.GetGetMethod();

                    Schema.Add(new KeyValuePair<string, Func<object, object>>(
                        property.Name,
                        (Func<object, object>) Delegate.CreateDelegate(typeof (Func<object, object>), getter)
                        ));
                }
            }
        }

        private void BuildTable()
        {
            _tableView = new List<List<object>>();

            foreach (var item in this)
            {
                TableView.Add(ToTableRow(item));
            }
        }

        private List<object> ToTableRow(T item)
        {
            var row = new List<object>();

            foreach (var column in Schema)
            {
                row.Add(column.Value(item));
            }

            return row;
        }
    }     
}

根据此处提供的答案,我在 SO 问题 How to retrieve a list of all closed generic types generated by the .NET runtime?

中改写了这个问题

请记住,反射只是查询 元数据,因此其中包含的任何信息都是纯粹的编译类型信息。事实上,您拥有 TabularList<SomeType>) 的实例 而不是 更改定义它的程序集中包含的元数据。

封闭泛型在定义开放泛型的程序集中定义,也不在创建该特定封闭类型的程序集中定义。

您是否希望在 mscorlib 中找到 每个可能的 List<T> 封闭定义的元数据?您希望在创建 List<int> 变量的程序集中找到它吗?

请注意,它确实以另一种方式工作 - 如果您调用

Assembly a = Assembly.GetAssembly(typeof(List<int>));

你得到大会

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

因此,您也许可以反转您的逻辑 - 与其搜索程序集中的所有封闭类型,不如查找封闭类型的程序集以查看它是否 "supported"。

通用类型通常不存在于程序集中。如果是这种情况,那么 每个 类型参数的可能组合都需要存在,这很快就会为您提供无限数量的不同类型。

因此,通用类型 定义 是程序集中存在的具体类型。您可以通过对类型调用 GetGenericTypeDefinition() 来获取泛型类型定义:

Type t = typeof(List<int>);
t.Assembly.GetTypes().Contains(t); // false
t.Assembly.GetTypes().Contains(t.GetGenericTypeDefinition()); // true