views:

70

answers:

3

Currently i am using reflection with sql. I find if i want to make a specialize query it is easiest to get the results by creating a new class inheriting from another and adding the 2 members/columns for my specialized query. Then due to reflections in the lib in my c# code i can write foreach(var v in list) { v.AnyMember and v.MyExtraMember)

Now instead of having the class scattered around or modifying my main DB.cs file can i define a class inside a function? I know i can create an anonymous object by writing new {name=val, name2=...}; but i need a to pass this class in a generic function func(query, args);

+1  A: 

Classes (types in general) can not be defined inside methods. Doh.

pst
+1  A: 

It's possible (although not simple), but then the resulting class would not be known at compile time, so you wouldn't be able to use v.MyExtraMember. You would basically get a dynamically created anonymous object, so you would have to use reflection to access the extra members.

Guffa
+2  A: 

Have a look at the DynamicObject maybe it could serve your needs if you hide a Dictionary behind your implementation of TryGetMember/TrySetMember. There is a small example on this, follow the link.

bassfriend
Hey this is pretty cool. Maybe i can get the code modified to allow an object type and pass this in.
acidzombie24