generics

Using generics in a Parent Child relationship

I have an abstract class BaseItem declared like this: public abstract class BaseItem { public BaseItem Parent { get; protected set; } public List<BaseItem> Children = new List<BaseItem>(); public abstract string Function1(); } Basically, i'm trying to implement a design where each Item has a parent that will be of...

How do you make a Generic Generic Factory?

I am working on a client (Silverlight) interface to a collection of webmethod. And I am trying to avoid writing any custom code for every webmethod. So I have created a ServiceCall<TResult> to handle each call, TResult specifies the return type of the service (I use XmlSerializer to create the returned instance). The client class exposes...

Arraylist to list<t>?

How can I convert/dump an arraylist into a list? I'm using arraylist because I'm using the ASP.NET profiles feature and it looked like a pain to store List in profiles. Note: The other option would be to wrap the List into an own class and do away with ArrayList. http://www.ipreferjim.com/site/2009/04/storing-generics-in-asp-net-profi...

Missing constructor in a class derrived from a generic class

I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically lets you define a generic base repository class, then you can define all of your actual repository classes by deriving from the generic base. I want the option of using a repository with or without passing in the data context, so I decided to create...

how to avoid parameter type warning for self referencing typesafe interface in java?

I am working with tree's in java and I have the following interface for a simple unordered tree with a self reference: public interface Node<N extends Node> { public N getParent(); public void setParent(N parent); public Collection<N> getChildren(); public void addChild(N node); public void removeChild(N node); public N ...

Java Collections: Pass collection of children as collection of parents

Say I have an interface and some classes: public interface IPanel<ComponentType extends Component> { public void addComponents(Set<ComponentType> components); public ComponentType create(); } public class Button extends Component { } public class LocalizedButton extends Button { } public class ButtonsPanel implements IPanel<But...

IsAssignableFrom or AS ?

I have next code: private T CreateInstance<T>(object obj) // where T : ISomeInterface, class { ... if (!typeof(T).IsAssignableFrom(obj.GetType())) { throw ..; } return (T)obj; } Can it be replaced with this: T result = obj as T; if (result == null) { throw ..; } return result; If not - why? ...

How can I pass a Class as parameter and return a generic collection in Java?

I am designing a simple Data Access Object for my Java application. I have a few classes (records) that represents a single row in tables like User and Fruit. I would like to have a single method for getting all records of a specific type. For the moment I have it like this: public List<User> getAllUsers() { ... } public List<Fruit>...

Generic casting question C#

I have a function which takes a generic: public static ShowTrackChangesViewModel CreateVM<T>(IList<TrackChanges> TrackChanges, T entity) { //How do i access T properties ? } T here is an entityFramework object. How do i cast it back to the real one to access its property ? Do i need to write a big if code...

Avoid unchecked warning in following

I've done some fancy wrapping to avoid unchecked warnings in the past, but after 90 mins of poring over http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html, I can't write the findMatch method below and make it work without @SuppressWarnings("unchecked"). The parameterized class isn't known at compile time. public interface ...

What are the risks of explicitly casting from a list of type List<? extends MyObject> to a list of type List<MyObject> in Java?

I think the title should explain it all but just in case... I want to know what risks and potential issues relating to casting can arise from the following snippet of Java code: List<? extends MyObject> wildcardList = someAPI.getList(); List<MyObject> typedList = (List<MyObject>) wildcardList; My thoughts are that all objects in the ...

Generic List of Array

How remove the: Type safety: The expression of type List[] needs unchecked conversion to conform to List<Object>[] compiler warning in the following expression: List<Object>[] arrayOfList = new List[10]; ...

How can i make a generic variable without using generics?

I have a generic class, class ComputeScalar<T> : IComputeVariable where T : struct { // This is why i have to use generics. ComputeBuffer<T> buffer; T data; } class ComputeArray<T> : IComputeVariable where T : struct { // This is why i have to use generics. ComputeBuffer<T> buffer; T[] data; } and i use this class...

Get generic type of class at runtime

How can i achieve this? public class GenericClass<T> { public Type getMyType() { //How do I return the type of T? } } Everything I have tried so far always returns type Object rather than the specific type used. Thanks a lot. ...

Java nested wildcard generic won't compile

I have a problem with bounded nested wildcards in Java generics. Here's a common case: public void doSomething(Set<? extends Number> set) {} public void callDoSomething() { Set<Integer> set = new HashSet<Integer>(); doSomething(set); } This is standard Java generics, works fine. However if the wildcard becomes nested, it ...

Subclassing a generic type, returning instances of the subclass from a method in another class

It was such a simple, brilliant idea. Use the power of Java 5.0 enumerated types to encode details of a data dictionary (attribute name, type, range, units, etc.) and create a type-safe system for setting and reading attribute values (i,.e., attribute AAH is short, ACC is enumerated and should only accept the values ACC001, ACC002, ACC00...

generic list of value types with sequential layout and pack size -> BUG?!

The following code throws an ExecutionEngineException when I run the release build executable (start exe file). Is this a bug or is it normal behavior? value type with pack size = 1: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RunLong { public byte Count; public long Value; public RunLong(byte count, long...

De-serializing some XML (via a Stream) into Parent/Child objects

I have a fairly simple DAL assembly that consists of an SalesEnquiry Class which contains a List<T> of another Vehicle class. We'll be receiving XML files by email that I'm wanting to use to populate instances of my SalesEnquiry class, so I'm trying to use de-serialization. I've added XMLRoot/XMLElement/XMLIgnore attributes to both cla...

Specifying constructor constraint for Generic Parameter

I have a collection of objects which I pass as parameter to create objects of another type (one for one). I am doing this in many places (basically converting from data objects to business objects). I want to write a generic extension method to accomplish this. But I am stuck because I don't know how I can specify constraint that busines...

Java 1.4 to Java 1.5 - Rewrite code and avoid the @SupressWarnings

Hello everybody, I have an old personal project written in Java 1.4 that I am porting to 1.5 (in which I am a still newbie) for version 2. Besides adding new features to it and refactoring code, I am also migrating to generic collections, annotations etc. There is a particular piece of code that I don't know how to change to 1.5 and do...