繁体   English   中英

如何获得通用参数的类型

[英]how to get type of generic parameter

我无法确定我的应用程序中通用参数的类型。 情况类似于下面的代码。 当我得到通用ICollection ,我需要计数。 如果没有,我需要处理单个对象。

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Cat
    {
        public int Id { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cat cat1 = new Cat { Id = 1 };
            Cat cat2 = new Cat { Id = 2 };
            ICollection<Cat> cats = new List<Cat>();
            cats.Add(cat1);
            cats.Add(cat2);
            TestMethod<ICollection<Cat>>(cats);
            TestMethod<Cat>(cat1);
        }
        public static void TestMethod<T>(T parameter)
        {
            //if parameter is <ICollection<Cat>>, get count of cats?
            //else if (T is Cat), get id of the cat?
        }
    }
}

我错误地提出了问题,可能是猫,狗,老鼠或其他任何东西。 我不知道这是什么,我也不需要。 我正在尝试下面的代码,并得到转换错误。

((ICollection的)参数).Count之间;

我只是需要计数,如果它是任何对象的ICollection。

非常感谢您提供所有答案。

您可以使用模式匹配来检查类型,如下所示:

switch (parameter)
{
    case ICollection<Cat> collection:
        var count = collection.Count;
        // Do something
        break;

    case Cat cat:
        var id = cat.Id;
        // Do something
        break;

    default:
        throw new InvalidOperationException("No cats.");
}

但是,如果只想知道T是什么,则可以使用typeof(T)获取调用该方法时使用的泛型类型的System.Type对象。

您可以像下面那样获取参数(T)的类型,然后检查:

var type = parameter.GetType();
if(type == typeof(List<Cat>))
{
 // do something
}
else if(type == typeof(Cat))
{
 //do something
}

对于您的信息,如果出现以下情况,则为cat的运行时类型:

ICollection<Cat> cats = new List<Cat>();

类型:

List<Cat>

ICollection<Cat>

如果要检查并验证编译时类型,则在检查时可能需要对运行时类型使用显式强制转换。

其他替代方法只是使用“ is”关键字进行类型检查,如下所示:

if(parameter is ICollection<Cat>)
{

}
else if(parameter is Cat)
{

}

希望这会帮助你。

        int result;
        if (parameter is ICollection<Cat>)
            result = (parameter as (ICollection<Cat>)).Count;
        else if (parameter is Cat)
            result = (parameter as Cat).Id;

这不是您要使用泛型的情况,因为这不是泛型的目的。

您在这里有两个选择。

您可以使用以下两种方法:

public void TestMethod(Cat cat) {...}
public void TestMethod(ICollection<Cat> cats) {...}

或者,如果您确实需要这种通用方法,则可以使用object作为参数。

 public void TestMethod(object obj) 
 {
    Cat cat = obj as cat;
    if(cat != null)
    { 

        return;
    }
    ICollection<Cat> cats = obj as ICollection<Cat>;
    if(cats != null)
    {

    }
 }

但是即使那样,如果您使用反射,这也是一个好主意。

试试这个可能会帮助你

class Cat
{
    public int Id { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Cat cat1 = new Cat { Id = 1 };
        Cat cat2 = new Cat { Id = 2 };
        ICollection<Cat> cats = new List<Cat>();
        cats.Add(cat1);
        cats.Add(cat2);
        TestMethod<ICollection<Cat>>(cats);

        TestMethod<Cat>(cat1);
    }
    public static void TestMethod<T>(T parameter)
    {
        if (typeof(T) == typeof(ICollection<Cat>))  //if (parameter is ICollection<Cat>)
        {
            ICollection<Cat> cats = parameter as ICollection<Cat>;

            //Count your cats in count variable
            int count = cats.Count;
        }
        else
        if (typeof(T) == typeof(Cat))  // if (parameter is Cat)
        {
            Cat cat = parameter as Cat;

            //Get id of your cat in id variable
            int id = cat.Id;
        }
    }
}

您的TestMethod可以如下编写:

public static void TestMethod<T>(T parameter)
{
    // Check if it is a ICollection of any object
    if (parameter is ICollection && parameter.GetType().IsGenericType)
    {
        var itemCount = ((ICollection)parameter).Count;
    }
    else
    {
        // write your else logic
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM