I was looking at the answer of this question regarding multiple generic types in one container and I can't really get it to work: the properties of the Metadata class are not visible, since the abstract class doesn't have them. Here is a slightly modified version of the code in the original question:
public abstract class Metadata
{
}
...
Hello world!
Using the following code:
Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T) As T
Return If(Configuration.ContainsKey(SettingName), CType(Configuration(SettingName), T), DefaultVal)
End Function
Yields the following error:
Value of type 'String' cannot be converted to 'T'.
Any way I coul...
Hi, this code doesn't compile. I'm wondering what I am doing wrong:
private static Importable getRightInstance(String s) throws Exception {
Class<Importable> c = Class.forName(s);
Importable i = c.newInstance();
return i;
}
where Importable is an interface and the string s is the name of an implementing class.
The compiler says:
....
I am working on rewriting my fluent interface for my IoC class library, and when I refactored some code in order to share some common functionality through a base class, I hit upon a snag.
Note: This is something I want to do, not something I have to do. If I have to make do with a different syntax, I will, but if anyone has an idea on ...
I'm becoming increasingly frustrated with the limits of type-erased Java generics. I was wondering if there was a custom Java Compiler that provided a full version of generics without the quirks associated with type-erasure?
Chris
...
So strange! Please have a look the code first:
public class A {
}
public class B extends A {
}
public class C extends A {
}
public class TestMain {
public <T extends A> void test(T a, T b) {
}
public <T extends A> void test(List<T> a, List<T> b) {
}
public void test1(List<? extends A> a, List<? extends A> b) {
}
public ...
After using C++ I got used to the concept of Identifier which can be used with a class for the type, provides type safety and has no runtime overhead (the actual size is the size of the primitive). I want to do something like that, so I will not make mistakes like:
personDao.find(book.getId());//I want compilation to fail
personDao.fi...
Given a particular interface ITarget<T> and a particular type myType, here's how you would determine T if myType implements ITarget<T>. (This code snippet is taken from the answer to an earlier question.)
foreach (var i in myType.GetInterfaces ())
if (i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(ITarget<>))
...
Hi all,
I need to represent the unit of Percent per second using the JScience.org's JSR 275 units and measures implementation. I am trying to do to the following:
Unit<Dimensionless> PERCENT_PER_SECOND = NonSI.PERCENT.divide(Si.SECOND).asType(Dimensionless.class)
but I am getting a ClassCastException when I try to do that.
The fo...
In C# I am trying to write code where I would be creating a Func delegate which is in itself generic. For example the following (non-Generic) delegate is returning an arbitrary string:
Func<string> getString = () => "Hello!";
I on the other hand want to create a generic which acts similarly to generic methods. For example if I want ...
I want to call a generic method that constrains the input type T to implement two interfaces:
interface IA { }
interface IB { }
void foo<T>(T t) where T : IA, IB { }
How can I fix the last line of
void bar(object obj)
{
if (obj is IA && obj is IB)
{
foo((IA && IB)obj);
}
}
?
Reflection probably allows to do th...
I've been dealing a lot lately with abstract classes that use generics. This is all good and fine because I get a lot of utility out of these classes but now it's making for some rather ugly code down the line. For example:
abstract class ClassBase<T>
{
T Property { get; set; }
}
class MyClass : ClassBase<string>
{
OtherClass P...
In some interfaces i wrote I'd like to name generic type parameter with more than one character to make the code more readable.
Something like....
Map<Key,Value>
Instead of this...
Map<K,V>
But when it comes to methods, the type-parameters look like java-classes which is also confusing.
public void put(Key key, Value value)
Thi...
After a bit of programming one of my classes used generics in a way I never seen before. I would like some opinions of this, if it's bad coding or not.
abstract class Base<T> : where T : Base<T>
{
// omitted methods and properties.
virtual void CopyTo(T instance) { /*code*/ }
}
class Derived : Base<Derived>
{
override void ...
I noticed in C#, unlike C++, you can combine virtual and generic methods. For example:
using System.Diagnostics;
class Base {
public virtual void Concrete() {Debug.WriteLine("base concrete");}
public virtual void Generic<T>() {Debug.WriteLine("base generic");}
}
class Derived : Base {
public override void Concrete() {Debug...
In Java, the Collections class contains the following method:
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> c)
Its signature is well-known for its advanced use of generics,
so much that it is mentioned in the Java in a Nutshell book
and in the official Sun Generics Tutorial.
However, I could n...
I have a simple extension method that I would like to use to add an item to an array of items.
public static T[] addElement<T>(this T[] array, T elementToAdd)
{
var list = new List<T>(array) { elementToAdd };
return list.ToArray();
}
this works ok, but when I use it, I am having to set the array equal to the return value. I se...
private List<T> GetFieldList()
{
var Fields = new { DisplayName = "MCP", FieldName = "t.MCP", FieldType = 1 };
var FieldList = (new[] { Fields }).ToList();
return FieldList;
}
Should I be able to do something like this?
...
Please see an example of my code below:
CODE UPDATED
public class ScrollableCheckboxList
{
public List<ScrollableCheckboxItem> listitems;
public ScrollableCheckboxList<TModel>(IEnumerable<TModel> items, string valueField, string textField, string titleField) where TModel : class
{
listitems = new List<ScrollableChe...
This is a scenario created to help understand what Im trying to achieve.
I am trying to create a method that returns the specified property of a generic object
e.g.
public object getValue<TModel>(TModel item, string propertyName) where TModel : class{
PropertyInfo p = typeof(TModel).GetProperty(propertyName);
return p.GetValue...