In C# I want to create a list based on a dynamic value type, e.g.:
void Function1() {
TypeBuilder tb = .... // tb is a value type
...
Type myType = tb.CreateType();
List<myType> myTable = new List<myType>();
}
void Function2(Type myType)
{
List<myType> myTable = new List<myType>();
}
This won't comple because List<> want...
This came up as a question I asked in an interview recently as something the candidate wished to see added to the Java language. It's commonly-identified as a pain that Java doesn't have reified generics but, when pushed, the candidate couldn't actually tell me the sort of things that he could have achieved were they there.
Obviously be...
Here's the method I'm trying to write ( doesn't compile now, because what is not seen as an Iterable ):
public <T,V> ArrayList<V> mySelect(T what,ITest<V> x) {
ArrayList<V> results = new ArrayList<V>();
for(V value : what) {
if(x.accept(value)) {
results.add(value);
}
}
return results;
}
...
I have two generic save methods in a repository class:
public void Save<T>(T entity)
{
_session.Save(entity);
}
public void Save<T>(IEnumerable<T> entities)
{
foreach (var item in entities)
{
_session.Save(item);
}
}
However, when I use Save(collection) (which infers the type automatically), it recognizes it a...
The title says it all really.
What's the difference between:
SomeClass<T extends OtherClass>
and
SomeClass<T super OtherClass>
Thanks
...
Hi
I have some linq to sql method and when it does the query it returns some anonymous type.
I want to return that anonymous type back to my service layer to do some logic and stuff on it.
I don't know how to return it though.
I thought I could do this
public List<T> thisIsAtest()
{
return query;
}
but I get this error
Error...
Why isn't a Map<String,List<SomeBean>> castable to Map<String,List<?>>?
What I'm doing now is this:
Map<String, List<SomeBean>> fromMap = new LinkedHashMap<String, List<SomeBean>>();
/* filling in data to fromMap here */
Map<String,List<?>> toMap = new LinkedHashMap<String, List<?>>();
for (String key : fromMap.keySet()) {
toMap....
This question is continue of How to distinguish MethodBase in generics
In brief: I need to distinguish in Dictionary same generic method when it is called for different generic types substitution.
static Dictionary<MethodBase, string> cache = new Dictionary<MethodBase, string>();
static void Method1<T>(T g)
{
MethodBase m1 = Meth...
Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects.
Each method performs differently:
Button1: 15 milliseconds
Button2: 31 milliseconds
Button...
I have;
List<String> stringList = new ArrayList<String>();
List<Integer> integerList = new ArrayList<Integer>();
Is there a (easy) way to retrieve the generic type of the list?
...
Say I have a Customer class with the usual properties: CustomerID, Name, etc.
As a result of a query, I get a generic list of Customer objects: List<Customer>
Is there an elegant way to get an array/list of CustomerID or Name property values from this generic list? (i.e. string[] customerIDs = ???? )
I know I could do a foreach and fi...
Defining a method as
myMethod(Object... obj){}
allows arbitrary number and types of parameters to be used.
I'd love to use generics for strict definition of the number and types of parameters.
For example, Let's assume that the above mentioned myMethod(Object...) is a method in a class named MyClass and MyClass can be instantiated b...
Can you guess what is the reason to not allow sealed classes for type-constraints in generics? I only have one explanation is to give opportunity to use naked constraints.
Sorry, for editing.
I think you need to rephrase the
question title. It should be "Why we
can’t use sealed classes as generic
constraints?" – this.__curious...
Hello there..
I need to create a generic IList where T would be a generic class with various interfaces.
For example ChannelFactory<IService1> or ChannelFactory<IService2> and etc ..
...
I was trying out parsing XML using Fluent interface as described here.
The example is good but gives one compiler error that I am not able to fix.
The error is in the protected constructor below:
protected DynamicXml( IEnumerable<XElement> elements )
{
_elements = new List<XElement>( elements ); // error on this line
}
The two c...
I am reading about Java Generics and I came across this topic where I am a bit confused.
From : http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205
public abstract class Node <N extends Node<N>> {
private final List<N> children = new ArrayList<N>();
private final N parent;
protected Node(N par...
I have a class which has some properties of type List<float>, List<int> etc. Now I am quering the properties of this class through reflection so that I get a list of PropertyInfo.
I want to filter the types which are of type List<>. But the comparison
propertyInfo.PropertyType == typeof(List<>)
fails.
I can get around this by compar...
Here's a Java generic pattern:
public <T> T getResultData(Class<T> resultClass, other_args) {
...
return resultClass.cast(T-thing);
}
A typical call looks like:
DoubleBuffer buffer;
buffer = thing.getResultData(DoubleBuffer.class, args);
I've never been able to figure out how to use this pattern cleanly when the desire...
I have a heterogeneous List that can contain any arbitrary type of object. I have a need to find an element of the List that is of a certain type. Looking through the answers of other generics related questions, I'm not finding exactly what I need.
Here's an example of what I'm trying to accomplish:
List <Object> list = new ArrayList ...
I'm working with some C++/CLI code (new syntax) and am trying to declare a generic type and want to set a member variable to it's default.
In C#:
class Class<T>
{
T member = default(T);
}
What's the equivalent in CLI?
generic<typename T> public ref class Class
{
public:
Class() : member(default(T)) // <-- no worky
{
...