generics

C# specifying generic delegate type param at runtime

following setup, i have several generic functions, and i need to choose the type and the function identified by two strings at runtime. my first try looked like this: public static class FOOBAR { public delegate void MyDelegateType(int param); public static void foo<T>(int param){...} public static void bar<T>(int param){....

C# generics based object to object mapper question

I have the need for an object to object mapper in my application. I've tried out a few, but haven't been able to find anything that fits my needs, so I'm writing my own. Currently I have an interface like below: public interface IMapper<T, R> { T Map(R obj); } I then implement an AccountMapper that maps a Customer to an Account ...

Are there pitfalls to using static class/event as an application message bus

I have a static generic class that helps me move events around with very little overhead: public static class MessageBus<T> where T : EventArgs { public static event EventHandler<T> MessageReceived; public static void SendMessage(object sender, T message) { if (MessageReceived != null) MessageReceived(sen...

Reflective Generic Detection

Trying to find out if a provided Type is of a given generic type (with any generic types inside) Let me Explain: bool IsOfGenericType(Type baseType, Type sampleType) { /// ... } Such that: IsOfGenericType(typeof(Dictionary<,>), typeof(Dictionary<string, int>)); // True IsOfGenericType(typeof(IDictionary<,>), typeof(Dictionary<st...

Initialize generic object from a System.Type

I need to create a generic type, but I do not know the type at compile time. I would like to do this: Type t = typeof(whatever); var list = new List<t> this won't compile, because t is not a valid type. But it does know all about a valid type. Is there a way to dynamically create the generic list from a System.Type like this? I may ne...

With this generics code why am I getting "Argument 1: cannot convert from 'ToplogyLibrary.RelationshipBase<TNode>' to 'TRelationship'"

Hi, Any see why I'm getting a "Argument 1: cannot convert from 'ToplogyLibrary.RelationshipBase' to 'TRelationship'" in the code below, in CreateRelationship() ? public class TopologyBase<TKey, TNode, TRelationship> where TNode : NodeBase<TKey>, new() where TRelationship : RelationshipBase<TKey>, new() { // Properties p...

What is Action<string> ?

What is Action<string>, how can it be used? ...

Is it possible to write a generic +1 method for numeric box types in Java?

This is NOT homework. Part 1 Is it possible to write a generic method, something like this: <T extends Number> T plusOne(T num) { return num + 1; // DOESN'T COMPILE! How to fix??? } Short of using a bunch of instanceof and casts, is this possible? Part 2 The following 3 methods compile: Integer plusOne(Integer num) { ...

Error in my OO Generics design. How do I workaround it?

I get "E2511 Type parameter 'T' must be a class type" on the third class. type TSomeClass=class end; ParentParentClass<T>=class end; ParentClass<T: class> = class(ParentParentClass<T>) end; ChildClass<T: TSomeClass> = class(ParentClass<T>) end; I'm trying to write a lite Generic Array wrapper for any data type(ParentParentClass) ,...

How do I configure StructureMap to use a generic repository?

I have an interface IGenericRepository<TEntity> where TEntity : IEntity and an implementation GenericRepository<TEntity> where TEntity : Entity. I'm trying to inject a specific IGenericRepository<Section> into a class using StructureMap: ObjectFactory.Initialize(x => { x.For(typeof(IGenericRepository<>)).Use(typ...

C#: Func<T, TResult> for generic methods

It is possible to create a Func object what references a generic method? like the LINQ OrderBy: public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector ) ...

Improve this generic abstract class

I have the following abstract class design, I was wondering if anyone can suggest any improvements in terms of stronger enforcement of our requirements or simplifying implementing of the ControllerBase. //Dependency Provider base public abstract class ControllerBase<TContract, TType> where TType : TContract, class { public static TC...

Can I Cast a Generic List by Type??

NavigatorItem NavItem = (NavigatorItem)cboItems.SelectedItem; lblTitle.Text = NavItem.Title; RadWrapPanel Panel = new RadWrapPanel(); Type t = NavItem.ItemsType; //<------ The Type inside my List is here. List<???> items = (List<???>)NavItem.Items; // <----Here Is the problem foreach (object i...

Polymorphism in Delphi Generics

type TParent=class public member1:Integer; end; TChild=class(TParent) public member2:Integer; end; TGArray<T: TParent>=class public function test:T; end; implementation var g:TGArray<TChild>; function TGArray<T>.test:T; begin Result:=??.create; // <<<< Problem ! end; ...

Cast Object to Generic List

I have 3 generict type list. List<Contact> = new List<Contact>(); List<Address> = new List<Address>(); List<Document> = new List<Document>(); And save it on a variable with type object. Now i nedd do Cast Back to List to perfom a foreach, some like this: List<Contact> = (List<Contact>)obj; But obj content change every time, and i h...

Is It possible to use the second part of this code for repository patterns and generics

Is there any issues in using version 2,to get the same results as version 1. Or is this just bad coding. Any Ideas public class Customer { public int CustomerID { get; set; } public string EmailAddress { get; set; } int Age { get; set; } } public interface ICustomer { void AddNewCustomer(Customer Customer); void A...

Trouble with abstract generic methods

Let's say I have a class library that defines a couple entity interfaces: public interface ISomeEntity { /* ... */ } public interface ISomeOtherEntity { /* ... */ } This library also defines an IRepository interface: public interface IRepository<TEntity> { /* ... */ } And finally, the library has an abstract class called Repository...

Existentials and Scrap your Boilerplate

I'm writing a XML (de)serializer using Text.XML.Light and Scrap your Boilerplate (at http://github.com/finnsson/Text.XML.Generic) and so far I got working code for "normal" ADTs but I'm stuck at deserializing existentials. I got the existential data type data DataBox where DataBox :: (Show d, Eq d, Data d) => d -> DataBox and I'm t...

[C#] Problems with implementing generic IEnumerator and IComparable

Hi all! I'm working on an AVL Tree. The tree itself seems to be working but I need a iterator to walk through the values of the tree. Therefore I tried to implement the IEnumerator interace. Unfortunately I get a compile time error implementing IEnumerator and IComparable. First the code and below that the error. class AvlTreePreOrderE...

Generics not so generic !!

Hi I tried to implement a generic binary search algorithm in scala. Here it is : type Ord ={ def <(x:Any):Boolean def >(x:Any):Boolean } def binSearch[T <: Ord ](x:T,start:Int,end:Int,t:Array[T]):Boolean = { if (start > end) return false val pos = (start + end ) / 2 if(t(pos)==x) true else if (t(pos) < x) binSearch(x,pos+1,end,...