Hey SO,
I am trying to create a User Control that accepts a generic List of CustomObject as a bindable property. I've hit a wall and can't figure it out. Any help would be greatly appreciated!
I'd like it to look a little somthing like this :
TabularReport.ascx
[Bindable (true)]
public IList<T> Source
{
get;
set;
...
I have the following scenario:
class Foo { }
class Foo<T> : Foo { }
And then two methods
void DoStuff(Foo foo)
{
DoStuffImpl(foo);
}
void DoStuffImpl(Foo foo)
{
Console.WriteLine("A");
}
void DoStuffImpl<T>(Foo<T> foo)
{
Console.WriteLine("B");
}
void Main()
{
DoStuff(new Foo<int>()); // prints A
}
...
I'm creating a Settings object in my app, used for storing user-defined settings and whatnot. I plan on using various datatypes for my setting fields (ints, strings, enums, anything Serializable, really).
I'd also like, if possible, a type-safe way to set any of the settings. My proposed method would be doing something like this:
Tuple...
Ok, i don't think this is possible so I thought I would ask to make sure. I'm in the process of creating a function which reflects over the properties in a class and adds them to this data structure I have. Some of the properties are generic types.
So say we have DataType(Of T) that has a .Value property of type T:
Dim properties = Ge...
I know this is probably a really simple question but I'm having a brain fart at the moment. I am trying to create a method that can take one of 2 custom types. Basically the body of this method will be identical for both the types as they both have a Name property (I'm doing a comparison on the Name property to use in sorting). How shoul...
Was practicing Generics. Consider a stack method below. What is the best way of doing error checking other than throwing exceptions in a generic method. What if I want to return some result in this method.
public T pop()
{
if (top >= 0)
return arr[top--];
return -1 or null;
}
...
Is there a way to constraint a generic type to only integral type?
In example, if I have a method
T[] sort<T>(T[] data) where : T ... {}
what would I put as constraint if I wanted to ensure the parameter will be some sort of integral type?
I have tryed looking at MSDN however it does not seem to mention anything about constraining a...
So I've got these two methods
private static void AddOrUpdate(Computer input)
{
if (Simple.Repository.Exists<Computer>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
private static void AddOrUpdate(User input)
{
if (Simple.R...
Given the following
public class Service<T> : IService<T>
{
Repository<T> _repository = new Repository<T>();
public T Get<T>(int id)
{
return _repository.Get<T>(id);
}
}
public interface IService<T>
{
T Get<T>(int id);
}
I get the following warning
Type parameter 'T' has the same...
I've got a number of classes that implement a specific interface (ISearchable) and I'd like to return an IEnumerable of the base type (ISearchable) from a static method, but I'm not sure how to convert it without making intermediate collections.
The code is pretty simple, one of the domain objects' implementations is like so:
public cl...
Possible Duplicate:
Get generic type of java.util.List
I have a Map and I want to get the type of T from an instance of that Map. How can I do that?
e.g. I want to do something like:
Map<String, Double> map = new HashMap<String, Double>();
...
String vtype = map.getValueType().getClass().getName(); //I want to get Double her...
I would like some variant of this code to compile in java.
class X
{
List<X> getvalue(){...};
}
class Y extends X
{
List<Y> getvalue(){...};
}
Javac (1.6) returns an error because List<Y> and List<X> are not compatible.
The point is that I would like the compiler to
recognize that List<Y> is a compatible return
type to List<...
How do I get the actual type of T in a generic List at run time using reflection?
...
The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.
Can you guys give me a sample scenario when this constraint is needed?
...
Please, help me to explain the following behavior:
dynamic d = 1;
ISet<dynamic> s = new HashSet<dynamic>();
s.Contains(d);
The code compiles with no errors/warnings, but at the last line I get the following exception:
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.ISet<object>'...
I'm look for good alternatives to invoking a specific interface from a generic framework. I'll exemplify with code. Look to the question part, the example code is primarily included for thoroughness, and for putting the example into a real scenario.
Example
Assume we want to build a report based on a list of components. Say we have tw...
Hey, I'd like to know if what I'm trying to do is even possible? Comments in code should give and idea what I'm trying to achive :)
interface ITest<T> {
T t { get; }
bool DoTest();
}
public abstract class Test<T> : ITest<T> {
public Test (T nt) {
this.t = nt;
}
public Test () {
}
public T t {
...
I have a lambda selector, lets say Func<T, TResult>. Is it possible to convert it into a predictor (Func<T, bool>) using a TResult object as reference?
For example, convert
x => x.Name
into
x => x.Name == customerName
...
I am starting to play with extension methods and i came across with this problem:
In the next scenario i get a:
"extension method has a type constraint that can never be satisfied"
Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey))
ReadOnly Property InstanceKey() As TKey
End Interface
<Extension()> _
Public Function To...
I've been fighting with trying to override a method in a generic abstract class.
public abstract class Grandparent<T extends Grandparent>
public T set(final T other) //does stuff I don't want to do
public abstract class Parent<T extends Parent<T>> extends Grandparent<T>
public T set(final Parent<?> other) // does stuff I wan...