Thursday, June 10, 2010

Cast anonymous types

Today, when coding for one module in my project at company, I had a problem that make me crazy. Used var keyword on .NET 3.5, it's a anonymous type, so when we go out its scope, we don't know its type. I think many people also had this problem like me. So after using many methods for solve this problem, I also found the best solution for it. Thanks Alex for best idea from him! Now there are some my effort for solve it:
+ First thing, I tried to coding and gave this error:
                 Cannot cast 'exampleObj' (which has an actual type of '<>f__AnonymousType0<string,string>') ...
+ Next, I started my working for solving it. Wrote the extension method for cast anonymous method:
+ Object that content the anonymous type:
Binding this object to ListView:
+ And used the extension method for cast anonymous type:
Now we can use it very normally:
+ References:

No comments:

Post a Comment