MarcosMeli / FileHelpers

The FileHelpers are a free and easy to use .NET library to read/write data from fixed length or delimited records in files, strings or streams
https://www.filehelpers.net
MIT License
1.13k stars 349 forks source link

Detecting Auto-Implemented Properties in VB #201

Open ghost opened 8 years ago

ghost commented 8 years ago

There seems to be a discrepancy in how the library detects auto-implemented properties in VB vs C#

I created two brand-new MVC projects for testing.

C#

[IgnoreFirst]
[DelimitedRecord(",")]
public class WrkShop
{
    public string Title { get; set; }
    //plus other properties
}

//Instantiating WORKS
FileHelperEngine<WrkShop> engWrkShop = new FileHelperEngine<WrkShop>();

VB

<IgnoreFirst>
<DelimitedRecord(",")>
Public Class WrkShop
    Public Property Title As String
    'plus other properties
End Class

'Instantiating THROWS AN EXCEPTION
Dim EngWrkShop As FileHelperEngine(Of WrkShop) = New FileHelperEngine(Of WrkShop)

Stack Trace

[ArgumentNullException: Value cannot be null.
Parameter name: name]
   System.Type.GetProperty(String name) +11434556
   FileHelpers.FieldBase.CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute) +1549
   FileHelpers.RecordInfo.CreateCoreFields(IList`1 fields, TypedRecordAttribute recordAttribute) +93
AnotherKiwi commented 7 years ago

@ghost - do you know if this issue has been fixed in later releases?

I'm about to start using FileHelpers in a project I am developing using VB. If FileHelpers still has this issue I'll have to try and learn enough C# to create a separate project for the file I/O that is required.

sanguaire commented 7 years ago

I have the same issue. The IL-Code of the VB compiler is different.

IL of AutoProperty in VB IL_0002: stfld int32 FileHelperVb.TestRecord::_Id

IL of AutoProperty in C# IL_0002: stfld int32 FileHelpersC.TestRecord::'<Id>k__BackingField'

In your code you only check for the C# name of the backing field.

if (fi.Name.EndsWith("__BackingField") &&
                    fi.Name.StartsWith("<") &&
                    fi.Name.Contains(">"))

                    res.FieldFriendlyName = fi.Name.Substring(1, fi.Name.IndexOf(">") - 1);

                res.IsAutoProperty = true;

                var prop = fi.DeclaringType.GetProperty(res.FieldFriendlyName);
                if (prop != null)
                {
                    Attributes.WorkWithFirst<FieldOrderAttribute>(prop, x => res.FieldOrder = x.Order);
                }

This results in a res.FieldFriendlyName of null.

Can you fix this in a new build?

mcavigelli commented 7 years ago

Thank you for analyzing and fixing this problem and for your patience.

It would speed up the inclusion of the fix if you could add a NUnit test revealing this problem.

Thank you, Matthias

mcavigelli commented 5 years ago

See also #331: