ยังไงเพื่อตัวกรองรายการจากชุดสะสมจากพื้นฐานประเภทถูกจัดเก็บไว้ในตัวแปร

0

คำถาม

ฉันต้องตามลำดับชั้นในผังต้นไม้:

class Animal

class Dog : Animal

class Cat : Animal

ฉันมี List<Animal> คลังภาพแล้วอยากให้เป็นวิธีการที่จะกลับมาเอาแมวหรือทุกอย่างหมา แต่ฉันไม่สามารถหาวิธีทำให้ตัวกรองรายการส่วนประกอบจากพื้นฐานรูปแบบตัวแปร ดังนั้นเหมือนนี้:

int AnimalsOfType(Type animalType)
{
    // Gives error "animalType is a variable but is used like a type".
    return animals.OfType<animalType>().Count;
}
c# casting inheritance list
2021-11-21 02:30:01
2

คำตอบที่ดีที่สุด

0
using System.Linq;

int AnimalsOfType(Type animalType)
{
    return animals.Count(a => a.GetType() == animalType);
}
2021-11-21 05:33:50

ขอบคุณแต่ทำไมถึงไม่สามารถเป็นตัวแปร ot ประเภท Type ถูกใช้เป็นประเภท?
K-RUSHer

คุณไม่สามารถส่งตัวแปรประเภท System.Type เป็นพารามิเตอร์ทั่วไปจะเป็นทั่วไปฟังก์ชันโดยตรง เหตุผลคือ:ทั่วไปพารามิเตอร์เป็นตัวแทนตอนที่รหัสคือถูกคอมไพล์โดยใช้ อ่านคำตอบซึ่งอธิบายอีก
Ibram Reda

จะมีอะไรเพิ่มมากขึ้นที่จะประยุกต์@item text character set animals.Count(a => a.GetType() == animalType);
Sarin

@เซรินใช่คุณพูดถูกผมแก้ไขมัน
Ibram Reda
0

คนที่มีประสิทธิภาพสูงสุดเท่าที่วิธีการอยู่ที่นี่คือที่จะใช้ MakeGenericMethod แล้ว CreateDelegate เพื่อสร้าง delegates ต้องทั่วไปวิธีการของ. คุณสามารถแคชของพวกนี้ delegates อยู่ในพจนานุกรม

static Dictionary<Type, Func<List<Animal>, int>> _methods = new Dictionary<Type, Func<List<Animal>, int>>();

static int CountOfType<T>(List<Animal> source) =>
    source.Count(a => a is T);  
    
int AnimalsOfType(List<Animal> animals, Type animalType)
{
    if(!_methods.TryGetValue(animalType, out var dlgt))
    {
        dlgt = (Func<List<Animal>, int>)
             this.GetType().GetMethod("CountOfType")
                  .MakeGenericMethod(animalType)
                  .CreateDelegate(typeof(Func<List<Animal>, int>)));
        _methods[animalType] = dlgt;
    }
    return dlgt(animals);
}

มีนัดเดียวเล็กๆเมื่อเริ่มการทำงานราคาครั้งแรกที่คุณโทรหาวิธีการนี้,ต่อประเภทนี้ได้นะ

2021-11-21 03:01:30

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่