FileHelpers.Dynamic.ClassBuilder.CreateRecordClass Error

寵の児 提交于 2021-02-08 12:01:21

问题


I'm trying to reading a file with layout created at runtime. But when running FixedLengthClassBuilder.CreateRecordClass (), an exception occurs:

Error Compiling Expression:
Line 31: The type or namespace name 'SGCompras' could not be found (are you missing a using directive or an assembly reference?).

My code:

public void ReadFile(string filePath){
     ...
     var itemsLayout = Context.FixedLayouts.Where(p => p.LayoutName == "Margem").ToList();
     var builder = GetFixedClass(itemsLayout, "Margem");
     var layout = builder.CreateRecordClass();

     var engine = new FileHelperEngine(layout);
     var linhasArquivo = engine.ReadFile(filePath);
     ...
}

public FixedLengthClassBuilder GetFixedClass(List<FixedLengthLayout> items, string className){
            var cb = new FixedLengthClassBuilder(className);

            foreach (var item in items)
            {
                switch (item.FieldDataType)
                {
                    case "String":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(string));
                        cb.LastField.FieldNullValue = string.Empty;
                        cb.LastField.AlignMode = AlignMode.Left;
                        cb.LastField.AlignChar = ' ';
                        cb.LastField.TrimMode = TrimMode.Both;
                        break;
                    case "Date":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(DateTime));
                        cb.LastField.FieldNullValue = string.Empty;
                        break;
                    case "Custom Date":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(DateTime));
                        cb.LastField.FieldNullValue = string.Empty;
                        cb.LastField.Converter.TypeName = typeof(CustomDateConverter).FullName;
                        cb.LastField.AlignMode = AlignMode.Right;
                        cb.LastField.AlignChar = '0';
                        break;
                    case "Integer":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(int?));
                        cb.LastField.FieldNullValue = 0;
                        cb.LastField.AlignMode = AlignMode.Right;
                        cb.LastField.AlignChar = '0';
                        break;
                    case "Long Integer":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(long?));
                        cb.LastField.FieldNullValue = 0;
                        cb.LastField.AlignMode = AlignMode.Right;
                        cb.LastField.AlignChar = '0';
                        break;
                    case "Decimal":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(decimal?));
                        cb.LastField.FieldNullValue = 0;
                        cb.LastField.AlignMode = AlignMode.Right;
                        cb.LastField.AlignChar = '0';
                        break;
                    case "Custom Decimal":
                        cb.AddField(item.FieldName, item.FieldLength, typeof(decimal?));
                        cb.LastField.FieldNullValue = 0;
                        cb.LastField.Converter.TypeName = typeof(CustomDecimalConverter).FullName;
                        cb.LastField.AlignMode = AlignMode.Right;
                        cb.LastField.AlignChar = '0';
                        break;
                }
            }

            return cb;
        }

public class CustomDateConverter : ConverterBase
    {
        public override object StringToField(string stringValue)
        {
            if (string.IsNullOrEmpty(stringValue) || stringValue == "00000000")
            {
                return null;
            }

            return DateTime.ParseExact(stringValue, "ddMMyyyy", CultureInfo.InvariantCulture);
        }

        public override string FieldToString(object fieldValue)
        {
            return fieldValue == null ? "00000000" : ((DateTime)fieldValue).ToString("ddMMyyyy");
        }
    }

public class CustomDecimalConverter : ConverterBase
    {
        public override object StringToField(string from)
        {
            return Convert.ToDecimal(decimal.Parse(from) / 100);
        }

        public override string FieldToString(object fieldValue)
        {
            return ((decimal)fieldValue).ToString("0.00").Replace(".", "").Replace(",", "");
        }
    }

The error relates to my custom converter.

Exception SourceCode

[FixedLengthRecord(FixedMode.ExactLength)]
public sealed class Margem
{

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(12)]
public System.String MatriculaCliente;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(11)]
public System.String CPFCliente;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(50)]
public System.String NomeCliente;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(2)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> MesReferencia;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(4)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> AnoReferencia;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDecimalConverter))]
[FieldFixedLength(9)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Decimal> ValorMargemFolha;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDecimalConverter))]
[FieldFixedLength(9)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Decimal> ValorMargemCartao;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDecimalConverter))]
[FieldFixedLength(9)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Decimal> ValorDescontoObrigatorio;

[FieldNullValue("")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDateConverter))]
[FieldFixedLength(8)]
[FieldAlign(AlignMode.Right, '0')]
public System.DateTime DataNascimento;

[FieldNullValue("")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDateConverter))]
[FieldFixedLength(8)]
[FieldAlign(AlignMode.Right, '0')]
public System.DateTime DataAdmissao;

[FieldNullValue("")]
[FieldConverter(typeof(SGCompras.Nucleo.FileHelper.FileConverters.CustomDateConverter))]
[FieldFixedLength(8)]
[FieldAlign(AlignMode.Right, '0')]
public System.DateTime DataDemissao;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(4)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> RegimeTrabalho;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(3)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> CodigoBanco;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(10)]
public System.String NumeAgenciaBanco;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(1)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> NumeroDigitoAgencia;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(12)]
public System.String NumeroContaBanco;

[FieldNullValue(typeof(System.Int32), "0")]
[FieldFixedLength(1)]
[FieldAlign(AlignMode.Right, '0')]
public System.Nullable<System.Int32> NumeroDigitoConta;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(65)]
public System.String ReservadoEmpresa;

[FieldNullValue("")]
[FieldTrim(TrimMode.Both)]
[FieldFixedLength(10)]
public System.String Critica;

}

Exception StackTrace

at FileHelpers.Dynamic.ClassBuilder.ClassFromString(String classStr, String className, NetLanguage lang, List`1 additionalReferences)
   at FileHelpers.Dynamic.ClassBuilder.CreateRecordClass()
   at SGCompras.Servicos.ServicoArquivo.ReadFile(String filePath) in D:\Projetos\SGCompras\SGCompras.Servicos\ServicoArquivo.cs:line 356

You can view the source code of the exception that FieldConverter was built correctly, as:

[FieldConverter (typeof (SGCompras.Nucleo.FileHelper.FileConverters.CustomDateConverter))]

Someone would have any idea what might be happening?


回答1:


EDIT

Update your FileHelpers from 2.0 to 3.1.5 and add this:

classBuilder.AdditionalReferences.Add(typeof(MyCustomConverter.DecimalConverter).Assembly);


来源:https://stackoverflow.com/questions/35361374/filehelpers-dynamic-classbuilder-createrecordclass-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!