Question

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);

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top