For the sake of “post processing” on a method, I want to import an extra function into a method.
How can I import a Func that returns an anonymous Type as a parameter for a .Select extension method?
The expression is anounymous like:
p => new
{
ThumnailUrl = p.PicasaEntry.Media.Thumbnails[0].Attributes["url"],
ImageUrl = p.PhotoUri
}
and needs to be used at parameter ????? and performed at .Select(?????)
private void BindControl<T, U>(string uri, DataBoundControl target, ?????)
where T : KindQuery, new()
where U : PicasaEntity, new()
{
PicasaFeed feed = CreateFeed<T>(uri);
albumList.DataSource = GetPicasaEntries<U>(feed).Select(?????);
albumList.DataBind();
}
update:
finally I want to call it like:
string albumUri = PicasaQuery.CreatePicasaUri(PicasaUserID, PicasaAlbumID);
BindControls<AlbumQuery, Album>(albumUri, albumList, ?????);
string photoUri = PicasaQuery.CreatePicasaUri(PicasaUserID, PicasaAlbumID);
BindControls<PhotoQuery, Photo>(photoUri, slideShow, ?????);
the other methods are like:
private PicasaFeed CreateFeed<T>(string uri)
where T : KindQuery, new()
{
PicasaFeed feed = null;
try
{
PicasaService service = new PicasaService(PicasaApplicationName);
T query = new T();
query.BaseAddress = uri;
feed = service.Query(query);
}
catch (Exception ex)
{
//exception handling not shown
}
return feed;
}
private IEnumerable<T> GetPicasaEntries<T>(PicasaFeed feed)
where T : PicasaEntity, new()
{
if(feed == null){
return null;
}
IEnumerable<T> entries = null;
string cacheKey = feed.Id.ToString();
if(Cache.Get(cacheKey) == null)
{
entries = feed.Entries.Select(x => new T() { AtomEntry = x }).ToList();
Cache.Insert(cacheKey, entries,
null, Cache.NoAbsoluteExpiration, new TimeSpan(0,20,0));
}
return entries;
}
Anonymous types are really only designed for local use. In a strongly typed language types that can’t be strongly typed are not really encouraged for general use… They are just a part of c#s little dance in the dynamic world.
You have two choices.
Create a strong type.
And then use :
Or refer to it as Object. Then use reflection to get the data out – I wouldn’t recommend this.