FileHelpers
                                
                                 FileHelpers copied to clipboard
                                
                                    FileHelpers copied to clipboard
                            
                            
                            
                        Detecting Auto-Implemented Properties in VB
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.
- Visual Studio 2015 Update 1
- Projects targeted for .NET 4.5.2
- Installed FileHelpers from NuGet, Version 3.1.5 (8/7/2015)
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
@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.
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?
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
See also #331:
- [ ] Include small VB.Net project (as F# project)
- [ ] add test
- [ ] include code to detect VB.Net AutoProperty