c# - Comparing generics: A "master-type" IEnumerable<> that is generic, but matches all specific types (IEnumerable<int>, IEnumerable<object>, ...)? -
i want to compare typeof(ienumerable<>) types of various specific classes of ienumerable, e.g.
compare(typeof(ienumerable<>), typeof(ienumerable<object>)) // should return true compare(typeof(ienumerable<>), typeof(ienumerable<int>)) // should return true compare(typeof(ienumerable<>), typeof(ienumerable<myclass>)) // should return true compare(typeof(ienumerable<>), typeof(ienumerable)) // should return false because ienumerable not generic ienumerable<> type
how can this? common methods such == or isassignablefrom return false above examples.
probably not necessary question, background:
i'm writing conversion class convers object other type. i'm using attributes (xlconverts):
public class xlconvertsattribute : attribute { public type converts; public type to; }
to flag type each methods converts into. 1 of conversion methods converts object ienumerable:
[xlconverts(converts = typeof(object), = typeof(ienumerable<>))] public static ienumerable<t> toienumerable<t>(object input) { // .... }
then have more general method
public static object convert(object input, type totype) { // ... }
which uses reflection method has xlconverts.to == totype, reflects own class find approrpaite conversion method given desired target type.
now when call convert(input, typeof(ienumerable)), supposed find method toienumerable reflection. can flag [xlconverts(to = typeof(ienumerable<>)), , ienumerable<> not ienumerable, won't find method.
i'm aware using ienumerable instead of ienumerable<> job here, explicitly need use generic ienumerable<> because later on, want further reflection , filter out methods convert generic type.
thanks!
public static bool compare(type generictype, type t) { return t.isgenerictype && t.getgenerictypedefinition() == generictype; }
Comments
Post a Comment