Shipment import from CSV or Excel  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

Re: Shipment import from CSV or Excel

Postby SBarnes » Wed Jun 18, 2025 8:48 am

The code below demonstrates what you are trying to do in a console app and caters for the Jiwa conding conventions of how that backing fields are declared either as underscore or m underscore

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ReadOnlyPropertiesClass readOnlyPropertiesClass = new ReadOnlyPropertiesClass();
            ReflectionHelper.SetReadOnlyPropertyValue(readOnlyPropertiesClass, "PropertyWithUnderscore", "PropertyWithUnderscoreValue");
            ReflectionHelper.SetReadOnlyPropertyValue(readOnlyPropertiesClass, "PropertyWithMUnderscore", "PropertyWithMUnderscoreValue");
            Console.WriteLine(readOnlyPropertiesClass.PropertyWithUnderscore);
            Console.WriteLine(readOnlyPropertiesClass.PropertyWithMUnderscore);
        }
    }
   



    public class ReadOnlyPropertiesClass
    {
        private string _PropertyWithUnderscore = "";
        private string m_PropertyWithMUnderscore = "";

        public   string PropertyWithUnderscore { get { return _PropertyWithUnderscore; }  }
        public readonly string PropertyWithMUnderscore { get { return m_PropertyWithMUnderscore; } }



    }


    public static class ReflectionHelper
    {
        public static bool SetReadOnlyPropertyValue(object target, string propertyName, object newValue)
        {
            if (target == null)
                throw new ArgumentNullException(nameof(target));
            if (propertyName == null)
                throw new ArgumentNullException(nameof(propertyName));

            Type type = target.GetType();
            PropertyInfo prop = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            if (prop == null)
                throw new ArgumentException($"Property '{propertyName}' not found on type '{type.FullName}'.");

            // Only look for _PropertyName and m_PropertyName
            string[] backingFieldNames = new[]
            {
        $"_{propertyName}",
        $"m_{propertyName}"
    };

            FieldInfo backingField = null;

            foreach (var fieldName in backingFieldNames)
            {
                backingField = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (backingField != null)
                    break;
            }

            if (backingField == null)
            {
                throw new Exception ($"Backing field for property '{propertyName}' not found.");
                return false;
            }

            backingField.SetValue(target, newValue);
            return true;
        }
    }

}

Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: Shipment import from CSV or Excel

Postby DannyC » Wed Jun 18, 2025 10:22 am

The code below demonstrates what you are trying to do in a console app and caters for the Jiwa conding conventions of how that backing fields are declared either as underscore or m underscore

Appreciate the assistance but nothing in that sample code says why 'field' in my code is returning null. For now, that's my current hurdle.
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: Shipment import from CSV or Excel

Postby Mike.Sheen » Wed Jun 18, 2025 10:51 am

DannyC wrote:
The code below demonstrates what you are trying to do in a console app and caters for the Jiwa conding conventions of how that backing fields are declared either as underscore or m underscore

Appreciate the assistance but nothing in that sample code says why 'field' in my code is returning null. For now, that's my current hurdle.


When I come across things like this, where I know I'm looking for something but are not sure of the name, I would spit out into a debug window a list of all the fields or properties.

Do that, and you'll see in the list that is output something that looks promising. If you don't, then adjust the binding flags until you do.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Shipment import from CSV or Excel  Topic is solved

Postby Mike.Sheen » Wed Jun 18, 2025 11:01 am

Here's an example I found we've used before in a customisation - a helper method to set a private property:

Code: Select all
public static void SetPrivateFieldProperty<T>;(object obj, string propertyName, T NewValue)
{
    var field = obj.GetType().GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic);
    if (field != null)
    {
      field.SetValue(obj, NewValue);
    }
   else
   {
       var property = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic);
       if (property != null)
       {
         property.SetValue(obj, NewValue);
       }
      else
      {
         throw new Exception(string.Format("Could not find a private property called '{0}' in class '{1}'.", propertyName, obj.GetType().ToString()));
      }
   }
}


and an example of use:

Code: Select all
Helpers.SetPrivateFieldProperty<DateTime>(DebtorAllocationsForm.DebtorAllocations.Debtor, "_LastSavedDateTime", (DateTime)newLastSavedDateTime);


It's setting the _LastSavedDateTime private backing field which is a DateTime, of an object "DebtorAllocationsForm.DebtorAllocations.Debtor".
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Shipment import from CSV or Excel

Postby SBarnes » Wed Jun 18, 2025 11:18 am

The null is because there is no field called "<QuantityOrdered>_QuantityOrdered" try "_QuantityOrdered" but in reality, I would just change things to use one of the methods Mike and I have given you as they work from the property name.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: Shipment import from CSV or Excel

Postby DannyC » Wed Jun 18, 2025 2:40 pm

Code: Select all
Here's an example I found we've used before in a customisation - a helper method to set a private property:


Ahhhh! I see.
So I have managed to populate QuantityOrdered. I will try for the other read only properties but for now that helper from Mike has shown me the way forward.
There was a semicolon that I had to remove to get it to compile but otherwise all good.

Really appreciate that code! Thanks.
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Previous

Return to Technical and or Programming

Who is online

Users browsing this forum: Bing [Bot] and 2 guests