views:

4334

answers:

9

We all know that, when we create anonymous class

var Employee = new { ID = 5, Name= "Prashant" };

at run time it will be of type

<>f__AnonymousType0<int,string>

is there any way to specify some meaningfully name to these classes ??

+4  A: 

Make it a regular class with a name?

public class Employee
{
    public int ID;
    public string Name;
}


var Employee = new Employee { ID = 5, Name= "Prashant" };
Dead account
+19  A: 

public class Employee {}

Chad Grant
this actually made me lol :-)
Eoin Campbell
Will that even work? Lol.
Pwninstein
No he was merely saying "create a class"
Nick Berardi
+11  A: 

It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only.

Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler what name to use for an anonymous type.

In fact, the names of the types generated by the compiler use illegal characters in their naming so that you cannot have a name collision in your application.

Chris
+1 for stating the cold hard facts! :P
Cerebrus
+6  A: 
public class Employee {
    public int ID { get; set; }
    public string Name { get; set; }
}

Then use the following syntax

var employee = new Employee { ID = 5, Name = "Prashant" };
Nick Berardi
I don't get why this answer is rated higher than mine?
Dead account
@Ian: its may be 'coz Nick answered before you
Prashant
@Prashant, I was first to answer (select "oldest" tab). Don't matter anyway, that's just the way SO works sometimes.
Dead account
@Ian you can have the 15pts if it matters that much to you. These were basically posted at the same time, and maybe people liked the fact that I used properties instead of the often bad idea of using public fields.
Nick Berardi
A: 

I think that, by definition, Anonymous Types can't have a name, just that which the compiler gives it. If you're looking for more meaningful design-time information on Anonymous Types then you're expecting too much from the compiler.

Pwninstein
+1  A: 

Yes, you are creating an Anonymous Class , if you want your class to have a name, i.e. Not Anonymous, then declare a regular class or struct.

jcopenha
A: 

Since it's anonymous, you cannot name it. If you need to know the name of a type, then you must really create a class.

Luis Abreu
+1  A: 

No, there is no way to give a meaningful type name to these classes as you've declared them. Anonymous Types are just that, anonymous. There is no way to explicitly "name" the type in code without resorting to very ugly hacks.

If you really need to give the type a name you will need to explicitly declare and use a new type with the properties you need.

JaredPar
+9  A: 

Actually, if you're not afraid of getting extremely nitty gritty, you could use TypeBuilder to build your own runtime type based on your anonymous type, which will allow you to specify a name for the type. Of course, it is much easier to just declare a class as almost everyone else in this thread suggested, but the TypeBuilder way is far more exciting. ;)

TypeBuilder

Banang
+1 Never heard of that before. Good answer :)
Dead account
Unfortunatly, though, it's a very silly solution, but I thought I'd mention it for giggles... ;)
Banang