第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何獲取泛型參數(shù)的類型

如何獲取泛型參數(shù)的類型

C#
繁星點(diǎn)點(diǎn)滴滴 2021-11-14 10:11:59
我無(wú)法確定應(yīng)用程序中通用參數(shù)的類型。情況就像下面的代碼。當(dāng)我得到一個(gè)泛型時(shí)ICollection,我需要計(jì)數(shù)。如果沒(méi)有,我需要處理單個(gè)對(duì)象。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?        }    }}我提錯(cuò)了問(wèn)題,它可以是貓、狗、老鼠或其他任何東西。我不知道它是什么,我也不需要。我正在嘗試下面的代碼并遇到鑄造錯(cuò)誤。((ICollection)parameter).Count;如果它是任何對(duì)象的 ICollection,我只需要計(jì)數(shù)。非常感謝所有的答案。
查看完整描述

3 回答

?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

   int result;

        if (parameter is ICollection<Cat>)

            result = (parameter as (ICollection<Cat>)).Count;

        else if (parameter is Cat)

            result = (parameter as Cat).Id;


查看完整回答
反對(duì) 回復(fù) 2021-11-14
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

這不是您想要使用泛型的情況,因?yàn)檫@不是泛型的目的。


你在這里有兩個(gè)選擇。


要么你做兩個(gè)這樣的方法:


public void TestMethod(Cat cat) {...}

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

或者,如果您確實(shí)需要這種通用方法,則使用對(duì)象作為參數(shù)。


 public void TestMethod(object obj) 

 {

    Cat cat = obj as cat;

    if(cat != null)

    { 


        return;

    }

    ICollection<Cat> cats = obj as ICollection<Cat>;

    if(cats != null)

    {


    }

 }

但即便如此,如果您使用反射,這只是一個(gè)好主意。


查看完整回答
反對(duì) 回復(fù) 2021-11-14
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

試試這個(gè)可能對(duì)你有幫助


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;

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-14
  • 3 回答
  • 0 關(guān)注
  • 206 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)