generics

Casting between value types on a class using generics

In this section of a function (.NET 2.0): public void AttachInput<T>(T input) where T : struct { if (input is int) { Inputs.Add((int)input); } } The compiler shows the error "Cannot convert type 'T' to 'int'. So, I used Convert.ToInt32() which worked - but does it box input to an object? Is there a better solution?...

C# - Cast object to IList<T> based on Type

I am trying out a little reflection and have a question on how the cast the result object to an IList. Here is the reflection: private void LoadBars(Type barType) { // foo has a method that returns bars Type foo = typeof(Foo); MethodInfo method = foo.GetMethod("GetBars") .MakeGenericMethod(bar); object obj = m...

IEnumerable<CustomType> in PowerShell

I'm trying to use Enumerable.ToList() in PowerShell. Apparently, to do that, I have to explicitly convert the object to IEnumerable<CustomType>, but I am unable to do that. It seems I can't correctly write IEnumerable<CustomType> in PowerShell. Both IEnumerable<string> and CustomType itself work correctly (the custom type I'm trying to u...

Why does a function that takes IEnumerable<interface> not accept IEnumerable<class>?

Say, for instance, I have a class: public class MyFoo : IMyBar { ... } Then, I would want to use the following code: List<MyFoo> classList = new List<MyFoo>(); classList.Add(new MyFoo(1)); classList.Add(new MyFoo(2)); classList.Add(new MyFoo(3)); List<IMyBar> interfaceList = new List<IMyBar>(classList); But this produces the e...

Problem at JUnit test with generics

In my utility method: public static <T> T getField(Object obj, Class c, String fieldName) { try { Field field = c.getDeclaredField(fieldName); field.setAccessible(true); return (T) field.get(obj); } catch (Exception e) { e.printStackTrace(); fail(); return null; } } The line ...

Generic overloading tells me this is the same function. Not agree.

base class: Class List(Of T) Function Contains(ByVal value As T) As Boolean derived class: Class Bar : List(Of Exception) ' Exception type as example ' Function Contains(Of U)(ByVal value As U) As Boolean compiler tells me that that two are the same, so I need to declare Overloads/new this second function. But I want use U...

Factory Method Pattern using Generics-C#

Just I am learning Generics.When i have an Abstract Method pattern like : //Abstract Product interface IPage { string pageType(); } //Concerete Product 1 class ResumePage : IPage { public string pageType() { return "Resume Page"; } } //Concrete Product 2 class SummaryPage : IPage { public string pageType() ...

Delphi 2010 - Why can't I declare an abstract method with a generic type parameter?

I am trying to do the following in Delphi 2010: TDataConverter = class abstract public function Convert<T>(const AData: T): string; virtual; abstract; end; However, I keep getting the following compiler error: E2533 Virtual, dynamic and message methods cannot have type parameters I don't quite understand the reason why I can't d...

Java Generics: set List of superclass using List of subclass

If I have a method in MyClass such as setSuperClassList(List<Superclass>) ...should I be able to do this: new MyClass().setSuperClassList(new ArrayList<Subclass>()) It appears this won't compile. Why? ...

Force an unchecked call

Hello, Sometimes, when using Java reflection or some special storing operation into Object, you end up with unchecked warnings. I got used to it and when I can't do anything about it, I document why one call is unchecked and why it should be considered as safe. But, for the first time, I've got an error about a unchecked call. This fun...

What should i do to test EasyMock objects when using Generics ? EasyMock

See code just bellow Our generic interface public interface Repository<INSTANCE_CLASS, INSTANCE_ID_CLASS> { void add(INSTANCE_CLASS instance); INSTANCE_CLASS getById(INSTANCE_ID_CLASS id); } And a single class public class Order { private Integer id; private Integer orderNumber; // getter's and setter's ...

Do you gain any operations when you constrain a generic type using where T : struct?

This may be a bit of an abstract question, so apologies in advance. I am looking into generics in .NET, and was wondering about the where T : struct constraint. I understand that this allows you to restrict the type used to be a value type. My question is, without any type constraint, you can do a limited number of operations on T. D...

How to declare a Generics Action in struts2.xml file ?

Hi everyone, My problems is in a Struts2 action, where I have a class : public class MyAction<T> extends ActionSupport with a private member like this : private T myData; And I would like to declare this aciton in the struts.xml file, how can i manage to do so ? Thanks for the answer. Ps : I've tried without declaration of T...

Object Model Contracts

I'm trying to create a data model for WCF based off of interfaces from my core object model, but I'm having trouble with some of the associations Currently I have this in my core data model public class A : IA { public string name { /* ... */ } public EntitySet<B> children { /* ... */ } } public class B : IB { publ...

Is there a way to make ToEnum generic

I would like to do this but it does not work. bool TryGetEnum<TEnum, TValue>(TValue value, out TEnum myEnum) { value = default(TEnum); if (Enum.IsDefined(typeof(TEnum), value)) { myEnum = (TEnum)value; return true; } return false; } Example usage: MyEnum mye; bool success = this.TryGetEnum<MyEnum,...

C# Generics Casting

Visual sutdio 2008 has the ability to automatically create unit test stubs. I have used this to create some basic unit tests, but I am confused by something: private class bla : BaseStoreItem { // } /// <summary> ///A test for StoreData ///</summary> public void StoreDataTestHelper<T>() where T : BaseStoreItem ...

Generic Vector in Java

Hi, Why doesn't the add() method here compile? Vector<?> vectorList[] = new Vector<?>[10]; vectorList[0].add("elem1"); Thanks ...

Java generics parameters with base of the generic parameter

Hello, I am wondering if there's an elegant solution for doing this in Java (besides the obvious one - of declaring a different/explicit function. Here is the code: private static HashMap<String, Integer> nameStringIndexMap = new HashMap<String, Integer>(); private static HashMap<Buffer, Integer> nameBufferIndexMap = ...

C# How to check if a class implements generic interface ?

How to get generic interface type for an instance ? Suppose this code: interface IMyInterface<T> { T MyProperty { get; set; } } class MyClass : IMyInterface<int> { #region IMyInterface<T> Members public int MyProperty { get; set; } #endregion } MyClass myClass = new MyClass(); /* returns the ...

Limit on amount of generic parameters in .NET?

Is there a limit on the amount of generic parameters you can have on a type in .NET? Either hard limit (like 32) or a soft limit (where it somehow effects performance to much, etc.) What I'm referring to is: class Foo<T0, T2, T3, T4, etc.> { } ...