generics

C# Generics Refactoring

Hi All, I am trying to refactor a piece of code which seems easily refactorable but is proving difficult. There are two method which seem very similar and I feel should be refactored:- public class MyClass { private void AddBasicData(Receiver receiver) { var aHelper = new AHelper(); var bHelper = new BHelper();...

C# generic cache with type safe

Hello, I'm looking for a way to have a generic local cache for any object. Here is the code : private static readonly Dictionary<Type,Dictionary<string,object>> _cache = new Dictionary<Type, Dictionary<string, object>>(); //The generic parameter allow null values to be cached private static void AddToCache<T>(stri...

C# Assign a delegate based on a type - refactor help?

Hi All, I had a quick question. Something seems really wrong with this code. I want to take advantage of generics and delegates and quite possibly generic delegates if applicable. I am working with some code generated api's and the objects generated are very similiar. I see that they all implement an interface so I was to try to create o...

c# Generics help - how to pass params to new T()

Possible Duplicate: Create instance of generic type? How can I pass a param to a generic contstructor? public class Payment<T> where T: HostFunctionContext, IClaimPayment, new() { public IResultEntity Display(MyUser user, string claim, int? cert) { **HostFunctionContext func = new T(user) as HostFunctionCon...

Is it possible to have a base class with a return type that adjusts to the type of the base class?

Basically my setting is this: public abstract class BaseObject{ public abstract BaseObject Clone(); } public class DerivedObject : BaseObject{ public DerivedObject Clone() { //Clone logic } } The above code doesn't compile because it isn't possible to change the return type when overriding a method. Is it po...

Java Generics for C# Gurus

Are there any good quick reference guides out there (preferably online) for Java Generics from the perspective of someone with complete understanding of C# Generics? I find that the syntax is so similar that I expect it to just work and then I run into unfamiliar Java syntax like this: Class<?> foo; which I thought was similar to the ...

Dictionary of Action<T> Delegates

I have object XML serialized messages coming into a class called MessageRouter. The XML contains the Type name it it was serialized from, and I need to be able to invoke different delegate methods depending on the type that are not known until runtime. I'm not extremely strong in generics so hopefully this will make sense to someone... ...

Problem with Windsor Calls to "Genericized" Methods After Adding an Interceptor

I have an application that was working fine that uses Windsor for IoC. I want to log the method calls, parameters, and execution time of all calls made to components instantiated by Windsor, so I implemented a LoggingInterceptor that implements IInterceptor, that contains: Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); ...

generic non-invasive cache wrapper

I'm trying create a class which adds functionality to a generic class, without directly interfacing with the wrapped class. A good example of this would be a smart pointer. Specifically, I'd like to create a wrapper which caches all the i/o for one (or any?) method invoked through the wrapper. Ideally, the cache wrapper have the follo...

Interface declaration together with generic constraints

Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T> where T : Ad { //... } How do I specify that BrandQuery implements an interface, say IDisposable ? This is obviously the wrong way: public class BrandQuery<T> : Query<T> where T : Ad, IDisposable { //... } because that would only put a generi...

Java circular Generics

I'm having a problem understanding Java generics and I've simplified to this example class A<T extends B> { public void fun(T t) { } } class B { A a; public void event() { a.fun(this); } } The problem is that this generates a warning because A is defined inside B but A is already using it as a generic typ...

Why has my generic method stopped working?

In my project I have a factory method that loads an object that implements an interface. You pass in the class you desire and receive an instantiation of it, like so. public class Factory { public static <E extends SomeInterface> E load( Class<E> clss ) throws Exception { return clss.newInstance(); } } You could invoke...

Efficient way to get a range of Keys from Dictionary

I have a Dictionary that for most operations I just need to retrieve a single entry by key, but for a few operations I will need to work with the entries associated with a range of keys. The way that occurs to me to do this is to use GetKeys and a FindAll that will match the range I am interested in, but was wondering if someone could su...

C# Generic interface

I need to pass a generic type parameter to an interface. I have a string with the name of the type. I have something like this: string type = "ClassType"; Type t = Type.GetType("ClassType"); IProvider<t> provider = (IProvider<t>)someObject; This doesn't work for me. What is the correct way to do it? Thanks. ...

Generic classes with shared static fields in C#

I have a generic class definition similar to this: public sealed class MyClass<TProperty,TOwner> { ... } Now I'd like any instances of MyClass<TProperty,TOwner> regardless of the types of TProperty or TOwner to share a Hashtable. I thought of creating an internal MyClassBase with a protected internal static field of type Hashtable...

Is it possible to extend a generic class parameter in Java?

Does something like this make any sense at all in Java? class A<T extends B> extends T{ int fun1() { .... } } abstract class B extends X implements C { } interface C { int fun1(); } I want class B to extend both X and C ideally. But since there is no multiple inheritance in Java, I'm trying to come up with a slicke...

IList using covariance and contravariance in c#, is this possible?

Hi all would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could us...

Polymorphically convert Java enum values into a list of strings

I have a handful of helper methods that convert enum values into a list of strings suitable for display by an HTML <select> element. I was wondering if it's possible to refactor these into a single polymorphic method. This is an example of one of my existing methods: /** * Gets the list of available colours. * * @return the list o...

Inference from Generic Type Question.

I suppose this is more of a public rant, but why can't I get c# to infer my Id's type? public EntityT Get<EntityT>(IdT id) where EntityT : EntityObject<IdT> and a defined EntityObject with a Guid as an Id as follows: public Foo : EntityObject<Guid> Inheriting from the abstract EntityObject class defined as follows: public abstract...

How to shuffle a List<T>

I found this piece of Java code at Wikipedia that is supposed to shuffle an array in place: public static void shuffle (int[] array) { Random rng = new Random(); int n = array.length; while (n > 1) { n--; int k = rng.nextInt(n + 1); int tmp = array[k]; array[k] = array[n]; array...