如何从泛型类或方法的成员中获取T的类型?

天大地大妈咪最大 提交于 2019-12-30 19:05:51

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

假设我在类或方法中有一个泛型成员,所以:

public class Foo<T>
{
    public List<T> Bar { get; set; }

    public void Baz()
    {
        // get type of T
    }   
}

当我实例化该类时, T变为MyTypeObject1 ,因此该类具有通用列表属性: List<MyTypeObject1> 。 这同样适用于非泛型类中的泛型方法:

public class Foo
{
    public void Bar<T>()
    {
        var baz = new List<T>();

        // get type of T
    }
}

我想知道,我的类列表包含什么类型的对象。 那么名为Bar的列表属性或局部变量baz ,包含什么类型的T

我不能做Bar[0].GetType() ,因为列表可能包含零元素。 我该怎么做?


#1楼

使用以下扩展方法,您可以在没有反射的情况下逃脱:

public static Type GetListType<T>(this List<T> _)
{
    return typeof(T);
}

或者更一般:

public static Type GetEnumeratedType<T>(this IEnumerable<T> _)
{
    return typeof(T);
}

用法:

List<string>        list    = new List<string> { "a", "b", "c" };
IEnumerable<string> strings = list;
IEnumerable<object> objects = list;

Type listType    = list.GetListType();           // string
Type stringsType = strings.GetEnumeratedType();  // string
Type objectsType = objects.GetEnumeratedType();  // BEWARE: object

#2楼

您可以从任何实现IEnumerable <T>的集合类型中获取“T”类型,具体如下:

public static Type GetCollectionItemType(Type collectionType)
{
    var types = collectionType.GetInterfaces()
        .Where(x => x.IsGenericType 
            && x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        .ToArray();
    // Only support collections that implement IEnumerable<T> once.
    return types.Length == 1 ? types[0].GetGenericArguments()[0] : null;
}

请注意,它不支持两次实现IEnumerable <T>的集合类型,例如

public class WierdCustomType : IEnumerable<int>, IEnumerable<string> { ... }

我想如果你需要支持这个,你可以返回一系列类型......

此外,如果您正在执行此操作(例如,在循环中),您可能还希望缓存每个集合类型的结果。


#3楼

类型:

type = list.AsEnumerable().SingleOrDefault().GetType();

#4楼

考虑一下:我使用它以相同的方式导出20个类型列表:

private void Generate<T>()
{
    T item = (T)Activator.CreateInstance(typeof(T));

    ((T)item as DemomigrItemList).Initialize();

    Type type = ((T)item as DemomigrItemList).AsEnumerable().FirstOrDefault().GetType();
    if (type == null) return;
    if (type != typeof(account)) //account is listitem in List<account>
    {
        ((T)item as DemomigrItemList).CreateCSV(type);
    }
}

#5楼

我使用这种扩展方法来完成类似的事情:

public static string GetFriendlyTypeName(this Type t)
{
    var typeName = t.Name.StripStartingWith("`");
    var genericArgs = t.GetGenericArguments();
    if (genericArgs.Length > 0)
    {
        typeName += "<";
        foreach (var genericArg in genericArgs)
        {
            typeName += genericArg.GetFriendlyTypeName() + ", ";
        }
        typeName = typeName.TrimEnd(',', ' ') + ">";
    }
    return typeName;
}

public static string StripStartingWith(this string s, string stripAfter)
{
    if (s == null)
    {
        return null;
    }
    var indexOf = s.IndexOf(stripAfter, StringComparison.Ordinal);
    if (indexOf > -1)
    {
        return s.Substring(0, indexOf);
    }
    return s;
}

你这样使用它:

[TestMethod]
public void GetFriendlyTypeName_ShouldHandleReallyComplexTypes()
{
    typeof(Dictionary<string, Dictionary<string, object>>).GetFriendlyTypeName()
        .ShouldEqual("Dictionary<String, Dictionary<String, Object>>");
}

这不是你想要的,但它有助于展示所涉及的技术。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!