What I used to do is create a case switch that sorts my LINQ in this format:
List<products> productList = GetAllLists();
switch (sortBy)
{
case "name":
return productsList.OrderBy(pl => pl.Name);
case "date":
return productsList.OrderBy(pl => pl.DateCreate);
}
which, in the long run, becomes cumbersome.
I wanted to hav...
(This is somewhat a followup to my previous question)
I've got a Foo<?> object, foo. Foo<T> is an interface.
How to get the type value hidden behind the <?>?
Note that this is not trivial, as foo can be for example an object of class Bar<String>, where Bar<T> implements Foo<T>, or some anonyomus class implementing interface FloatFoo, ...
Consider the following structures:
internal struct Coordinate
{
public Double Top { get; set; }
public Double Left { get; set; }
}
internal struct Dimension
{
public Double Height { get; set; }
public Double Width { get; set; }
}
internal struct Property
{
public Boolean Visible { get; set; }
internal String La...
So for starters lets say that I have a LinkedList<String>,
I can easily convert it to an array via toArray(). i.e.
LinkedList<String> strList = new LinkedList<String>();
String[] strArray = strList.toArray(new String[0]);
But Lets say I have a LinkedList<T>
Then I the following code:
LinkedList<T> tList = new LinkedList<T>();
T[] s...
Suppose I have a family of types which support a given member function, like the Property member below:
type FooA = {...} with
member this.Property = ...
type FooB = {...} with
member this.Property = ...
Suppose the member Property returns an integer for each of the the above types. Now, I wan to write a generic function th...
In the context of C#, .Net 4...
Given a data source object that supplies vertices by index from an array of doubles where a vertex contains ten doubles with members Px, Py, Pz, Nx, Ny, Nz, S, T, U, V. and the backing array contains all or any subset of vertex members based on the data source's stride, offset and count properties. The da...
I have some code that creates a list of lists. The list is of this type:
List<List<Device>> GroupedDeviceList = new List<List<Device>>();
But need to return the result in the following type:
IEnumerable<IGrouping<object, Device>>
Is this possible via a cast etc or should I be using a different definition for my list of lists?
...
I have a bunch of Repository classes which all look a bit like the following. Note that I have omitted certain methods; I just want you to get a flavour.
public class SuggestionRepository : ISuggestionRepository
{
private IUnitOfWork _unitOfWork;
public SuggestionRepository(IUnitOfWork unitOfWork)
{
_unitOfWork = un...
I'm struggling with some Generic constraint issues when trying to implement a library that allows inheritance and hoping someone can help.
I'm trying to build up a class library that has 3 flavours to it, each building on top of the other. To me it seemed like a perfect opportunity to use Generics as I can't quite do what I want throug...
In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code.
Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity:
Interface:
IQueryable<Suggestion> All { get; }
Generic ba...
I have several message queues that have specific messages on them.
I've created classes for these messages using xsd.exe.
I can receive a message syncronously and deseriazlise it with this method:
public oneOfMyTypes DeserializeMessage(XDocument message)
{
var serializer = new XmlSerializer(typeof(oneOfMyTypes));
var entity = (oneOfMy...
In the code below the "Move" public class derives fromthe generic type "Submit". "Submit" is a method, part of the DSS model, which handles messages and accepts two parameters, one is the message body and one is the message response.
My question is: How or WHY does a class derive from a method?!
It seems to me (since i'm only a beginne...
Hi
As per the title really, how can you set a dependency property in XAML when the base class is generic? When trying to do this I get a NullReferenceException, setting the property from code behind works fine. It also works when the base class is not generic.
I'm using .NET4
Here is some sample code to demonstrate:
WindowBase.cs
usi...
I'm trying to create a generic extension method, that works on typed data tables :
public static class Extensions
{
public static TableType DoSomething<TableType, RowType>(this TableType table, param Expression<Func<RowType, bool>>[] predicates)
where TableType : TypedTableBase<RowType>
where RowType : DataRow
{
...
hi all it's simple class declaration public class test but i don't understand it public class test<T> .
...
Possible Duplicates:
Solution for overloaded operator constraint in .NET generics
Define a generic that implements the + operator
Hi,
So I have a generic class MyClass<T> and I want to put a constraint that the type T must define an operator, for example public static T operator +(T x,T y).
Is that possible?
If not, I was...
I just ran into this situation and i thought it was a nice opportunity to use the default keyword. But it doesn't compile and i can't think of why. The example below illustrates my problem:
public class Test<TDataSource>
{
public IQueryable<TDataSource> DataSource { get; set; }
public bool GetOneOrDefaultResult()
{
...
I have a function that uses reflection to set properties of object A from object B.
At one point, I need to instantiate a generic collection. However, I am unable to get it working. Here is what I have now:
IList list = destProperty.PropertyType.GetGenericTypeDefinition()
.MakeGenericType(destProperty.PropertyType.GetGen...
With the structure..
abstract class Unit
{
int Id;
}
class Measure : Unit
{
int Current;
int Baseline;
}
class Weight : Unit
{
int Minimum;
int Maximum;
int Current;
}
I basically want to add an "Add" method for adding, say, two Measures together, or adding two Weights together. But it needs to be in the Unit base class. So ba...
Hi there,
I am writing a very simple asynchronous helper class to go along with my project. The purpose of the class is that it allows a method to be run on a background thread. Here is the code;
internal class AsyncHelper
{
private readonly Stopwatch timer = new Stopwatch();
internal event DownloadCompleteHa...