Skip to content

TransactionHelper reads private field names instead of public property names. #99

@Kuffs2205

Description

@Kuffs2205

https://github.com/Backendless/.NET-SDK/blob/49d8bb46e7add11aac5ce24edd0c3ef8b9d6aaf2/Backendless/Transaction/TransactionHelper.cs

 internal static Dictionary<String, Object> ConvertInstanceToMap<E>( E instance )
    {
      if( instance == null )
        throw new ArgumentException( ExceptionMessage.NULL_INSTANCE );

      Dictionary<String, Object> entity = new Dictionary<String, Object>();
      Type fieldsType = typeof( E );
      FieldInfo[] fields = fieldsType.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance );

      foreach( FieldInfo field in fields )
        entity[ field.Name ] = field.GetValue( instance );

      return entity;
    }
	
    internal static String GetObjectIdFromInstance<E>( E instance )
    {
      if( instance == null )
        throw new ArgumentException( ExceptionMessage.NULL_INSTANCE );

      Type fieldsType = typeof( E );
      FieldInfo[] fields = fieldsType.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance );

      foreach( FieldInfo field in fields ) 
        if( field.Name == "objectId" || field.Name == "ObjectId" )
          return (String) field.GetValue( instance );

      throw new ArgumentException( ExceptionMessage.NULL_OBJECT_ID_IN_INSTANCE );
    }

These two methods read the field names from a Business Object rather than the property names. Because of that, transactions fail as it is looking for "objectId" etc when the field name is "_objectId" but the property name is "objectId"

Private fields tend to be internal to the class. Is it really the intention that it work this way?

I fixed it in my own project by forking the source and replacing FieldInfo with PropertyInfo for these two methods but I would rather stick with official releases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions