सरल कोड जो मुझे List<int>
की जेनेरिक टाइप टाइप परिभाषा ICollection<>
का सामान्य इंटरफ़ेस रखने की अपेक्षा करता है। फिर भी मैं List<int>
से एक स्वीकार्य प्रकार प्राप्त नहीं कर सकता जो मुझे उनकी तुलना ठीक से करने की अनुमति देता है।इंटरफेस के जेनेरिक टाइप टाइप परिभाषा की तुलना
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public static void Main()
{
var a = typeof(List<int>);
var b = typeof(ICollection<>);
var r1 = a.GetGenericTypeDefinition().GetInterfaces();
foreach (var x in r1)
{
Console.WriteLine(x);
}
Console.WriteLine();
Console.WriteLine(b);
Console.WriteLine();
Console.WriteLine(r1.Any(x => x == b));
}
}
आउटपुट
System.Collections.Generic.IEnumerable`1[T]
System.Collections.Generic.IReadOnlyList`1[T]
System.Collections.Generic.IReadOnlyCollection`1[T]
System.Collections.IEnumerable
System.Collections.Generic.IList`1[T]
System.Collections.Generic.ICollection`1[T]
System.Collections.ICollection
System.Collections.IList
System.Collections.Generic.ICollection`1[T]
False
मैं उम्मीद है कि r1
एक प्रकार है कि b
के बराबर था निहित होगा।
संपादित
फिक्स्ड, जॉन स्कीट मुझे क्या हो रहा था में सही जानकारी दी।
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public static void Main()
{
var a = typeof(List<int>);
var b = typeof(ICollection<>);
var r1 = a.GetInterfaces()
.Where(x => x.IsGenericType)
.Select(x => x.GetGenericTypeDefinition());
foreach (var x in r1)
{
Console.WriteLine(x);
}
Console.WriteLine();
Console.WriteLine(b);
Console.WriteLine();
Console.WriteLine(r1.Contains(b));
}
}
आउटपुट
System.Collections.Generic.IEnumerable`1[T]
System.Collections.Generic.IReadOnlyList`1[T]
System.Collections.Generic.IReadOnlyCollection`1[T]
System.Collections.Generic.ICollection`1[T]
System.Collections.Generic.IList`1[T]
System.Collections.Generic.ICollection`1[T]
True
धन्यवाद, आपने मुझे वह अंतर्दृष्टि दी जिसकी मुझे आवश्यकता थी। –