猿问

如何获取泛型参数的类型

我无法确定应用程序中通用参数的类型。情况就像下面的代码。当我得到一个泛型时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)parameter).Count;


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


非常感谢所有的答案。


繁星点点滴滴
浏览 180回答 3
3回答

青春有我

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

大话西游666

这不是您想要使用泛型的情况,因为这不是泛型的目的。你在这里有两个选择。要么你做两个这样的方法:public void TestMethod(Cat cat) {...}public void TestMethod(ICollection<Cat> cats) {...}或者,如果您确实需要这种通用方法,则使用对象作为参数。&nbsp;public void TestMethod(object obj)&nbsp;&nbsp;{&nbsp; &nbsp; Cat cat = obj as cat;&nbsp; &nbsp; if(cat != null)&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; ICollection<Cat> cats = obj as ICollection<Cat>;&nbsp; &nbsp; if(cats != null)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp;}但即便如此,如果您使用反射,这只是一个好主意。

SMILET

试试这个可能对你有帮助class Cat{&nbsp; &nbsp; public int Id { get; set; }}class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Cat cat1 = new Cat { Id = 1 };&nbsp; &nbsp; &nbsp; &nbsp; Cat cat2 = new Cat { Id = 2 };&nbsp; &nbsp; &nbsp; &nbsp; ICollection<Cat> cats = new List<Cat>();&nbsp; &nbsp; &nbsp; &nbsp; cats.Add(cat1);&nbsp; &nbsp; &nbsp; &nbsp; cats.Add(cat2);&nbsp; &nbsp; &nbsp; &nbsp; TestMethod<ICollection<Cat>>(cats);&nbsp; &nbsp; &nbsp; &nbsp; TestMethod<Cat>(cat1);&nbsp; &nbsp; }&nbsp; &nbsp; public static void TestMethod<T>(T parameter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (typeof(T) == typeof(ICollection<Cat>))&nbsp; //if (parameter is ICollection<Cat>)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ICollection<Cat> cats = parameter as ICollection<Cat>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Count your cats in count variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = cats.Count;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; if (typeof(T) == typeof(Cat))&nbsp; // if (parameter is Cat)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cat cat = parameter as Cat;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Get id of your cat in id variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int id = cat.Id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答