2
голосов
3ответов
226 просмотров

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. publi...

2
голосов
4ответов
986 просмотров

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 ...

10
голосов
3ответов
4625 просмотров

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;

17
голосов
4ответов
11063 просмотров

"where" keyword in class declaration in c sharp

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()

0
голосов
2ответов
3264 просмотров

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 Hiber...

4
голосов
3ответов
6311 просмотров

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&gt...

3
голосов
2ответов
178 просмотров

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?

4
голосов
7ответов
653 просмотров

Generics and Factories

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...

4
голосов
1ответов
1317 просмотров

Generic method with unspecified type possible?

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...

4
голосов
2ответов
369 просмотров

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 '...

1
голосов
2ответов
1441 просмотров

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 hav...

1
голосов
2ответов
1076 просмотров

.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>:I...

3
голосов
1ответов
957 просмотров

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<>); /...

0
голосов
2ответов
825 просмотров

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(...

2
голосов
2ответов
2005 просмотров

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&...

466
голосов
27ответов
389423 просмотров

How to remove elements from a generic list while iterating over 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) (because it results in Collection was modified; enumeration operation m...

0
голосов
1ответов
822 просмотров

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 cons...

2
голосов
2ответов
2886 просмотров

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,...

1
голосов
1ответов
148 просмотров

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...

10
голосов
2ответов
6531 просмотров

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 ...

0
голосов
2ответов
100 просмотров

Is this code sample really returning the right parameterized types?

I've read the Beautiful code with Google Collections, Guava and static imports article about Java collections, and the following snippet got my attention: Map<String, Map<Long, List<String>>> map = Maps.newHashMap(); The thing is, I don't understand how it's possible that the...

0
голосов
7ответов
2328 просмотров

Is it possible to instantiate an object of generic type in Java or C#?

For example, in Java, if I have a parameterized class, is it possible to do the following? public class MyClass<T>{ public void aMethod { .. T object = new T(); .. } .. } In Java, I think it is not possible, because the compiler doesn't know what constructor to call. Bu...

3
голосов
2ответов
262 просмотров

In a knot with Generics

I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where TRepo : IRepository<T> { public static TRepo Repository { get;private set; } } A repository interface: public interface IRepository<T> //where T : DomainOb...

384
голосов
18ответов
88331 просмотров

Passing a single item as IEnumerable<t></t>

Is there a common way to pass a single item of type T to a method which expects an IEnumerable&lt;T&gt; parameter? Language is C#, framework version 2.0. Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this ...

40
голосов
3ответов
11168 просмотров

Why can't I catch a generic exception in C#?

I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example) static void Main(string[] args) { RunTest&lt;ArgumentException&gt;(); } static void RunTest&lt;T&...

1
голосов
2ответов
126 просмотров

Why can I not compare two generic objects of a certain implicit type?

Consider the following code: public class SystemManager&lt;T&gt; where T : ISettings { public SystemManager() { T implicit1 = default(T); T implicit2 = default(T); if (implicit1 != implicit2) { // This will not compile saying the compiler canno...

2
голосов
5ответов
1056 просмотров

How to specify a generic type as a restriction in a generic method

I have a generic class like this: class MyGenericClass&lt;T&gt; { } On the other hand I have an aggregator class that I use to obtain singleton instances of MyGenericClass and of derived classes, where each instance is identified by the concrete class type and by the parameter type. That is, M...

1
голосов
1ответов
684 просмотров

Generic Http Module

The problem I am trying to make a generic http module in asp.net C# for handling roles defined by an enum which i want to be able to change by a generic parameter. This will make it possible to use the generic module with any kind of enum defined for each project. The module hooks into the Auth...

0
голосов
1ответов
1850 просмотров

How to write a generic class that implements an interface having generic methods with constraints defined

I'm new to Generics implementation and need inputs on 2 issues I face : I have an interface ICommand defined as : public ICommand { List&lt;T&gt; Execute&lt;T&gt;() where T : IValidationResult; IDomain&lt;T&gt; GetDO&lt;T&gt;() where T : IValidationResult; } intentionally I have it as ...

5
голосов
5ответов
7635 просмотров

How can I enforce a type constructor in a concrete class through the interface it implements?

Let me describe the situation, and I'm sure I'm just thinking about this problem incorrectly. I have a concrete class that will implement an interface. I want to enforce in the contract that the class must have a constructor with a specific type. So for instance: interface MyInterface {} public ...