generics

C# - How to get class of Type

I need to use method like: DoSomething<(T)>(); But i don't know which Type i have, only object of class Type. How can i call this method if I have only: Type typeOfGeneric; ...

Why does my WCF service return and ARRAY instead of a List <T> ?

In the web servce I say public List<Customer> GetCustomers() { PR1Entities dc = new PR1Entities(); var q = (from x in dc.Customers select x).ToList(); return q; } (customer is a entity object) Then I generate the proxy when I add the service.. and in the reference.cd it say public wcf...

In C# how can I serialize a List<int> to a byte[] in order to store it in a DB field?

In C# how can I serialize a List<int> to a byte[] in order to store it in a DB field? I know how to serialize to a file on the disk, but how do I just serialize to a variable? Here is how I serialized to the disk: List<int> l = IenumerableofInts.ToList(); Stream s = File.OpenWrite("file.bin"); Binar...

Why does this implicit type conversion in C# fail?

Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public Wrapped(T obj) { /* ... */ } public static implicit operator Wrapped<T>(T obj) { return new Wrapped<T>(obj); } public void Dispose() { /* ... */ } } As you can see, it provides an implicit type conversion ope...

Accessing generic lists with delegate notation

I see some people write: //wordList is List<string> wordList.ForEach(delegate(string word){ Console.WriteLine(word);}); instead of: foreach(string word in wordList) { Console.WriteLine(word); } What is the advantage in doing so. Also I couldn't fathom the Action delegate syntax given above though I have used delegates in C#...

Help With Generics? How to Define Generic Method?

Is it possible to create a generic method with a definition similar to: public static string GenerateWidget<TypeOfHtmlGen, WidgetType>(this HtmlHelper htmlHelper , object modelData) // TypeOfHtmlGenerator is a type that creates custom Html tags. // GenerateWidget creates custom Html tags whi...

Retrieving the type of a Collection

So I have something like the following in Java: private List<SomeType>variable; // ....variable is instantiated as so ... variable = new ArrayList<SomeType>(); // there's also a getter public List<SomeType> getVariable() { /* code */ } What I would like to be able to do is figure out that variable is a collection of SomeType progra...

Can I Create A Generic Method of a Type of Interface?

Is it possible to create a generic method with a signature like public static string MyMethod<IMyTypeOfInterface>(object dataToPassToInterface) { // an instance of IMyTypeOfInterface knows how to handle // the data that is passed in } Would I have to create the Interface with (T)Activator.CreateInstance();? ...

How do I determine if a value is an instance of a generic type, ignoring the type parameter, in vb.net?

I have a class C(Of T). I want to determine if some given value has type C, regardless of what T is. For example, I might want to determine if a value is a strongly-typed list, regardless what type of items the list stores. I just need to know how to do it in VB.net. In Java the syntax is like this: var result = obj instanceof Gen2<?>;...

How Can I List a TDictionary in Alphabetical Order by Key in Delphi 2009?

How can I use a TEnumerator to go through my TDictionary in sorted order by key? I've got something like this: var Dic: TDictionary<string, string>; Enum: TPair<string, string>; begin Dic := TDictionary<string, string>.create; Dic.Add('Tired', 'I have been working on this too long'); Dic.Add('Early', 'It is too...

Is it possible to specify both upper and lower bound constraints on type parameters in Java?

Is it possible to specify both upper and lower bound constraints on type parameters in Java? I found a conversation in Sun's forum in which this issue was discussed (apparently before the generics feature was finalized), but there was no final answer. In summary, is there a valid syntax to do this: public class MyClass<T extends Numbe...

calling Overloaded method from a generic method.

How to create a generic method which can call overloaded methods? I tried but it gives a compilation error. Test.java:19: incompatible types found : java.lang.Object required: T T newt = getCloneOf(t); ^ import java.util.*; public class Test { private Object getCloneOf(Object s) ...

Casting Type array to Generic array?

The short version of the question - why can't I do this? I'm restricted to .NET 3.5. T[] genericArray; // Obviously T should be float! genericArray = new T[3]{ 1.0f, 2.0f, 0.0f }; // Can't do this either, why the hell not genericArray = new float[3]{ 1.0f, 2.0f, 0.0f }; Longer version - I'm working with the Unity engine here, alth...

Can I pass a non-generic type where a generic type is expected?

I want to define a set of classes that collect and persist data. I want to call them either on-demand basis, or in a chain-of-responsibility fashion, as the caller pleases. To support the chaining, I have declared my interface like so: interface IDataManager<T, K> { T GetData(K args); void WriteData(Stream stream); void Wri...

Restrict a generic type

I want to restrict the generic type parameter to: 1) either that of a certain user defined reference type; OR 2) any of the primitive types in the CLR; How do I say something to the effect of: interface IDataManager<T>: IDataManager where T: IDataObject, T: ValueType ...

Semi-generic function

I have a bunch of overloaded functions that operate on certain data types such as int, double and strings. Most of these functions perform the same action, where only a specific set of data types are allowed. That means I cannot create a simple generic template function as I lose type safety (and potentially incurring a run-time problem ...

Java: Why is this Subclass valid?

Here, I have an abstract class: abstract class A<E extends A> { abstract void foo(E x); } Here's a class that extends A: class B extends A<B>{ void foo(B x){} } And here's another (E is B here on purpose): class C extends A<B>{ void foo(B x){} } Both of those classes are valid, and the reasoning for that makes sense ...

Casting a non-generic type to a generic one

I've got this class: class Foo { public string Name { get; set; } } And this class class Foo<T> : Foo { public T Data { get; set; } } Here's what I want to do: public Foo<T> GetSome() { Foo foo = GetFoo(); Foo<T> foot = (Foo<T>)foo; foot.Data = GetData<T>(); return foot; } What's the easiest way to conv...

Using a Type object to create a generic

Hello all! I'm trying to create an instance of a generic class using a Type object. Basically, I'll have a collection of objects of varying types at runtime, and since there's no way for sure to know what types they exactly will be, I'm thinking that I'll have to use Reflection. I was working on something like: Type elType = Type.Get...

Why does C# (4.0) not allow co- and contravariance in generic class types?

What is the real reason for that limitation? Is it just work that had to be done? Is it conceptually hard? Is it impossible? Sure, one couldn't use the type parameters in fields, because they are allways read-write. But that can't be the answer, can it? The reason for this question is that I'm writing an article on variance support in ...