generic

Creating generic list of instances of a class.

I have several projects where I build a dictionary from a small class. I'm using C# 2008, Visual studio 2008 and .net 3.5 This is the code: namespace ReportsTest { class Junk { public static Dictionary<string, string> getPlatKeys() { Dictionary<string, string> retPlatKeys = new Dictionary<string, stri...

In C#: How to declare a generic Dictionary with a type as key and an enumeration of that type as value?

Hi all, I want to declare a dictionary that stores typed IEnumerable's of a specific type, with that exact type as key, like so: (Edited to follow johny g's comment) private IDictionary<Type, IEnumerable<T>> _dataOfType where T: BaseClass; //does not compile! The concrete classes I want to store, all derive from BaseClass, therefore ...

Generic TryParse

I am trying to create a generic extension that uses 'TryParse' to check if a string is a given type: public static bool Is<T>(this string input) { T notUsed; return T.TryParse(input, out notUsed); } this won't compile as it cannot resolve symbol 'TryParse' As I understand, 'TryParse' is not part of any interface. Is this pos...

generic identifier

Hi, as I probably do not describe the problem in the right terms, I was not able to get an answer with google. Please excuse! In the following code, I would like to replace 'hardcoded' identifier COMMENT with the variable editedField. How to do that? var editedField:String = event.dataField; if (model.multipleProcessingData[i][editedI...

Using Accelerometer in Wiimote for Physics Practicals

I have to develop some software in my school to utilize the accelerometer in the Wiimote for recording data from experiments, for example finding the acceleration and velocity of a moving object. I understand how the accelerometer values will be used but I am sort of stuck on the programming front. There is a set of things that I would l...

Generic Interface with method pointer

So, first I have my command property interface public interface ICommandProperty<T, U> { Func<T> CreateCommand { get; set; } Func<T, U> ParseResponse { get; set; } } The idea is that I can create a simple parser that takes a string and returns an IPAddress for example. This interface is then used in another interface: public...

Generic C# Class: Set "Generic" Property

I'm quite new to C#, so I might have a problem that C# has a simple solution for. I have a generic class with a property of "generic" type. I want to have a function to set that property, but I need to convert it to do so. public class BIWebServiceResult<T> { public T Data; public delegate StatusCode StringToStatusCode(string I...

Logic: Best way to sample & count bytes of a 100MB+ file

Let's say I have this 170mb file (roughly 180 million bytes). What I need to do is to create a table that lists: all 4096 byte combinations found [column 'bytes'], and the number of times each byte combination appeared in it [column 'occurrences'] Assume two things: I can save data very fast, but I can update my saved data very slo...

Problem resolving a generic Repository with Entity Framework and Castle Windsor Container

Hi, im working in a generic repository implementarion with EF v4, the repository must be resolved by Windsor Container. First the interface public interface IRepository<T> { void Add(T entity); void Delete(T entity); T Find(int key) } Then a concrete class implements the interface public class Repository<T> : IR...

Deferred execution of List<T> using Linq

Say I have a List<T> with 1000 items in it. Im then passing this to a method that filters this List. As it drops through the various cases (for example there could be 50), List<T> may have upto 50 various Linq Where() operations performed on it. Im interested in this running as quickly as possible. Therefore, i dont want this List<T> f...

[Django] models.Model class member not appearing in model_instance._meta.fields

Hi, I have a django.contrib.contenttypes.generic.genericForeignKeyField as a member of my model, however, it is not appearing when I instantiate the model and then try to get the fields out of the _meta of the object. e.g: class A(models.Model): field2 = models.IntegerField(...) field1 = generic.genericForeignKeyField() a...

How to perform a generic insert in entity framework?

I want to perform a generic insert in entity framework. This is what I wrote - static public void Insert<T>(MyEntities DataContext, T obj) where T : class { try { DataContext.AddObject(DataContext,obj); DataContext.SaveChanges(); } catch (Exception e...

Why is one class valid while the other is not?

As you can see, having a non void return type is important. class TestValid { public String f(List<String> list) { return null; } public Integer f(List<Integer> list) { return null; } public void test() { f(Arrays.asList("asdf")); f(Arrays.asList(123)); } } class TestInvalid { public void f(List<String> list) { ...

Conditional logic on Generic Type Constraints

I'm trying to write an automocking extension of Unity. While it would be much easier to use a Windsor subdependency resolver and RhinoMocks I'm forced by the business to use Unity and Moq. I haven't found an existing solution that uses Moq and I've found out why. Moq can't generate mocks from just a Type parameter, which makes Unity ex...

Get request generic type in method

I have a method which returns a generic type, is there a way to retrieve the value of <T> without having to give this by parameters? public <T> T getObject(String location, String method) { // ! Here I want to retrieve the class of T Class<?> requestedClass = getMeTheClassThatWasRequested(); return requestedClass; } Is th...

Best way to share Java implementation between concrete JPA classes?

I have about 10 different entities in my J2EE application that right now share the exact same implementation. They all inherit from the same generic abstract class that has been annotated as a @MappedSuperclass, but this class contains none of the implementation I have repeated in all the concrete subclasses. If I could, I'd put all...

CXF - Webservice method with parameter type as Element

Hi, I am trying to develop SOAP based webservice using CXF. My requirement is to accept any XML data structure as method parameter and then the logic to parse/handle this data would be internally taken care by webservice (A generic webservice for accepting request). Hence I want to declare the method parameter as either org.w3c.dom.Elem...

Get property value of any entity in NHibernate in a generic way

Hi, I need to create a method like this: object GetPropertyValue(object entity, string databaseColumnName, int index); that will take any entity, a column name which is represented as a property in entity class and an optional index that is used if DB column is located inside collection property by some index. For example: Field e...

F#: Function with flexible type argument and return value?

I'm trying to write a function that accepts a certain type or any of its sub-types as one of its arguments, then returns a value of a type or any of its sub-types. [<AbstractClass>] type Spreader () = abstract Fn : unit -> unit type Fire () = inherit Spreader () override self.Fn () = () type Disease () = inherit Spread...

How to Create a Method that only accepts elements that implement Iterable

I want to write a function printAll(), which accepts only those elements that implement that implement Iterable so that I can iterate over them and print the elements. How do I do that? ...