IQueryable OfType gdzie T jest typem runtime
Muszę być w stanie uzyskać coś podobnego do następującego do pracy:
Type type = ??? // something decided at runtime with .GetType or typeof;
object[] entityList = context.Resources.OfType<type>().ToList();
Czy to możliwe? Jestem w stanie korzystać z.Net 4, Jeśli coś nowego w tym pozwala. 22
6 answers
Można to nazwać refleksją:
MethodInfo method = typeof(Queryable).GetMethod("OfType");
MethodInfo generic = method.MakeGenericMethod(new Type[]{ type });
// Use .NET 4 covariance
var result = (IEnumerable<object>) generic.Invoke
(null, new object[] { context.Resources });
object[] array = result.ToArray();
Alternatywą byłoby napisanie własnej metody ogólnej OfTypeAndToArray
do wykonania obu jej bitów, ale powyższa metoda powinna zadziałać.
36
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:23:05
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:23:05
Wygląda na to, że musisz użyć tutaj refleksji...
public static IEnumerable<object> DyamicOfType<T>(
this IQueryable<T> input, Type type)
{
var ofType = typeof(Queryable).GetMethod("OfType",
BindingFlags.Static | BindingFlags.Public);
var ofTypeT = ofType.MakeGenericMethod(type);
return (IEnumerable<object>) ofTypeT.Invoke(null, new object[] { input });
}
Type type = // ...;
var entityList = context.Resources.DynamicOfType(type).ToList();
8
Author: Timwi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:23:39
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:23:39
Wyłącznie na twoje pytanie Użyj "generyków", nie jest to niemożliwe.
Generics jest funkcją czasu kompilacji, a nie wykrywaniem środowiska uruchomieniowego. W przypadku trybu runtime należy użyć funkcji Reflection lub Dynamic.
0
Author: Aliostad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:24:18
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:24:18
O co chodzi ...
public static IList OfTypeToList(this IEnumerable source, Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
return
(IList) Activator.CreateInstance(
typeof(List<>)
.MakeGenericType(type),
typeof(System.Linq.Enumerable)
.GetMethod(nameof(System.Linq.Enumerable.OfType),
BindingFlags.Static | BindingFlags.Public)
.MakeGenericMethod(type)
.Invoke(null, new object[] { source }));
}
0
Author: Jörn von Holten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-27 11:18:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-27 11:18:41
Rozwiązaniem do obsługi wielu typów jest
public static IQueryable<TEntity> OfTypes<TEntity>(this DbSet<TEntity> query, IEnumerable<Type> types ) where TEntity : class
{
if( types.Count() == 0 ) return query;
var lambda = GetOfOnlyTypesPredicate( typeof(TEntity), types.ToArray() );
return query.OfType<TEntity>().Where( lambda as Expression<Func<TEntity,bool>>);
}
public static LambdaExpression GetOfOnlyTypesPredicate( Type baseType, Type[] allowed )
{
ParameterExpression param = Expression.Parameter( baseType, "typeonlyParam" );
Expression merged = Expression.TypeIs( param, allowed[0] );
for( int i = 1; i < allowed.Length; i++ )
merged = Expression.OrElse( merged, Expression.TypeIs( param, allowed[i] ));
return Expression.Lambda( merged, param );
0
Author: Drew Ogle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-11 16:06:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-11 16:06:00
object[] entityList = context.Resources
.Where(t=> t.GetType() == type)
.ToArray();
-1
Author: James Curran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:16:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 16:16:47