473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Confused about generics ...

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
2 1041
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.