generics

I want to get 2 values returned by my query. How to do, using linq-to-entity

var dept_list = (from map in DtMapGuestDepartment.AsEnumerable() where map.Field<Nullable<long>>("GUEST_ID") == DRowGuestPI.Field<Nullable<long>>("PK_GUEST_ID") join dept in DtDepartment.AsEnumerable() on map.Field<...

Question concerning SCJP-6 exam

While preparing for the SCJP-6 exam I faced with a difficult issue. I can’t find answer by myself. Please, answer for the question and give short comments: abstract class A<K extends Number> { // insert code here } public abstract <K> A<? extends Number> useMe(A<? super K> k); public abstract <K> A<? super Number> useMe(A<? ex...

Traversing through an arbitrary dictionary tree structure in C#

I am trying to write a recursive C# function that will operate on generic dictionaries of the form IDictionary<string, T> where T is either another IDictionary<string, T> or a string. My first failed attempt looked something like this: public string HandleDict(IDictionary<string, string> dict){ // handle the leaf-node here } publi...

How to create a property of generic class type?

I have this code: public class SelectionList<T> : ObservableCollection<SelectionItem<T>> where T : IComparable<T> { // Code } public class SelectionItem<T> : INotifyPropertyChanged { // Code } I need to create a property which is of the type SelectionList as follows: public SelectionList<string> Sports { get; set; } But when I ...

Using overloaded operator== in a generic function

Consider the following code: class CustomClass { public CustomClass(string value) { m_value = value; } public static bool operator ==(CustomClass a, CustomClass b) { return a.m_value == b.m_value; } public static bool operator !=(CustomClass a, CustomClass b) { return a.m_value != b.m_value; } ...

Generic usercontrol possible?

Since .Net 4 does support generics in XAML, I'd like to create a UserControl using generics, like: public class MyComboBox<T> { } I can declare the UserControl quite well, but how would I use it in a XAML file? Or can't this be done in XAML? ...

Create Generic Class Instance from Static Method in a Derived Class

I have a class in C# with a template and static method similar to class BClass<T> { public static BClass<T> Create() { return new BClass<T>(); } } From this I derive a class and specify a template parameter to the base class class DClass : BClass<int> { } A problem occurs when I try to use the static method to create an ...

Generic object to object mapping with parametrized constructor

I have a data access layer which returns an IDataRecord. I have a WCF service that serves DataContracts (dto's). These DataContracts are initiated by a parametrized constructor containing the IDataRecord as follows: [DataContract] public class DataContractItem { [DataMember] public int ID; [DataMember] public string Titl...

Dynamic casting using a generic interface

Hi Is there any way to cast to a dynamic generic interface.. Site s = new Site(); IRepository<Site> obj = (IRepository<s.GetType()>)ServiceLocator.Current.GetInstance(t) obviously the above won't compile with this cast. Is there anyway to do a dynamic cast of a generic interface. I have tried adding a non generic interface but the s...

How do I convert the Cookies collection to a generic list? Easily

Anyone know how I can convert Request.Cookies into a List<HttpCookie>? The following didn't work as it throws an exception. List<HttpCookie> lstCookies = new List<HttpCookie>( Request.Cookies.Cast<HttpCookie>()); Exception: Unable to cast object of type 'System.String' to type 'System.Web.HttpCookie' ...

What's the purpose behind wildcards and how are they different from generics?

I'd never heard about wildcars until a few days ago and after reading my teacher's Java book, I'm still not sure about what's it for and why would I need to use it. Let's say I have a super class Animal and few sub classes like Dog, Cat, Parrot, etc... Now I need to have a list of animals, my first thought would be something like: List...

How to convert string to any type

Hi there I want to convert a string to a generic type I have this: string inputValue = myTxtBox.Text; PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName); Type propType = propInfo.PropertyType; object propValue = ????? I want to convert 'inputString' to the type of that property, to check if it's compatible how...

Visual Studio 2008 having problems with namespaces when used as type in Generic coolection

I just upgraded last week from Visual Studio 2005 to 2008. I am having an issue with compiler resolving namespaces when I use a class as a type in a Generic collection. Intellisense recognizes the class and the compiler generates no errors when I use the class except when it is a type in a Generic collection declaration either as retur...

C# Generics Multiple Inheritance Problem

Can any one help me with this syntax issue with C#? I have no idea how to do it. class SomeClass<T> : SomeOtherClass<T> where T : ISomeInterface , IAnotherInterface { ... } I want SomeClass to inherit from SomeOtherClass and IAnotherInterface and for T to inherit ISomeInterface only It seems the problem is that the where keyword scre...

C# determining generic type

I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1; MyObject<datatype2> obj2; MyObject<datatype3> obj3; I want to store these objects in a List... I think I would do that like this: private List<MyObject<object>> _myList; I then want to create a function that takes 1 parameter, be...

A good practical application of the Covariance and Contravariance in .NET 4.0?

I wish to get acquainted with the recently release of the .NET Framework 4.0 and its Covariance and Contravariance in Generics. Even though I have read what is written at the referenced link, I can't get a grab on how it should be used, and when it shouldn't. A brief explanation and a simple real-world-like code sample is appreciated. ...

What's my best approach on this simple hierarchy Java Problem?

First, I'm sorry for the question title but I can't think of a better one to describe my problem. Feel free to change it :) Let's say I have this abstract class Box which implements a couple of constructors, methods and whatever on some private variables. Then I have a couple of sub classes like BoxA and BoxB. Both of these implement ex...

How to create a generic C# method that can return either double or decimal?

I have a method like this: private static double ComputePercentage(ushort level, ushort capacity) { double percentage; if(capacity == 1) percentage = 1; // do calculations... return percentage; } Is it possible to make it of a generic type like "type T" where it can return either decimal or double, dep...

Convert generic type definition strings from C# style to CLR style

I want to convert a C# style generic type string, like "System.Dictionary<System.String, System.String>" to it's CLR equivalent: "System.Dictionary`1[System.String, System.String]" and back. Is there an easy way to do this, or do I have to resort to string manipulation? EDIT: I only have the string representation in C#/VB/etc styl...

C#4.0: How to find out if types are co-variantly equal

For example I have interface ICommand {} class MovePiece : ICommand {} class Successful<CMD> where CMD : ICommand {} and I want the following: CovariantlyEqual( typeof(Successful<ICommand>), typeof(Successful<MovePiece>)) .ShouldEqualTrue() Update Eric asks below why I would want to test such a thing. Fair enough. I think t...