RandallFlagg / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

ToDataTable throws on type with public static member #102

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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);

Original issue reported on code.google.com by AvniD...@gmail.com on 18 May 2015 at 3:40

GoogleCodeExporter commented 9 years ago
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.

Original comment by azizatif on 18 May 2015 at 4:04

GoogleCodeExporter commented 9 years ago
(fixing issue summary)

Original comment by azizatif on 18 May 2015 at 4:41

GoogleCodeExporter commented 9 years ago
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

Original comment by azizatif on 18 May 2015 at 5:35

GoogleCodeExporter commented 9 years ago
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

Original comment by AvniD...@gmail.com on 18 May 2015 at 7:19

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 1f24f2a60a25

Original comment by azizatif on 19 May 2015 at 4:52

GoogleCodeExporter commented 9 years ago
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

Original comment by azizatif on 19 May 2015 at 6:05