How can I write a generic container class that implements a given interface in C#?

This isn’t pretty, but it seems to work: public static class GroupGenerator { public static T Create<T>(IEnumerable<T> items) where T : class { return (T)Activator.CreateInstance(Cache<T>.Type, items); } private static class Cache<T> where T : class { internal static readonly Type Type; static Cache() { if (!typeof(T).IsInterface) { throw new InvalidOperationException(typeof(T).Name + ” is not an … Read more

Implementations of interface through Reflection

The answer is this; it searches through the entire application domain — that is, every assembly currently loaded by your application. /// <summary> /// Returns all types in the current AppDomain implementing the interface or inheriting the type. /// </summary> public static IEnumerable<Type> TypesImplementingInterface(Type desiredType) { return AppDomain .CurrentDomain .GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => … Read more

Java Interface Usage Guidelines — Are getters and setters in an interface bad?

I don’t see why an interface can’t define getters and setters. For instance, List.size() is effectively a getter. The interface must define the behaviour rather than the implementation though – it can’t say how you’ll handle the state, but it can insist that you can get it and set it. Collection interfaces are all about … Read more

What is a static interface in java?

I’m curious about the case when it’s not an inner interface. The static modifier is only allowed on a nested classes or interfaces. In your example Entry is nested inside the Map interface. For interfaces, the static modifier is actually optional. The distinction makes no sense for interfaces since they contain no code that could … Read more

Where to put @Transactional? In interface specification or implementation? [duplicate]

It really all depends on your application architecture, in my opinion. It depends on how you are proxying your classes. If you have your app set to proxy-target-class=”true” (in your application context, then your @Transactional information wont be picked up if you annotate the Interface. Check out The Spring Docs — “Tips” for more information. … Read more