generics

Marshalling .NET generic types

Here is a C# program that tries Marshal.SizeOf on a few different types: using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] class AClass { } [StructLayout(LayoutKind.Sequential)] struct AStruct { } [StructLayout(LayoutKind.Sequential)] class B { AClass value; } [StructLayout(LayoutKind.Sequent...

Is this a right way to put Java generics in action?

I just started messing with generics and I want to make sure I got this right, any help? public interface Cart<T extends CartItem> { public void setItems(List<T> items); public List<T> getItems(); } public interface CartItem { public BigDecimal getQty(); public void setQty(BigDecimal qty); // more } public class Ca...

How can I get delegates to property accessors from a generic type?

I'm currently building a node editor (as in Blender) and am having trouble getting delegates to property accessors from a generic type. So far the question here has brought me closest, but I'm having trouble that I think is specifically related to the type of object being generic. For reference, a "Node" is synonymous with an object, an...

Generic Concat Extension method for Paramarrays not working for IEnumerable(of String)

Inspired by Javascripts variable Arguments in Max()/Min() and list comprehension in functional languages I tried to get the same in VB.NET using Generic Extension methods given IEnumerable(of T) as resulttype. This works well excepts for strings. Why? These kind of extension methods may be considered a bad idea. Any strong reason Why t...

How to remove elements from a generic list while iterating around it?

I am looking for a better 'pattern' for working with a list of elements which each need processed and then depending on the outcome are removed from the list. You can't use .Remove(element) inside a foreach (var element in X)... you also can't use for (int i = 0; i < elements.Count(); i++) and .RemoveAt(i). Previously I have done craz...

Problem getting generic extension method to work correctly

I'm trying to create the extension method AddRange for HashSet so I can do something like this: var list = new List<Item>{ new Item(), new Item(), new Item() }; var hashset = new HashSet<Item>(); hashset.AddRange(list); This is what I have so far: public static void AddRange<T>(this ICollection<T> collection, List<T> list) { fore...

Why won't this generics code compile?

//The class is defined like so.... public class CreateNewAccountHandler : ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse> { public CreateNewAccountResponse ExecuteCommand(CreateNewAccountCommand command) { throw new NotImplementedException(); } } //And here it the code which won...

Generic type constructor resolution in C#, for IoC with generic service registrations

I'm trying to add generic service support to our IoC container, and I have a question. Let's say I have the following type: public interface IService<T> { ... } public class Service<T> : IService<T> { ... } and then the following code: Type type = typeof(Service<>); // <-- notice no specific type here ConstructorInfo ctor = LogicToF...

.NET generic types - finding most specific type

Are there any good algorithms for determining the "best" type to instantiate in order to fulfill a request? For instance say I have the following classes: public interface ISometype<T> {} public class SomeTypeImpl<T>:ISometype<T> {} public class SomeSpecificTypeImpl<T>:ISometype<T> where T: ISpecificSpecifier {} public interface ISpeci...

How do I write a generic extension method to convert a List to a HashSet using HashSet.AddRange()?

HashSet does not have an AddRange method, so I want to write an extension method for it. This is what I have: public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list) { foreach (var item in list) { collection.Add(item); } } I have a base class, Media, and a derived class, Photo. This is t...

Complex class declaration confusing forms designer?

I have a class declared as: public class Foo<T> : Panel where T : Control, IFooNode , new() { ... } I added it by hand to test it, but I will eventually need something that can be displayed in the Forms designer. The Forms designer doesn't like this, it says: Could not find type 'FooTestNameSpace.Foo'. Please make sure that the ...

Generic method with unspecified type possible?

Hi there, I do need a solution for loading lists of objects - lookups where only one property is referenced from the current object as in this example. class LookupObjectAddress { [...] public string City { get; set; } [...] } class WorkingObject { // references the property from LookupObjectAddress public string City { get; s...

Generics and Factories

Hi all, I'm new to generics and have been trying to figure out how i can return an instance of a class whose base is generic from a factory. See sample code below. The issues are highlighted in factory class: public abstract class MyGenericBaseClass<T> { public string Foo() {...} } public sealed class MyDerivedIntClass : MyGen...

How to encapsulate the concept of a combination of generics into a new type?

Is there any way that one can encapsulate Dictionary to be a new type like DataDictionary so that instead of needing to change the definition in however many places it is used, it can be changed in only a few. Or should I just wrap this in another class that exposes only the aspects that I need? ...

Java generics - method parameter

Is it necessary to parametrize the entire interface for this scenario, even though Bar is only being used in a single method? public interface IFoo<T>{ void method1(Bar<T> bar); //Many other methods that don't use Bar.... } public class Foo1 implements IFoo<Yellow>{ void method1(Bar<Yellow> bar){...}; //Many othe...

java generics type parameters and operations on those types

In searching for an answer to an interesting situation which I had recently encountered I came upon the following question: Type safety, Java generics and querying I have written the following class (cleaned up a bit) public abstract class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements BaseDao<T> { /** ...

"where" keyword in class declaration in c sharp

Hi, Could anyone help me with the line where TEntity : class, IEntity, new() in the following class declaration. public abstract class BaseEntityManager<TEntity> where TEntity : class, IEntity, new() Thanks for your help in advance. ...

Class Helper for generic class?

I'm using Delphi 2009. Is it possible to write a class helper for a generic class, i.e. for TQueue . The obvious TQueueHelper <T> = class helper of TQueue <T> ... end; does not work, nor does TQueueHelper = class helper of TQueue ... end; ...

How to avoid unchecked casts in overriden Collection's methods?

I'm working on a List implementation. Because of this, I'll have to override the methods Collection.containsAll(Collection<?> c); Collection.removeAll(Collection<?> c); Collection.retainAll(Collection<?> c); But as it's explained by Sun, they accept collections with any kind of content (note the <?>). So the collection is not checked ...

Casting problems with connected Generic classes.

Here's a thinned out version of the classes I have. public abstract class BaseParent { } public abstract class ChildCollectionItem<T> where T : BaseParent { // References a third-party object that acts as the parent to both the collection // items and the collection itself. public T parent; // References the collection to w...