Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kuffs2205 opened this issue Jan 17, 2023 · 0 comments

Comments

@Kuffs2205
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant