Skip to main content
added another suggestion for getting around System.Type/generics issue
Source Link
Dan Tao
  • 128.9k
  • 57
  • 309
  • 451

How about an extension method?

public static bool HasAny(this IEnumerable source, Type type) { foreach (object item in source) if (item != null && item.GetType().Equals(type)) return true; return false; } 

Usage:

bool hasDataType1 = myList.HasAny(typeof(MyObject<datatype1>)); 

Note that if you don't want to have to type out typeof(...) -- i.e., if you basically want your Exist method to only care about objects of type MyObject<T>, I'd go with something like SLaks's answer:

public static bool Exist<T>(this IEnumerable source) { return source.OfType<MyObject<T>>().Any(); } 

Also, SLaks is right that you really can't have a List<MyObject<object>> that's full of anything other than objects of type MyObject<object> or some derived class (and MyObject<datatype1>, etc. do not derive from MyObject<object> -- generics don't work that way).

Another way I might suggest to work around the whole "you can't get the type of a generic class using a System.Type object without using reflection" issue would be this: Make your MyObject<T> implement a non-generic interface, like this:

public interface IMyObject { Type DataType { get; } } public class MyObject<T> : IMyObject<T>, IMyObject { public Type DataType { get { return typeof(T); } } } 

Then your list could be a List<IMyObject> (the non-generic interface) and your Exist method could look like this:

public static bool Exist<T>(this IEnumerable source, Type type) { return source.OfType<IMyObject>().Any(x => x.DataType.Equals(type)); } 

How about an extension method?

public static bool HasAny(this IEnumerable source, Type type) { foreach (object item in source) if (item != null && item.GetType().Equals(type)) return true; return false; } 

Usage:

bool hasDataType1 = myList.HasAny(typeof(MyObject<datatype1>)); 

Note that if you don't want to have to type out typeof(...) -- i.e., if you basically want your Exist method to only care about objects of type MyObject<T>, I'd go with something like SLaks's answer:

public static bool Exist<T>(this IEnumerable source) { return source.OfType<MyObject<T>>().Any(); } 

Also, SLaks is right that you really can't have a List<MyObject<object>> that's full of anything other than objects of type MyObject<object> or some derived class (and MyObject<datatype1>, etc. do not derive from MyObject<object> -- generics don't work that way).

How about an extension method?

public static bool HasAny(this IEnumerable source, Type type) { foreach (object item in source) if (item != null && item.GetType().Equals(type)) return true; return false; } 

Usage:

bool hasDataType1 = myList.HasAny(typeof(MyObject<datatype1>)); 

Note that if you don't want to have to type out typeof(...) -- i.e., if you basically want your Exist method to only care about objects of type MyObject<T>, I'd go with something like SLaks's answer:

public static bool Exist<T>(this IEnumerable source) { return source.OfType<MyObject<T>>().Any(); } 

Also, SLaks is right that you really can't have a List<MyObject<object>> that's full of anything other than objects of type MyObject<object> or some derived class (and MyObject<datatype1>, etc. do not derive from MyObject<object> -- generics don't work that way).

Another way I might suggest to work around the whole "you can't get the type of a generic class using a System.Type object without using reflection" issue would be this: Make your MyObject<T> implement a non-generic interface, like this:

public interface IMyObject { Type DataType { get; } } public class MyObject<T> : IMyObject<T>, IMyObject { public Type DataType { get { return typeof(T); } } } 

Then your list could be a List<IMyObject> (the non-generic interface) and your Exist method could look like this:

public static bool Exist<T>(this IEnumerable source, Type type) { return source.OfType<IMyObject>().Any(x => x.DataType.Equals(type)); } 
Source Link
Dan Tao
  • 128.9k
  • 57
  • 309
  • 451

How about an extension method?

public static bool HasAny(this IEnumerable source, Type type) { foreach (object item in source) if (item != null && item.GetType().Equals(type)) return true; return false; } 

Usage:

bool hasDataType1 = myList.HasAny(typeof(MyObject<datatype1>)); 

Note that if you don't want to have to type out typeof(...) -- i.e., if you basically want your Exist method to only care about objects of type MyObject<T>, I'd go with something like SLaks's answer:

public static bool Exist<T>(this IEnumerable source) { return source.OfType<MyObject<T>>().Any(); } 

Also, SLaks is right that you really can't have a List<MyObject<object>> that's full of anything other than objects of type MyObject<object> or some derived class (and MyObject<datatype1>, etc. do not derive from MyObject<object> -- generics don't work that way).