-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write dynamic objects #187
Comments
I will need to look into this. I thought it wasn't working because I will take a look at this tonight. |
The issue is that when writing it looks at the type of the generic parameter of the IEnumerable that's passed in and uses that to auto map. Since the type is Using I will put out at least the fix for dynamic tomorrow, and I will update you on the dictionary issue. |
Thanks for the update Josh. We've built a generic reporting system on top of Dapper which is returning dynamic objects. Specifically Dapper returns Would it be possible to register a CSV Map for these dynamic objects or does the mapping API require compile time properties? |
I need to handle the |
Any status or tips for implementing the |
No, it doesn't support more than a single property per field for writing. The change will require it to detect that it's an IDictionary when mapping and use key/value pairs to create the maps instead of the properties. |
Have you looked at the possibility of using the GetDynamicMemberNames method from the DynamicObject class? Could this be used to build the map? I'm mainly concerned with writing an object that is dynamically built at runtime. |
I haven't, but good thinking. I'll keep that in mind when I do this. Thanks. |
Also, looks like I would have to use |
Update stackoverflow question when this functionality gets implemented. http://stackoverflow.com/questions/26072099/how-do-you-use-csvhelper-to-write-a-class-derived-from-dynamicobject/26150939#26150939 |
I've managed to write lists of expandos, like so:
Just in case this helps anyone until this functionality is incorporated |
Its been a few years :) Any update on this? |
No. :( I'm making this project more of a priority now though so I can get through some of these major features that have been around for a while. Unfortunately, stuff like this tends to take a lot of hours to do. |
This is turning out to be a really difficult thing to do. The problem is, you can do anything based on the type, because it doesn't mean anything. You need to have the object itself. This doesn't work with reflection and there aren't any I found a way to pull the data here. http://stackoverflow.com/a/14011692/68499 This still has the issues I stated above. I'm working through it, but it'll probably take a few days at least. Hopefully I can come up with something that is somewhat clean. |
I found a way that isn't horrible to look at. This feature will be in version 3.0.0-beta2 on NuGet. |
Hi @JoshClose , is there any documentation for writing the csv based on a list of dynamic objects? I haven't seen anything in the documentation. Regards, |
Not yet, but it should work like any other type of object. Are you having some issues with it? |
@JoshClose , thanks for this, I was also looking at writing ExpandoObjects to Csv's and having installed version 3.0.0 beta 7 it all worked well!! I tried to install the latest which was beta 8 but got a Nuget error saying it wasn't compatible with .Net version 4.5.2. I would have suggested using the Dynamitey package which is brilliant for getting a list of dynamic properties and their values from a dynamic object but as you've sorted it, I don't really mind how it works! :) Great library, thanks for this. |
And @mayconbeserra, using version 3.0.0 beta 7, I just called the code as normal i.e.
|
So, can I ask what your use case for .NET 4.5.2 is? Why are you using that over 4.6? I switched to VS2017 and removed all project types except netstandard1.3. Here is a chart of what that supports. https://github.com/dotnet/standard/blob/master/docs/versions.md If it's a common case to need < 4.6 for new development, I'll add back in 4.0 support or something. My thought is new development would be >= 4.6 or core. Legacy development could use CsvHelper 2.x. |
Fortunately I am in a position to upgrade to 4.6 without any problem's so I'll do that. No reason, just because that was the version when the project was created, |
One reason supporting older .NET might be helpful is I believe that Mono currently maps to .NET 4.5 for support. So it might be helpful in being compatible with Mono which helps to be able to use the code across OSX and Linux. I could be missing some Mono/.NET 4.6 compatibility nuance so apologies in advance! |
Xamarin can use .NET Core. https://blog.xamarin.com/net-standard-library-support-for-xamarin/ Are you talking about something else? |
Mono is a bit different than Xamarin. The default Mono project targets .NET 4.5. I just tested a Mono project with CsvHelper and you can properly add and use CsvHelper 2.16.3. But when you try to add the latest beta it gives an error because the package does not target .NET 4.5 etc... There might be some way to leverage the net standard beta version of CsvHelper within a Mono project but I am not sure. I will let you know if I figure out a way but just wanted to mention it. |
Based on this https://github.com/dotnet/standard/blob/master/docs/versions.md you should be able to use Mono 4.6 with netstandard1.3, which is what CsvHelper 3.0 is using. Are there any issues using 4.6 over 4.5? |
I'm struggling with this a little. It's easy enough to serialize a sequence of ExpandoObjects to CSV. However, I'm concerned about the safety of the code. Is there any guarantee of the ordering of the properties in the ExpandoObject? Given that it implements IDictionary it doesn't seem like there is an ordering guarantee of the properties. If the implementation of ExpandoObject (or it's internal dictionary) changes in the future, how do I ensure that my properties/columns are enumerated in the same order? It seems like that makes this a risk (albeit probably a low one...)
|
If you need to have it in a specific order and don't want to count on just the header names, you'll need to use an actual class and specify a column index; either by using a mapping file, or attributes. |
Hi, I am using the 2.16.3 |
Use it how? Does this pertain to dynamic objects? |
Looking for ways to map the dynamic object property to 'CSV header name' of their own. seems related and impossible to do at the moment. This is a promising option at the moment: https://askcodes.net/questions/dynamic-creation-of-columns-using-csvhelper |
@mishal153 Do you want to use |
Is it possible to write an
IEnumerable<dynamic>
. The following code throws an exception "No properties are mapped for System.Object":The text was updated successfully, but these errors were encountered: