generics

How to use same interface two times with diferrent template parameters, in an interface?

I Think it would be more clearer with this example. We Want to see two methods with diferrent parameters in the processor class. "int Process (int value);" "double Process (double value);" But compiler says for IRoot : 'Generics.IRoot' cannot implement both 'Generics.IProcess' and 'Generics.IProcess' because they may unify for some typ...

C# Wrong conversion using Convert.ChangeType()

Hello! I am using Convert.ChangeType() to convert from Object (which I get from DataBase) to a generic type T. The code looks like this: T element = (T)Convert.ChangeType(obj, typeof(T)); return element; and this works great most of the time, however I have discovered that if I try to cast something as simple as return of the followi...

Custom Java Iterator with type confusion

I have a generic class which bundles an Object and an order: public class OrderedObject<T> { private int order; private T object; public OrderedObject(int order, T object) { this.order = order; this.object = object; } public int getOrder() { return order; } public T getObject() { ...

Generics-Get and Put rule

I have read O'Reilly book, in that i came to know this GET and PUT rule Use an extends wildcard when you only get values out of a structure, use a super wildcard when you only put values into a structure, and don't use a wildcard when you both get and put. exception is: *)You cannot put anything into a type declared with an EXT...

Pass Type dynamically to <T>

See i have a situation like this... object myRoledata = List<Roles>() --> (some list or Ienumerable type) Now i have a generic method which creates an XML object from List<T> - Something like this.. public string GetXML<T>(object listdata) { List<T> objLists = (List<T>)Convert.ChangeType(listData, typeof(List<T>)); for...

Inferred wildcard generics in return type

Java can often infer generics based on the arguments (and even on the return type, in contrast to e.g. C#). Case in point: I've got a generic class Pair<T1, T2> which just stores a pair of values and can be used in the following way: Pair<String, String> pair = Pair.of("Hello", "World"); The method of looks just like this: public st...

How To Expose a DIctionary<> to COM Interop

I have an Interface that is defined something along these lines: Interface foo { int someProperty {get; set;} Dictionary<string, object> Items; } The concrete class that implements this interface needs to be registered for COM Interop. Everything compiles and the assemblies seem to register OK, but then when trying to create t...

Protobuf attributes with a hierarchy of generic classes

I have a class hierarchy that looks like this. These classes contain a lot of other details which I have excluded. This is a simplification to focus on the serialization aspect of these classes. [ProtoInclude(1, typeof(Query<bool>))] [ProtoInclude(2, typeof(Query<string>))] [ProtoInclude(3, typeof(Query<int>))] [ProtoInclude(4, typeof...

Type parameters vs. generics

Is there ever a reason to use Type parameters over generics, ie: // this... void Foo(Type T); // ...over this. void Foo<T>(); It seems to me that generics are far more useful in that they provide generic constraints and with C# 4.0, contravarience and covarience, as well as probably some other features I don't know about. It would see...

.NET Generic List<T> Problem, works by design, need work around. Add value instead of reference

I have a List<T> and I need to avoid the behavior I'm about to outline: // assume cls and numberToAdd are parameters passed in. int pos = numberToAdd; List<MyClass> objs = new List<MyClass>(numberToAdd); for(int i = 0; i < numberToAdd; i++) { objs.Add(cls); objs[i].X = -((pos * cls.Width) + cls.Width...

How to make a Java class that implements one interface with two generic types?

I have a generic interface public interface Consumer<E> { public void consume(E e); } I have a class that consumes two types of objects, so I would like to do something like: public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple> { public void consume(Tomato t) { ..... } public void consume(Apple a) { ...

Benefit of Generic Constructors

What is the benefit of having generic constructor for a non-generic class? Java spec permits the following: class NonGeneric { <T> NonGeneric() { } ... NonGeneric ref = new <String> NonGeneric(); } Can one come up with a realistic example of when it enhances the typesafety of the class? How would it be better than using Generi...

Accessing a Java generics stored in a Map

Hello I'm trying to access a custom Java generic stored in a map as below. Unfortunately I get a type mis-match error. Now, I can cast it to the type I want because but this seems messy to me. Is there a clean way of doing the assignment? Thanks public interface BusinessObject { } public class SalesItemA implements BusinessObject { }...

java generics design problem (state machine)

i made a state machine and would like it to take advantage of generics in java. currently i dont see the way i can make this work and get pretty looking code. im sure this design problem has been approached many times before, and im looking for some input. heres a rough outline. class State { ... } only one copy of each distinct st...

C#-Generics -ISerializable,IEnumerable,IList -efficient application

I need simple example to use ISerializable,IEnumerable,IList with Generics efficiently. Also wish to know what are all the other Interfaces we can use along with Generics. Update : The task i need to perform is using these interfaces I have to serialize the custom Types Collect them in Generic object Iterate them to find the ma...

Java Generics?

Over the years I seen many people use the word "generics", I honestly have not a clue what it means, whatever it is I most likely use it but just don't know that it was called that. :p ...

Prefer Inheritence or Containment when creating specialized collections?

I have a need for specialized collection classes. Let's call them FooItems and BarItems. I basically need all the functionality of a List, but I need to do some extra work when new items are added or removed from the collection. My first stab at this was to simply derive from List and List, then create my own Add and Remove methods....

Can I use StructureMap to return all implementations of a generic interface for a specific type parameter

I have a generic interface, IValidator. I want to be able to use StructureMap to retrieve a list of all classes that implement IValidator for a given type T. For example, var PersonValidators = ObjectFactory.GetAllInstances<IValidator<Person>>(); var AddressValidators = ObjectFactory.GetAllInstances<IValidator<Address>>(); I know how...

Generics with Fluent and NHibernate

I am trying to create a messaging system between users and organizations - that is, a user can send and recieve messages from users or organizations. So, to support this system, I started with an interface: public interface IPassableObject<F, T, O> where F : DomainObject where T : DomainObject { Guid PassedItemId { get; se...

Using generic Dictionary over wcf: what do I need to look for?

Suppose I have a WCF service and a method in the contract <ServiceContract()> _ Interface IThingService '... <OperationContract()> _ Function GetThing(thingId As Guid) As Thing End Interface where Thing is an ordinary class with ordinary properties, except for one member: Public Class Thing ' ... Public Property Photos() As ...