I’ve create a generic method like
public void BindRecordSet<T>(IEnumerable<T> coll1, string propertyName)
where T : class
and in my class ‘T’ i’ve write indexer
public object this[string propertyName]
{
get
{
Type t = typeof(SecUserFTSResult);
PropertyInfo pi = t.GetProperty(propertyName);
return pi.GetValue(this, null);
}
set
{
Type t = typeof(SecUserFTSResult);
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(this, value, null);
}
}
now in my method when i write code like
var result = ((T[])(coll1.Result))[0];
string result= secFTSResult[propertyName];
I am getting the error
Cannot apply indexing to an expression of type ‘T’
Please help
Thanks
Unless you use a generic constraint to an interface which declares the indexer, then indeed – that won’t exist for abitrary
T. Consider adding:and:
and:
(feel free to rename
IHasBasicIndexerto something more sensible)Or a simpler alternative in 4.0 (but a bit hacky IMO):
(which will resolve it once per
Tat runtime)