Exemplo n.º 1
0
        /// <summary>
        /// Initialize the structure for a Field
        /// </summary>
        /// <param name="reflectedInfo">Information about the property/field gleaned from reflection</param>
        public PropertyFieldMappingInfo(Core.Reflection.FieldInfo reflectedInfo)
        {
            Field      = reflectedInfo;
            IsProperty = false;

            Attribute   = reflectedInfo.GetAttributes <FileFieldAttribute>().First();
            ColumnIndex = Attribute.Index;
        }
        /// <summary>
        /// Gets the record data as a string array
        /// </summary>
        /// <typeparam name="T">Type of business object</typeparam>
        /// <param name="instance">Instance of business object to turn into a record-array</param>
        /// <param name="headers">Headers for the object (NULL to auto-detect -- bad idea when fetching for multiple items)</param>
        /// <param name="objectInfo">Reflected object metadata (NULL to auto-detect -- bad idea when fetching for multiple items)</param>
        /// <returns>String array containing records</returns>
        public string[] GetRecord <T>(T instance, string[] headers = null, ClassInfo objectInfo = null)
            where T : class, new()
        {
            objectInfo ??= TypeInspector.InspectOnlyMembersAnotated <T, FileFieldAttribute>();
            headers ??= GetHeader <T>(objectInfo);

            string[] record = new string[objectInfo.Properties.Count + objectInfo.Fields.Count];
            foreach (PropertyInfo property in objectInfo.Properties)
            {
                object             objValue    = property.Read(instance);
                FileFieldAttribute ffAttribute = property.GetAttributes <FileFieldAttribute>().First();

                if (objValue == null)
                {
                    objValue = ffAttribute.NullValue;
                }
                else
                {
                    if (property.Type.IsEnum && (ffAttribute.UseEnumIntegerValue))
                    {
                        // by default, reflection would have given us the name (in objValue)
                        objValue = (int)objValue;
                    }
                    else if (property.Type == typeof(bool))
                    {
                        objValue = ((bool)objValue ? (ffAttribute.BooleanTrues?[0] ?? "true") : (ffAttribute.BooleanFalses?[0] ?? "false"));
                    }
                    else if (property.Type == typeof(bool?))
                    {
                        bool?b = (bool?)objValue;
                        if (b.HasValue)
                        {
                            objValue = (b.Value ? (ffAttribute.BooleanTrues?[0] ?? "true") : (ffAttribute.BooleanFalses?[0] ?? "false"));
                        }
                    }
                    else if (property.IsEnumerable)
                    {
                        string serialized = JsonSerializer.Serialize(
                            objValue, property.Type,
                            new JsonSerializerOptions()
                        {
                            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                        }
                            );

                        if ((serialized.Length > 0) && (serialized[0] == '['))
                        {
                            serialized = serialized[1..];