I have the following extension method, and would like to make it more generic so I don't have to implement it for every class in our domain.
public static IList<User> ToList(this DataTable table)
{
IList<User> users = new List<User>();
foreach (DataRow row in table.Rows)
users.Add(User.FromDataRow(row));
return use...
I have a system which I've been wrestling with for a while. Essentially, it uses a lot of abstraction to deal with the fact that later extension is not just expected, but necessary. One place this is required is data access. The system generally deals with managing objects encapsulating some observation (in the sense of an observed value...
I have written the following method to return a list of Unserializable classes (LINQ classes) from a list of Serializable classes (POCOs):
List<UnSerializableEntity> ToListOfUnserializables(List<SerializableEntity> entityList)
{
var tempList = new List<UnSerializableEntity>();
entityList.ForEach(e =>
{
if (e != null)...
Given an extension method like this:
Public Sub RehydrateTo(Of T As New)(ByVal input As String, ByRef output As T)
Dim ms As MemoryStream = MsFromString(input)
Dim f As New DataContractSerializer(GetType(T))
Try
output = CType(f.ReadObject(ms), T)
Catch ex As SerializationException
output = New T
Dim ild ...
I have an existing application that uses Active Record for its data retrieval. It's in VB.NET (first time I'm doing VB.NET; I usually work in C#). And I'm building a method to return a List(Of T) of an object.
The current pattern uses a SQLDataAdapter to populate a datatable. I COULD add the record to the List(Of T) as I fill the datat...
Duplicate
What are the differences between Generics in C# and Java… and Templates in C++?
Hi all,
I am experienced C++ programmer but quite new to C#.
Whats up with those constraints and generics? Why doesn't it work the same as in C++ where constraints are implicit and derived from the instantiations you do to the template cla...
I have a collection class that inherits from List<>. I've set up a function to sort the list by a certain property like so:
public PlaylistCollection SortByName(IEnumerable<Playlist> playlists)
{
return (PlaylistCollection)playlists.OrderBy(p => p.Name);
}
When I try to use the sorted results in my code like this:
artistSource.Pl...
I want to have a Class object, but I want to force whatever class it represents to extend class A and implement interface B.
I can do:
Class<? extends ClassA>
Or:
Class<? extends InterfaceB>
but I can't do both. Is there a way to do this?
...
How come in java we cannot do:
List<List<? extends Number>> aList = new ArrayList<List<Number>>();
Even though this is OK:
List<? extends Number> aList = new ArrayList<Number>();
Compiler error message is:
Type mismatch: cannot convert from ArrayList<List<Number>> to List<List<? extends Number>>
...
Where do JVM Implementations differ (except licensing)?
Does every JVM implement Type Erasure for the Generic handling?
Where are the differences between:
JRockit
IBM JVM
SUN JVM
Open JDK
Blackdown
Kaffe
.....
Deals one of them with Tail-Call-Optimization?
...
Hi all,
public IList<T> GetClientsByListofID<T>(IList<int> ids) where T : IClient
{
IList<T> clients = new List<T>();
clients.Add( new Client(3));
}
I am getting a compiler error here:
cannot convert from 'Bailey.Objects.Client' to 'T'
The client object implements the IClient interface. My goal here is to try and loosen t...
I can do
public class MyGenericClass : DL
//but i cannot do
public class MyGenericClass <T> : T
How would i do the second? if i cannot do that, how can i do something like
public class MyGenericClass <T>
{
T obj;
//have all MyGenericClass.XYZ call obj.XYZ
}
...
I want to create an array of Classes, each representing a type that is available in the system I'm building. All the Classes involved are subclasses of a common superclass. So I'd like to do:
Class<? extends SuperClass>[] availableTypes = { SubClass1.class, SubClass2.class };
This gives me the error:
Cannot create a generic array o...
In C# I can initialize a list using the following syntax.
List<int> intList= new List<int>() { 1, 2, 3 };
I would like to how that {} syntax works, and if it has a name. There is a constructor that takes an Ienumerable, you could call that.
List<int> intList= new List<int>(new int[]{ 1, 2, 3 });
That seems more "standard". When I d...
This code compiles fine in Java <= 1.4. Java 1.6 bitches and moans with the warning:
"The method add(Object) belongs to the raw type Collection. References to generic type Collection should be parameterized"
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUtils;
import org.apache.commons.collec...
I have class where the relevant part looks like
class C {
void Method<T>(SomeClass<T> obj) {
list.Add(obj);
}
List<?> list = new List<?>();
}
How should I define the list so that the class compiles?
I want a list of type List<SomeClass<?>>, that is a list of objects of SomeClass where each object can have any type...
I have a string serialization utility that takes a variable of (almost) any type and converts it into a string. Thus, for example, according to my convention, an integer value of 123 would be serialized as "i:3:123" (i=integer; 3=length of string; 123=value).
The utility handles all primitive type, as well as some non-generic collectio...
Given a generic class definition like public class ConstrainedNumber<T>: IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>, IComparable where T:struct, IComparable, IComparable<T>, IEquatable<T>, how can I define arithmetic operators for it?
The following does not compile, because the '+'...
Who is right? Eclipse or javac?
--------------- c/v/A.java ---------------
package c.v;
public class A<T> {
}
--------------- c/v/B.java ---------------
package c.v;
public class B extends A<B.Secret> {
private class Secret {};
}
Eclipse compiles B.java just fine.
Javac has a problem.
$ javac c/v/B.java
c/v/B.java:3: c.v.B.Sec...
Hi,
I'm trying to output an object graph via reflection. In it there are several generic types (lists, dictionaries). I do not know the types (string, object, etc.) they contain but want to list them (using .ToString()).
So, is there a way to output a generic list / dictionary in generic way, that means without writing overloaded func...