FileHelpers Support Forum Index
Some dot net books
FileHelpers Support
Support, Feedback, Suggestions and More...
Get forum post via any aggregator

Hi all, I'm a bit out of touch because my two works are keeping me really bussy.

My wife and me are waiting for a BABY =) so I'm using my free time to enjoy this unique and fantastic moment in life.

If you have any problem with the library you can try downloading the last Release Candidate version at:

Last Version: http://www.devoo.net/FileHelpers_2.2_RC1.rar

I dont have now time for a full release. Thanks for understanding my temporary absence.

Best Regards, Marcos

Retrieving inherited fields [PATCH]

 
Post new topic   Reply to topic    FileHelpers Support Forum Index -> Developers Discussions
View previous topic :: View next topic  
Author Message
bdemarzo



Joined: 31 Jul 2007
Posts: 7

PostPosted: Tue Jul 31, 2007 17:21    Post subject: Retrieving inherited fields [PATCH] Reply with quote

First off -- I just started working with FileHelpers and I'm very impressed!

In my situation, my FileHelper record classes serve dual-purpose: they can be imported via FileHelpers, and can be used as data persistence classes using an O/R mapper. This worked fine until I noticed that FileHelpers is attempting to access an internal field from the base class.

Here's an illustration:

Code:

//base class
public class EntityBase {
    internal bool _isReadOnly;
}

//my class
[DelimitedRecord(",")]
public class Person : EntityBase
{
   public Int32 PersonID;
   [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
   public String LastName;
}


When I run the engine to import this, I get the following error:

Quote:
Field: _isReadOnly. Error Converting '110001' to type: 'Boolean'. The string: 110001 cant be recognized as boolean using default true/false values.


Apparently, FileHelpers is looking at the internal field from the base class. A little digging, and it appears that RecordInfo.cs uses the RecursiveGetFields() method to start from the base class to get all properties. This is fine in simple situations (inheriting from System.Object) but can cause lots of grief inheriting from base classes that include fields and properties you don't want.

To address this, I wrote a new [IgnoreInheritedClass] attribute, and updated the RecursiveGetFields() method to avoid climbing the class inheritance tree if this attribute exists.

The following is the Subversion DIFF file, if you have an interest in seeing what I did and potentially incorporating it into the trunk.

Code:
Index: FileHelpers/Attributes/IgnoreInheritedClassAttribute.cs
===================================================================
--- FileHelpers/Attributes/IgnoreInheritedClassAttribute.cs   (revision 0)
+++ FileHelpers/Attributes/IgnoreInheritedClassAttribute.cs   (revision 0)
@@ -0,0 +1,24 @@
+#region "  © Copyright 2005-07 to Marcos Meli - http://www.devoo.net"
+
+// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.
+
+#endregion
+
+using System;
+
+namespace FileHelpers
+{
+   /// <summary>Indicates  that fields inherited from base classes must be ignored.</summary>
+   /// <remarks>See the <a href="attributes.html">Complete Attributes List</a> for more clear info and examples of each one.</remarks>
+   /// <seealso href="attributes.html">Attributes List</seealso>
+   /// <seealso href="quick_start.html">Quick Start Guide</seealso>
+   /// <seealso href="examples.html">Examples of Use</seealso>
+   [AttributeUsage(AttributeTargets.Class)]
+   public sealed class IgnoreInheritedClassAttribute : Attribute
+   {
+      /// <summary>Indicates that fields inherited from base classes must be ignored.</summary>
+      public IgnoreInheritedClassAttribute()
+      {
+      }
+   }
+}
\ No newline at end of file
Index: FileHelpers/Core/RecordInfo.cs
===================================================================
--- FileHelpers/Core/RecordInfo.cs   (revision 590)
+++ FileHelpers/Core/RecordInfo.cs   (working copy)
@@ -51,6 +51,7 @@
         private Regex mConditionRegEx = null;
 #endif
         internal int mFieldCount;
+      internal bool mRecursiveClasses;
 
         private ConstructorInfo mRecordConstructor;
 
@@ -136,8 +137,8 @@
 #endif
             }
 
+         mRecursiveClasses = !mRecordType.IsDefined(typeof(IgnoreInheritedClassAttribute), false);
 
-
 #if ! MINI
             if (typeof(INotifyRead).IsAssignableFrom(mRecordType))
                 mNotifyRead = true;
@@ -173,7 +174,7 @@
 
         private void RecursiveGetFields(ArrayList fields, Type currentType, TypedRecordAttribute recordAttribute)
         {
-            if (currentType.BaseType != null)
+         if (currentType.BaseType != null && mRecursiveClasses)
                 RecursiveGetFields(fields, currentType.BaseType, recordAttribute);
 
             fields.AddRange(currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
Back to top
View user's profile Send private message
Marcos
Site Admin


Joined: 07 Mar 2006
Posts: 400
Location: Bahía Blanca - Argentina

PostPosted: Tue Jul 31, 2007 17:52    Post subject: Reply with quote

Wow =)

Thanks for the GREAT FEEDBACK AND PATCH Very Happy

I will be reviewing it today and aplying it for the next 2.2 release the next days.

The solutions is also perfect to cut the tree climbing at the desired point

Thanks again an welcome to the project.

Cheers
_________________
Happy Coding Wink
Marcos ( www.devoo.net )
If you like the library support us with some books or monetary donations
Vote FileHelpers in Codeproject or Bookmark us in Delicious or here, Thanks !
Back to top
View user's profile Send private message
bdemarzo



Joined: 31 Jul 2007
Posts: 7

PostPosted: Tue Jul 31, 2007 19:00    Post subject: Reply with quote

Glad you approve. I'm glad I found this library -- it's coming in very handy in a project I'm working on.
Back to top
View user's profile Send private message
Marcos
Site Admin


Joined: 07 Mar 2006
Posts: 400
Location: Bahía Blanca - Argentina

PostPosted: Tue Jul 31, 2007 19:49    Post subject: Reply with quote

Changes Commited

http://fisheye3.cenqua.com/changelog/filehelpers/?cs=598

I change a bit the code to allow stop the inherited class search, ie, with the changes you can do

class A

[IgnoreInheritedClass]
class B:A

class C:B

so the resulting record class will contain the fields of class B, later the fields of C and nothing more.

Thanks again Very Happy
Marcos
_________________
Happy Coding Wink
Marcos ( www.devoo.net )
If you like the library support us with some books or monetary donations
Vote FileHelpers in Codeproject or Bookmark us in Delicious or here, Thanks !
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    FileHelpers Support Forum Index -> Developers Discussions All times are GMT
Page 1 of 1

 
  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Send your feedback

Source Forge Thanks Jetbrains for support us !!