generics

Generic conversion function doesn't seem to work with Guids

I have the following code: public static T ParameterFetchValue<T>(string parameterKey) { Parameter result = null; result = ParameterRepository.FetchParameter(parameterKey); return (T)Convert.ChangeType(result.CurrentValue, typeof(T), CultureInfo.InvariantCulture); } The type of result.CurrentVal...

the Nice programming language as an alternative to Java generics

I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics, he briefly mentions the Nice programming language as something that handles parametrized types better than Java, but compiles into Java bytecode. Does anyone have any experience with this? Generics make my head hurt, so the prospect of an alternative that i...

Java: Trouble with Generics & Collection type detection

I have a class called DataSet with various constructors, each specifying a different type of variable. It might look a bit like this: public class DataSet { private HashSet Data; public DataSet( DataObject obj ) { Data = new <DataObject>HashSet(); Data.add( obj ); } public DataSet( ObjectRelations...

Is it Possible to Make a Generic Control in .Net 3.5?

Hi all, I've got a control that's declared thus: public partial class MessageBase<T> : UserControl { protected T myEntry; public MessageBase() { InitializeComponent(); } public MessageBase(T newEntry) { InitializeComponent(); myEntry = newEntry; ...

LINQ- Combine Multiple List<T> and order by a value (.Net 3.5)

Hi, all I'm trying to combine a number of List<T> where T:IGetTime (i.e T will always have method getTime()). Then I'm trying order the items by the DateTime that getTime() returns. My LINQ looks like this: public static List<T> Combine(List<T> one, List<T> two, List<T> three) { var result = from IGetTime item in one ...

Serialization exception in .NET: System.InvalidOperationException: You must implement a default accessor on System.Collections.Generic.Stack`1

All I am trying to do is XmlSerializer serializer = new XmlSerializer(typeof(Stack<int>)); and I get the following at runtime: System.InvalidOperationException: You must implement a default accessor on System.Collections.Generic.Stack`1 [[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] b...

How do I call a constructor on a generic type within a function of a base type without its own constructor?

I thought a great way to test my understanding of generic functions would be to create a function that would spit out a hex representation of a hash using one of the classes that inherits from HashAlgorithm. Since all of the HashAlgorithm classes offer ComputeHash, I thought this would be simple. When I construct such a function. though,...

C# - List<T> or IList<T>

Can anyone explain to me why I would want to use IList over List in C#? Related question: Why is it considered bad to expose List<T> ...

What does "T" mean in C#?

Duplicate of: http://stackoverflow.com/questions/361336/what-are-generics-in-c# I have a VB background and I'm converting to C# for my new job. I'm also trying to get better at .NET in general. I've seen the keyword "T" used a lot in samples people post. What does the "T" mean in C#? For example: public class SomeBase<T> where T : ...

UserControl that has a generic class in its inheritance tree

I'm trying to create a UserControl that inherits from a generic class. It does not directly inherit from a generic class, but through an intermediate class that does not use generics. This compiles and works at runtime, but I get an error at design time. Here's my generic parent class: Public Class GenericParent(Of T) Inherits Us...

How to constrain multiple generic types?

Here's a simple syntax question (I hope), I know how to constrain one generic type using the where clause, but how to constrain two generic types? Maybe the easiest way is to write down what my best guess as to the syntax was. public class GenericDaoGetByIdTests<TDao, TComponent> : BaseDaoTests where TDao : IDao<TComponent>, TCompon...

How can I get the correct text definition of a generic type using reflection?

I am working on code generation and ran into a snag with generics. Here is a "simplified" version of what is causing me issues. Dictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>(); string text = dictionary.GetType().FullName; MessageBox.Show(text); With the above code snippet the value for "text" is as follows...

How can I overload a C# method by specific instances of a generic type.

Coming from a C++ background, I've run into a snag with overloading based on a specific instance of a generic type. The following doesn't work since only once instance of the code for the Foo<T> class is ever generated, so inside the Method, the type of this is simply Foo<T>, not Foo<A> or Foo<B> as I'd hoped. In C++ I'm used to templa...

Generics and locking on collections

is there any difference between: lock((IDictionary) _collection).SyncRoot) or lock(_collection) ...

CodeDom generic type constraint

Is there a way to generate a class constraint with CodeDom. Because when I use something like var method = new CodeMemberMethod(); var genericParam = new CodeTypeParameter("InterfaceType"); genericParam.Constraints.Add("class"); method.TypeParameters.Add(genericParam); the generated code is like private InterfaceType GetImpl<Interfa...

How do people get mixin-style re-use in C#?

I come from a C++ background where I can use template mixins to write code that refers to FinalClass which is a template parameter that is passed in. This allows reusable functions to be "mixed-in" to any derived class, by simply inheriting from ReusableMixin with a template paramter of MyFinalClass. This all gets inlined into the clas...

Is it possible to use a primitive type (int) in as a generic type in Java?

Specifically, with a SortedMap<Vector<String>, int> I get "dimensions expected after this (int) token." Help! ...

Java map with values limited by key's type parameter

Is there a way in Java to have a map where the type parameter of a value is tied to the type parameter of a key? What I want to write is something like the following: public class Foo { // This declaration won't compile - what should it be? private static Map<Class<T>, T> defaultValues; // These two methods are just fine ...

How to organize the controller of a MVC-based program with multiple screens/panels?

I'm currently working on a hobby project, written in Java, containing about two different JFrames with about 3-4 JPanels each. The problem I'm facing is that I'm not so sure how to handle the interaction with my controller and different view classes. For instance, I've an article by Sun on Java App. design with MVC, where they list the ...

How do I inherit from multiple extending generic interfaces?

I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public abstract class XMLMessage implements IMessage {} public interface IMessageListener { public void onMessage( IMessage message ); } public interface XMLMessageListener <T extends XMLMessage> extends IMessageListener { public void onMe...