generics

How to use method hiding (new) with generic constrained class

I have a container class that has a generic parameter which is constrained to some base class. The type supplied to the generic is a sub of the base class constraint. The sub class uses method hiding (new) to change the behavior of a method from the base class (no, I can't make it virtual as it is not my code). My problem is that the 'ne...

Accessing elements of List<List<string>>

Hello Friends. Can anyone let me know how can access an element of a list that has been added to a list of list. I'll mention the code. List<string> str = new List<string>(); List<List<string>> stud = new List<List<string>>(); A method has been defined that inserts data into str and after the method gets over. stud.Add(str); The me...

Generic extension method returning IEnumerable<T> without using reflection

Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this MatchCollection mc) { return new T[mc.Count]; } } And this class: public class Ingredient { public String Name { get; set; } } Is there any way to magically transform a MatchCollec...

Why can't I enforce derived classes to have parameterless constructors?

I am trying to do the following: class Program { static void Main(string[] args) { foo<baz> fooObject = new foo<baz>(); AnotherClass<baz> _baz = new AnotherClass<baz>(); _baz.testMethod(fooObject); } } public class AnotherClass<T> where T : bar { public void testMethod(foo<T> dummy) { ...

Approaches for generic, compile-time safe lazy-load methods

Suppose I have created a wrapper class like the following: public class Foo : IFoo { private readonly IFoo innerFoo; public Foo(IFoo innerFoo) { this.innerFoo = innerFoo; } public int? Bar { get; set; } public int? Baz { get; set; } } The idea here is that the innerFoo might wrap data-access methods o...

Service locator for generics

Hi everyone, I have say a dozen types T which inherit from EntityObject and IDataObject. I have generic the following interface IDataManager<T> where T : EntityObject, IDataObject ... I have also base class for data managers BaseDataManager<T> : IDataManager<T> where T : EntityObject, IDataObject .... And i have particular classes...

How do I read an attribute on a class at runtime?

I am trying to create a generic method that will read an attribute on a class and return that value at runtime. How do would I do this? Note: DomainName attribute is of class DomainNameAttribute. [DomainName(“MyTable”)] Public class MyClass : DomianBase {} What I am trying to generate: //This should return “MyTable” String Doma...

Discover generic types

Thanks @dtb for help, he advised really need piece of code for generic service locator static class Locator { private static class LocatorEntry<T> where T : ... { public static IDataManager<T> instance; } public static void Register<T>(IDataManager<T> instance) where T : ... { LocatorEntry<T>.instanc...

What is the problem with this Java code dealing with Generics?

interface Addable<E> { public E add(E x); public E sub(E y); public E zero(); } class SumSet<E extends Addable> implements Set<E> { private E element; public SumSet(E element) { this.element = element; } public E getSum() { return element.add(element.zero()); } } It seems that element...

java generics covariance

I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant the author states, Because ln is a List, adding a Float to it seems perfectly legal. But if ln were aliased with li, then it would break the type-safety promise impli...

Type patterns in Haskell

I'm trying to compile a simple example of generic classes / type patterns (see http://www.haskell.org/ghc/docs/latest/html/users_guide/generic-classes.html) in Haskell but it won't compile. Any ideas about what's wrong with the code would be helpful. According to the documentation there should be a module Generics with the data types Un...

Generic TypeIdenitifier convertion.How?

How do I convert the TypeIdenitifier to a class type? I need to use implicit convertion. type TMyChildArray<T>=class(TMyArray<T>) private FData:Array of T; procedure AddEnd(); end; TTypeIdenitifierParentClass=class(TAnotherParentClass) protected TestField:Cardinal; end; procedure TMyChildArray<T>.A...

using extends with Java Generics

Lets say that I have the following code: public class Shelter<A extends Animal, B extends Animal> { List<A> topFloor = new Vector<A>(); List<B> bottomFloor = new Vector<B>(); public A getFirstTopFloorAnimal(){return topFloor.get(0);} public B getFirstBottomFloorAnimal(){return bottomFloor.get(0);} //These 3 me...

How do I create a new AnyType[] array?

Which is the best practice in this situation? I would like an un-initialized array of the same type and length as the original. public static <AnyType extends Comparable<? super AnyType>> void someFunction(AnyType[] someArray) { AnyType[] anotherArray = (AnyType[]) new Comparable[someArray.length]; ...or... AnyType[] anot...

Is there a nice way of having static generic parameters is Java?

Hello, recently I'm writing some functions that I take from Haskell and translate into Java. One of the main problems I have is I cannot easily create a static property with a generic type. Let me explain by a little example... // An interface to implement functions public interface Func<P, R> { public R apply(P p); } // What I wan...

Java generic return type

Hi, I'd like to write a method that can accept a type param (or whatever the method can figure out the type from) and return a value of this type so I don't have to cast the return type. Here is a method: public Object doIt(Object param){ if(param instanceof String){ return "string"; }else if(param instanceof Integer){...

Using generics with XmlSerializer

Hi, When using XML serialization in C#, I use code like this: public MyObject LoadData() { XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyObject)); using (TextReader reader = new StreamReader(settingsFileName)) { return (MyObject)xmlSerializer.Deserialize(reader); } } (and similar code for deserializ...

Can protobuf-net serialize this combination of interface and generic collection?

I am trying to serialize a ItemTransaction and protobuf-net (r282) is having a problem. ItemTransaction : IEnumerable<KeyValuePair<Type, IItemCollection>></code> and ItemCollection is like this: FooCollection : ItemCollection<Foo> ItemCollection<T> : BindingList<T>, IItemCollection IItemCollection : IList<Item> where T is a derive...

Casting generics and the generic type

Consider, I have the following 3 classes / interfaces: class MyClass<T> { } interface IMyInterface { } class Derived : IMyInterface { } And I want to be able to cast a MyClass<Derived> into a MyClass<IMyInterface> or visa-versa: MyClass<Derived> a = new MyClass<Derived>(); MyClass<IMyInterface> b = (MyClass<IMyInterface>)a; But I...

Create all word variations of 2 character arrays

I have 2 collections of caracter string ex: List<string> one = new List<string>; one.Add("a"); one.Add("b"); one.Add("c"); List<string> two = new List<string>; two.Add("x"); two.Add("y"); two.Add("z"); What i would like to do is create a list of all the variations of words that can be created from this. But i only want to create 4 c...