generics

Casting "value" in C# property as a IEnumerable<> generic in DataSource override.

Here is your basic DataSource override: public override object DataSource { get { return base.DataSource; } set { base.DataSource = value; } } What I would like to do in the setter method is cast value to IEnumerable<> and do some manipulations on i...

MVP, generics and DRY

Hi, I have a problem with my MVP structure that is built upon generic presenters, views etc. and I feel I'm violating DRY and I dont know how to get around it. Example. public class Presenter<TView, TModel> where TView : IView where TModel : Model {} So far everything is fine, but I want to have it like this public class Presen...

How to Compare two class objecy with Reflection?

i try to compare two class comp1,comp2 i used method below: ComparerCollection(array_X, array_Y); but there are errors below. Arraylist generated from Ilist. how can i do that? namespace GenericCollecitonComparer { class Program { static void Main(string[] args) { myClass comp1 = new myClass() { ID ...

Java Abstract Class Implementing an Interface with Generics

I am trying to define an abstract class implementing Comparable. When I define the class with following definition: public abstract class MyClass implements Comparable <MyClass> subclasses have to implement compareTo(MyClass object). Instead, I want every subclass to implement compareTo(SubClass object), accepting an object of its own...

Moq: How to mock a generic method (when the Generic Type T is internal) ?

I've the following method defined public interface IData { T GetUserById<T>(int id) where T : IMyUser, new(); } The actual use of this method is like : da.GetUserById<MyUser>(id); where the MyUser is an internal class defined in the business logic and cannot be used by the unittest. In NMock this can be done using this code: ...

Define a generic that implements the + operator

I have a problem I’m working on, currently it is working for ints but i want it to work for all classes that can be added using the + operator. Is there any way to define this in the generic? For example, public List<T> Foo<T>() where T : ISummable Is there any way to do this? EDIT: Performance of passing in a delegate to do the summ...

How do you execute a Generic Interface method without knowing the exact type declaration

I have an interface declared as follows public interface ILoadObjects<T> where T : class { List<T> LoadBySearch(); } I then have a class declared as follows public class MyTestClass : ILoadObjects<MyTestClass> { List<MyTestClass> ILoadObjects<MyTestClass>.LoadBySearch() { List<MyTestClass> list = new List<MyTestCl...

"Current Type" placeholder in C# generic types?

Basically, what I want to do, is: public class MySpecialCollection<T> where T : ISomething { ... } public interface ISomething { public ISomething NextElement { get; } public ISomething PreviousElement { get; } } public class XSomething : ISomething { ... } MySpecialCollection<XSomething> coll; XSomething element = coll.G...

Fluent NHibernate and automapping generic types

Hi, I'm automapping most of my model, but have a problem with generics. I've got ValueContainer, and I make it abstract so that it doesn't throw an exception during automapping. Next, I have to create classes like StringValueContainer just to make it mapped. Needless to say, I don't like this approach, since I'm perfectly happy with the...

Force generic interface implementation in C#

Is there anyway to force a constraints for a generic definition to implement a "generic interface" ... that is, I want the class to support passing an interface and a generic class constraining it so that the class implements the interface. For example if I say: MyGenericClass<IMyInterface, MyImplementation>.DoSomething(); That should...

Why generic interfaces are not co/contravariant by default?

For example IEnumerable<T> interface: public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } In this interface the generic type is used only as a return type of interface method and not used as a type of method arguments thus it can be covariant. Giving this, can't compiler theoretically infer the va...

Understanding C# generics much better

I looked at some sample code using C# generics. Why and when should I use them? All the examples were complex. I need a simple, clear example that gets me started with C# generics. ...

Is generic constructor in non-generic class supported?

Is it not supported, is it supported but I have to do some tricks? Example: class Foo { public Foo<T1,T2>(Func<T1,T2> f1,Func<T2,T1> f2) { ... } } the generics are only used in constructor, there is no field/property depended on them, I use it (generics) to enforce the type correlation for f1 and f2. Remark: I found the w...

Generics without collection

I have a method that usually takes an item from a list and has a signature of: myMethod(T item) I want to use this method but I know what I am sending the method. SpecificItem myItem = new SpecificItem(); myMethod((T) myItem); This doesn't sit well with me. Is this a sign of bad code? ...

How to get type parameter values using java reflection?

interface Foo<T> { ... } class Bar implements Foo<Baz> { ... } I've got a Bar object. How to get the value of T for it (Baz)? So far, I only managed to get the interface and T, but I can't see a way to get its value. Thanks in advance. ...

Incorrect generic type reported -- why?

Why doesn't this work? public class FooImpl implements Foo { /* ... */ } public class Main { public static <T> Collection<T> getList(Class<? extends T> itemClass) { /* ... */ } public static void main(String[] args) { Collection<Foo> foos = getList(FooImpl.class); } } On the line where foos is declared, I'm getti...

Using .ToDictionary()

I have a method returning a List, let's call it GetSomeStrings(). I have an extension method on string class, returning number of characters in the string, eg. myString.Number('A'). I would like to, in a single line, grab a dictionary. An entry of the dictionary contains the string, and the number of a chosen character in the string. ...

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

Hi All, Is there any specific reason why indexing is not allowed in IEnumerable. I found a workaround for the problem I had, but just curious to know why it does not allow indexing. Thanks, ...

c# generic method + reflection question

In C# I want to create a generic method that: Accepts a MethodInfo object as a parameter. Returns the object that is returned when the MethodInfo is invoked. The source of my confusion is that I want the method to be generically typed to the same return type as the MethodInfo object that gets passed in. ...

Is it possible to use a list of untyped generics in C#?

I'm trying the following design without success: abstract class Foo<T> { abstract T Output { get; } } class Bar { List<Foo> Foos; } I would dislike using an array list, because I would have to use unsafe casts to get the type. I'd like Foo to be typed so that "Output" isn't just an object, in which case I'd have to use unsafe...