开发者

adhoc struct/class in C#?

Currently i am using reflection with sql. I find if i want to make a spe开发者_Python百科cialize 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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜