Generic list of generic objects

Yes, generics is a good choice. The key to achieving type-safety (and being identify the type with the Type property is to add an abstraction between the list and Field<T> class. Have Field<T> implement the interface IField. This interface doesn’t need any members. Then declare your list as being List<IField>. That way you constrain the … Read more

Help with Linq and Generics. Using GetValue inside a Query

You need to convert the code to an expression tree. using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { using (var context = new NorthwindEntities()) { IQueryable<Customer> query = context.Customers; query = Simplified<Customer>(query, “CustomerID”, “ALFKI”); var list = query.ToList(); } } static IQueryable<T> Simplified<T>(IQueryable<T> … Read more

How to write this EF Mock setup code as a reusable Generic Boilerplate?

Tim Larson already offered a great solution for this boilerplate code in his blog: public static class DbSetMocking { private static Mock<DbSet<T>> CreateMockSet<T>(IQueryable<T> data) where T : class { var queryableData = data.AsQueryable(); var mockSet = new Mock<DbSet<T>>(); mockSet.As<IQueryable<T>>().Setup(m => m.Provider) .Returns(queryableData.Provider); mockSet.As<IQueryable<T>>().Setup(m => m.Expression) .Returns(queryableData.Expression); mockSet.As<IQueryable<T>>().Setup(m => m.ElementType) .Returns(queryableData.ElementType); mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()) .Returns(queryableData.GetEnumerator()); return … Read more

Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

Unfortunately, generic Swift classes are not visible to Objective-C code and also are not supported in Interface Builder (in storyboards and xibs). I find these two points closely related. As a solution I would suggest you to use aggregation: do not make you view controller generic, but extract some logic to another (generic) class and … Read more

Consumer mapped Class in HashMap

This is essentially just like the type-safe heterogeneous container described by Joshua Bloch, except you can’t use the Class to cast the result. Weirdly, I can’t find a great example existing on SO, so here is one: package mcve; import java.util.*; import java.util.function.*; class ClassToConsumerMap { private final Map<Class<?>, Consumer<?>> map = new HashMap<>(); @SuppressWarnings(“unchecked”) … Read more

Why does this generic java method accept two objects of different type?

T is inferred to be Object, and both arguments are getting implicitly upcast. Thus the code is equivalent to: Main.<Object>random((Object)”string1″, (Object)new Integer(10)); What may be even more surprising is that the following compiles: random(“string1”, 10); The second argument is getting auto-boxed into an Integer, and then both arguments are getting upcast to Object.