generics

Cannot call action method 'System.Web.Mvc.PartialViewResult Foo[T](T)' on controller 'Controller' because the action method is a generic method

Cannot call action method 'System.Web.Mvc.PartialViewResult FooT' on controller 'Controller' because the action method is a generic method <% Html.RenderAction("Foo", model = Model}); %> Is there a workaround for this limitation on ASP MVC 2? I would really prefer to use a generic. The workaround that I have come up with is to change...

C#.NET Generic Methods and Inheritance.

Is it possible to do the following with generics in C#.NET public abstract class A { public abstract T MethodB<T>(string s); } public class C: A { public override DateTime MethodB(string s) { } } i.e. have a generic method in a base class and then use a specific type for that method in a sub class. ...

Generic property- how to specify the type at run time

I was reading a question on making a generic property, but I'm a little confused by the last example from the first answer (I've included the relevant code below): You have to know the type at compile time. If you don't know the type at compile time then you must be storing it in an object, in which case you can add the follo...

C# compiler fails to recognize a class is implementing an interface

The following code fails to compile (using VS2010) and I don't see why. The compiler should be able to infer that List<TestClass> is 'compatible' (sorry for lack of a better word) with IEnumerable<ITest>, but somehow it doesn't. What am I missing here? interface ITest { void Test(); } class TestClass : ITest { public void Te...

C# Multiple constraints

I have an application with lots of generics and IoC. I have an interface like this: public interface IRepository<TType, TKeyType> : IRepo Then I have a bunch of tests for my different implementations of IRepository. Many of the objects have dependencies on other objects so for the purpose of testing I want to just grab one that is val...

Unity doesn't work

Yesterday I've implemented the code: CustomerProductManager productsManager = container.Resolve<CustomerProductManager>(); It was compilable and working. Today (probably I've modified something I am constantly getting the error: The non-generic method 'Microsoft.Practices.Unity.IUnityContainer.Resolve(System.Type, string, par...

Castle Windsor : Configure a generic service and provide a non-generic implementation fails

Using Castle Windsor, I want to configure a generic service with a type parameter; and have it implemented by a known concrete type that implements the service with a specific type as the generic parameter. Expressed as a unit test, I would like to get the following to work: [TestClass] public class WindsorTests { [TestMethod] p...

Java Generic Collection of Generic Type with Bounded Wildcard

Please help me with this: If Lion IS-A Animal and given Cage<T>: Cage<? extends Animal> c = new Cage<Lion>(); // ok, but Set<Cage<? extends Animal>> cc = new HashSet<Cage<Lion>>(); // not ok What I don't see here? ...

Java weird generic return type

Browsing through Guava libraries I saw this weird signature on a readLines method from Files class: public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) I know a little bit about generics in java, but this baffled me. What does the double T mean here...

How to create a generic method in C# that's all applicable to many types - ints, strings, doubles etc.

Let's I have a method to remove duplicates in an integer Array public int[] RemoveDuplicates(int[] elems) { HashSet<int> uniques = new HashSet<int>(); foreach (int item in elems) uniques.Add(item); elems = new int[uniques.Count]; int cnt = 0; foreach (var item in uniques) ...

Circular dependency with generics

I have defined the following interface: public interface IStateSpace<State, Action> where State : IState where Action : IAction<State, Action> // <-- this is the line that bothers me { void SetValueAt(State state, Action action); Action GetValueAt(State state); } Basically, an IStateSpace interface should be something like a c...

Where are the function address literals in c++?

UPDATE: After some additional reading, what I really wanted was guaranteed early binding (which should translated to an immediate call for non-virtual functions and non-PIC code), which can be done by passing a (member) function as a template parameter. The problem I had was that gcc < 4.5 and icc 11.1 can generate some funky instruction...

Generics and Exposing .Net Types For COM Consumers?

I remember seeing a question on my official MS 70-536 exam that talked about a simple class that was designed to be exposed for COM calling clients and etc. of all the members defined in the classes I chose the answer D. The one that used a generic. My question to you guys is this: If you were designing a .net custom type that was to be...

What must be done to use the value of a reference type as a dictionary key?

Suppose I have a class T that I want to use as a key in a Dictionary<T,U> collection. What must I implement in T so that these keys are based on values of T rather than T references? I'm hoping it's just GetHashCode(). ...

C# Get Type of IEnumerable<TModel>

I have a method to which I pass an IEnumerable<TModel>. Then depending on the type of TModel, the method carries out a set of instructions as below: public void MyMethod<TModel>(IEnumerable<TModel> items) where TModel : class { int operationType; switch (typeof(TModel)) { case typeof(MyModelOn...

C# Access the Properties of a Generic Object

I have a method that counts the number of Contacts each Supplier, Customer and Manufacturer has (this is a scenario to try make explaining easier!) The models are all created by Linq to SQL classes. Each Supplier, Customer and Manufacturer may have one or more Contacts public int CountContacts<TModel>(TModel entity) where TModel : clas...

Delphi Superobject, generic list to json

I have a object with some TObjectList<>-fields that I try to encode as JSON with help form SuperObject. TLogs = TObjectList<TLog>; TMyObject = class(TObject) private FLogs: TLogs; end; Deep inside SuperObjects code, there is a ToClass procedure, iterating the fields and add them to the json result. In this loop, there is a check on...

What are some good java interview questions and answers regarding generics and annotations?

What are some good java interview questions and answers regarding generics and annotations? ...

What is the advantage of using constraints on C# Generics

Can someone tell me what the difference is between the following public class CarCollection<T>:List<T> where T:Car{} and public class CarCollection:List<Car>{} To me they seem to do the same thing, create type-safe collection of "Car" objects. ...

Generics with constraints hierarchy

I am currently facing a very disturbing problem: interface IStateSpace<Position, Value> where Position : IPosition // <-- Problem starts here where Value : IValue // <-- and here as I don't { // know how to get away this // circular...