I'm developing an application where I the need to invoke a method of a generic class and I don't care about the instances actual type. Something like the following Java code:
public class Item<T>{
private T item;
public doSomething(){...}
}
...
public void processItems(Item<?>[] items){
for(Item<?> item : items)
item.doSomethi...
Due to the implementation of Java Generics you can't have code like this. How can I implement this while maintaining type safety?
public class GenSet<E> {
private E a[];
public GenSet()
{
a = new E[INITIAL_ARRAY_LENGTH];
}
}
I saw a solution on the java forums that goes like this:
import java.lang.refl...
Notice the following code. The offending line has been commented out.
interface I<R> { }
class C : I<int> { }
class Program
{
private static void function<T, R>(T t) where T : class, I<R>
{
}
static void Main(string[] args)
{
// function(new C()); // wont compile
function<C, int>(new C());
}
}
...
I have a generic collection of type MyImageClass, and MyImageClass has an boolean property "IsProfile". I want to sort this generic list which IsProfile == true stands at the start of the list.
I have tried this.
rptBigImages.DataSource = estate.Images.OrderBy(est=>est.IsProfile).ToList();
with the code above the image stands at the ...
When writing xml documentation you can use <see cref="something">something</see>, which works of course. But how do you reference a class or a method with generic types?
public class FancyClass<T>
{
public string FancyMethod<K>(T value) { return "something fancy"; }
}
If I was going to write xml documentation somewhere, how would I...
The following code compiles:
class Testing<TKey, TValue>
{
public bool Test(TKey key)
{
return key == null;
}
}
However, TKey can be a value type, and possibly not allow the value "null".
I know the results of this program, and how to add constraints. What I'm wondering is why doesn't the compiler disallow this...
In some cases I've been using a DataTable, filtering it witha DataView and displaying the DataView in a DataGrid. I've recently started switching to using my own classes. For example:
[Serializable]
[System.Xml.Serialization.XmlRoot("Items", Namespace = "http://mycomp.com/test")]
public class Items: List<Item>
{
}
[Serializable]
[Sys...
I have a query like:
var function = GetSomeExpression();
using (FooModel context = new FooModel())
{
var bar = context.Bar.Where(function);
}
I'd like to make a generic method that can execute Where against different Entities in the context. The goal is not having to do context.Bar.Where, context.Car.Where, Context.Far.Where...
What is the best way to make a generic equivalence function for two Dictionaries, whose keys and values are value types?
Right now I have a Dictionary<string, bool>, and have created an extension method that (I think) works to test for equivalence between two Dictionary<string, bool>.
I wanted to make it more generic. And my first tho...
I need to specify that a Generic type should only accept enumerated types only in the closed type. Can anyone suggest a way to do this if contraints cannot work?
...
I'd like to create a generic type hierarchy for representing graphs. In particular, I'd like like to have classes Graph and Node, and I want that for every Graph type, there is a corresponding Node type and if I create a generic function for manipulating Graphs, I want this function to use the actual Node type. An example that I tried
t...
Through reflection, is there some way for me to look at a generic List's contained type to see what type the collection is of? For example:
I have a simple set of business objects that derive from an interface, like this:
public interface IEntityBase{}
public class BusinessEntity : IEntityBase
{
public IList<string> SomeString...
I've got an IList<DerivedClass> that I want to cast to ICollection<BaseClass> but when I attempt an explicit cast, I get null. Is it possible to do this without creating and populating a new collection?
Edit:
Since I only want to read from the collection, I switched to using a generic method:
public void PopulateList<BaseClass>(IColle...
So I am utilizing CollectionBase as an inherited class for custom collections. I am utilizing CollectionBase through an abstract class so that I don't repeated knowledge (following the DRY principle). The abstract class is defined as a generic class also. Here is how I am implementing my class:
public abstract class GenericCollecti...
I have a custom class file in C# that I inherited and partially extended. I am trying to re factor it now as I have just enough knowhow to know that with something like generics(I think) I could greatly condense this class.
As an inexperienced solo dev I would greatly appreciate any direction or constructive critism any can provide. ...
I am using Jon Skeet's very clever SmartEnumerable. I recommend checking it out if you haven't already seen it.
The class is defined :
public class SmartEnumerable<T> : IEnumerable<SmartEnumerable<T>.Entry>
The constructor is :
public SmartEnumerable(IEnumerable<T> enumerable)
and you use it by saying :
new SmartEnumerable<Cat>(m...
public static IList<T> ConvertTo<T>(DataTable table)
{
if (table == null)
{
return null;
}
List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
{
rows.Add(row);
}
return ConvertTo<T>(rows);
}
public static ...
I've a List of this type List> that contains this
List<int> A = new List<int> {1, 2, 3, 4, 5};
List<int> B = new List<int> {0, 1};
List<int> C = new List<int> {6};
List<int> X = new List<int> {....,....};
I want to have all combinations like this
1-0-6
1-1-6
2-0-6
2-1-6
3-0-6
and so on.
According to you is This possibile to resolv...
I've a problem!
I've a
List <List<Class>> L = new List<List<Class>>();
where Class contains
IDLinea
Posizione
And so on...
I want to have a Distinct List<List<Class>> Where there are no objects with same lines.
For example:
List <List<Class>> L = new List<List<Class>>();
var A = new List<Class>();
Class C = new Class();
C.ID...
I have a function which I need to call for three different types, with the underlying logic remaining the same for all the different types, so I figured it would be best to write this function using generics.
Here is the basic outline of the classes and functions involved:
'PO Base class'
Public MustInherit Class ProductionOrder
P...