Skip to content
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

ToDataTable throws on type with public static member #102

Closed
atifaziz opened this issue Aug 21, 2015 · 6 comments
Closed

ToDataTable throws on type with public static member #102

atifaziz opened this issue Aug 21, 2015 · 6 comments

Comments

@atifaziz
Copy link
Member

Originally reported on Google Code with ID 102

When trying to convert to a data table a lisdt of System.Drawing.Point I get the following
error "Static field requires null instance, non-static field requires non-null instance.Parameter
name: expression". 

code to reproduce:
System.Collections.Generic.List<System.Drawing.Point> l = new System.Collections.Generic.List<System.Drawing.Point>();
l.Add(new System.Drawing.Point(1,2));
MoreLinq.MoreEnumerable.ToDataTable(l);

Reported by AvniDani on 2015-05-18 15:40:31

@atifaziz
Copy link
Member Author

This is indeed a bug but meanwhile you might want use the following as a workaround:

MoreLinq.MoreEnumerable.ToDataTable(l, p => p.X, p => p.Y);

Even once fixed, I think the above workaround will be the actual usage you want since
Point has an instance property of IsEmpty (in addition to X and Y) that will get included
as a column in the resulting DataTable.

Reported by azizatif on 2015-05-18 16:04:12

  • Status changed: Accepted

@atifaziz
Copy link
Member Author

(fixing issue summary)

Reported by azizatif on 2015-05-18 16:41:40

@atifaziz
Copy link
Member Author

The exception is thrown by Expression.Field and is a breaking change with .NET 4.x.
On .NET 3.5, there is no exception thrown and DataTable is created with static members
also appearing as columns so perhaps this slipped unnoticed previously.

System.ArgumentException : Static field requires null instance, non-static field requires
non-null instance.
Parameter name: expression
   at System.Linq.Expressions.Expression.Field(Expression expression, FieldInfo field)
   at System.Linq.Expressions.Expression.MakeMemberAccess(Expression expression, MemberInfo member)
   at MoreLinq.MoreEnumerable.CreateMemberAccessor(Expression parameter, MemberInfo member)
in ToDataTable.cs: line 142

Reported by azizatif on 2015-05-18 17:35:29

  • Status changed: Started
  • Labels added: Priority-High, Milestone-Release1.1-SP1
  • Labels removed: Priority-Medium

@atifaziz
Copy link
Member Author

Few comments: 
1. If there is a chance for a bug that only I will fall on, my life story is that I
will fall on it :-)
2. Indeed I could use your suggestion of specifying the parameters to render to the
data table but in my case this was just an example. I have a much larger class that
I need to render to a data table and if I add fields to the class I now have to remember
to add them to the rendering list of fields. Obviously I would prefer to skip the need
if possible.
3. on a more technical level, I have some old code that does about the same (I prefer
to use yours). The code is working and did not fail were yours did. use it at your
own will
        Public Shared Function ToDataTable(Of T)(ByVal data As IList(Of T)) As DataTable
            Dim properties As ComponentModel.PropertyDescriptorCollection = ComponentModel.TypeDescriptor.GetProperties(GetType(T))
            Dim table As New DataTable()
            For Each prop As ComponentModel.PropertyDescriptor In properties
                table.Columns.Add(prop.Name, If(Nullable.GetUnderlyingType(prop.PropertyType),
prop.PropertyType))
            Next prop
            If data IsNot Nothing Then
                For Each item As T In data
                    If item IsNot Nothing Then
                        Dim row As DataRow = table.NewRow()
                        For Each prop As ComponentModel.PropertyDescriptor In properties
                            row(prop.Name) = If(prop.GetValue(item), DBNull.Value)
                        Next prop
                        table.Rows.Add(row)
                    End If
                Next item
            End If
            Return table
        End Function

Reported by AvniDani on 2015-05-18 19:19:26

@atifaziz
Copy link
Member Author

This issue was closed by revision 1f24f2a60a25

Reported by azizatif on 2015-05-19 04:52:41

  • Status changed: Fixed

@atifaziz
Copy link
Member Author

Published as v1.1.1 (aka SP1) as well as source packages:

* https://www.nuget.org/packages/morelinq/1.1.1
* https://www.nuget.org/packages/MoreLinq.Source.MoreEnumerable.ToDataTable/1.0.1
* https://www.nuget.org/packages/MoreLinq.Source.MoreEnumerable/1.2.1

Reported by azizatif on 2015-05-19 06:05:10

@atifaziz atifaziz added the bug label Jan 28, 2016
@atifaziz atifaziz added this to the 1.1 Service Pack 1 milestone Sep 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant