|
Here is an abstraction and simplification of my issue:
I have a set of toys and a corresponding box for these toys. I want the user to be able to specify the largest type of toy that the box can hold:
public class Box<T> {}
then within the Box class...
Started by Chris on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Either the interface about, you're going to need ....
The reason you need this is that if you use a single class the generic Toy, with each specific type of Toy implementing the interface...
Or base class can define the generic list.
|
|
I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List<ObjectType> based on the incoming List object so that my calling code is talking...
Started by Shaun F on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
For example, suppose you are calling a method in an older library the generic parameters....
You can simply cast the list:
List raw = new ArrayList(); List<String> generic = (List< for generic types, use a wildcard.
|
|
This is my set up,
class CostPeriodDto : IPeriodCalculation { public decimal? a { get; set; } public decimal? b { get; set; } public decimal? c { get; set; } public decimal? d { get; set; } } interface IPeriodCalculation { decimal? a { get; set; } decimal...
Started by Simon Wright on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Use Cast<IPeriodCalculation>() :
public class CostPeriodDto : IPeriodCalculation { public decimal? a { get; set; } public decimal? b { get; set; } public decimal? c { get... .
Try return this.costPeriodList.Cast<IPeriodCalculation>().ToList().
|
Ask your Facebook Friends
|
Running .NET 2.0, I have a generic method with the following signature:
static listType FillCollection<listType, objType>(IDataReader dr) where listType : ICollection<objType>, new()
it's purposes is to translate a datareader into a collection...
Started by Wes P on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Var list = new List<MyObject>(); var data = FillCollection<MyObject>(dr, list);
....
Static listType FillCollection<objType>(IDataReader dr, ICollection<objType> list the collection outside of FillCollection.
|
|
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<...
Started by errcw on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
list = new List<SomeClass<T>>(); }
The other option would be to use an interface:
class C { void Method<T>(T obj) where T : ISomeClass { list.Add(obj); } List<ISomeClass> list type, you could make the class itself....
|
|
I have a generic class that has one type parameter (T). I needed to store a collection of these generic objects that are of different types, so I created an interface that the generic class implements as suggested here . There is a property in the generic...
Started by thedugas on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Use a different generic type as needed class XmlUser : ISomeClassUser<string> { public.
|
|
I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:
public class Thing { public Collection<String> getStuff() { ... } }
getStuff() uses generics to return a collection of strings. Therefore...
Started by Steve Kuo on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Instead of Thing<?> ( parameterized type ) the Java compiler strips out all generic arguments, even thogh (as in your case) the generic type of the method has nothing to do with the generic type.
|
|
So I have a generic list, and an oldIndex and a newIndex value.
I want to move the item at oldIndex, to newIndex...as simply as possible.
Any suggestions?
Note The item should be end up between the items at (newIndex - 1) and newIndex before it was removed...
Started by Richard Ev on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
Var item ....
I would expect either:
// Makes sure item is at newIndex after the operation T item = list[oldIndex at newIndex - 1 T item = list[oldIndex]; list.RemoveAt(oldIndex); newIndex = (newIndex > oldIndex on this machine to check.
|
|
I have a generic List (of Foo) which contains n objects of Type Foo. One of the properties of Foo is PropertyA. PropertyA can be one of ValueA, ValueB or ValueC. Is there an easy way of splitting this into three seperate Lists, one for ValueA, one for...
Started by Simon on
, 7 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
Something along the lines of
Dim dic as New Dictionary(Of TypeOfYourValues, List(Of Foo)) For Each e As Foo In myList If Not dic.ContainsKey....
A generic algorithm implementing GroupBy shouldn't be too difficult, though.
To resort to your loop idea.
|
|
I'm looping all the properties in an object via reflection:
For Each p As PropertyInfo In values.[GetType]().GetProperties() If p.CanRead Then 'Do stuff End If Next
Can anyone tell me how to determine whether the property in question is a generic List...
Started by Simon on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Sorry Class Public Class ContainerClass Public Property objects() As List(Of Object) Get End Get Set End class instance Dim c As New ContainerClass() '....
Try this complete console application.
If p.PropertyType = TypeOf List(Of T) then...
|