By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Confused about generics ...

P: n/a
Hello,

I have the following method:

public static IEnumerable GetRoles(CultureInfo culture, bool open)
{
switch (culture.TwoLetterISOLanguageName.ToLower()) {
case "en":
return new[] {
new { Name = Role.Administrator, Description =
"Administrador", open = false },
new { Name = Role.Collaborator, Description =
"Colaborador", open = true }
}.OrderBy(r =r.Description);
}
return null;
}

Basically, what I would like to do, but not knowing if possible is:
- Create the items in a generic way (Not creating a class for this):
new { Name = Role.Administrator, Description = "Administrador", open
= false }

- Being able to use Linq on the result:
GetRoles(new CultureInfo("en-GB" , true)).Where(r =r.open = false)

I think I will need always to use List<RoleClassright?

Thanks,
Miguel
Oct 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 06 Oct 2008 10:06:22 -0700, shapper <md*****@gmail.comwrote:
[...]
Basically, what I would like to do, but not knowing if possible is:
- Create the items in a generic way (Not creating a class for this):
new { Name = Role.Administrator, Description = "Administrador", open
= false }
I don't understand why you have a parameter "open" that's never used.
That said, you can only get part of what you're looking for using syntax
similar to what you've posted (I've used "..." rather than writing out all
the code):

return new object[] { new { Name = .... }, ... };

Using some contortions, you could get a "var"-declared collection instead
on which you could successfully call OrderBy() with the lambda expression
you posted. But it starts to get really messy at that point, and even
once you've done that, the anonymous type won't be visible outside your
GetRoles() method.
- Being able to use Linq on the result:
GetRoles(new CultureInfo("en-GB" , true)).Where(r =r.open = false)
As long as you return an IEnumerable, you can use LINQ. The problem is
that the compiler won't have information about the type by the time the
collection instance gets back from the GetRoles() method. You'd have to
use reflection to inspect the type, get the "open" property, and then use
the associated PropertyInfo to get the value inside your LINQ expression.
Reflection is slow enough as it is, but you'd have to do it for each
element of the collection, which could really bog things down.

It's not clear at all from your question _why_ you want to do this
though. Even if this could have worked cleanly, it would have required
that the type be consistent throughout, which seems to me to be a great
justification for just going ahead and declaring the relevant type. Why
do you have such an aversion to declaring the type?

Pete
Oct 6 '08 #2

P: n/a
It will be very hard for a downstream consumer to know what your data
looks like, and hard to test. I would create a Role class that
represents a role. However, you don't need to switch to List<Role-
IEnumerable<Rolewould do.

Btw - this has got nothing to do with generics; just anonymous types.

Marc
Oct 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.