本文介绍了FileHelpers.Dynamic.ClassBuilder.CreateRecordClass错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取运行时创建的具有布局的文件.但是,当运行FixedLengthClassBuilder.CreateRecordClass()时,会发生异常:

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?).

我的代码:

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.

异常源代码

[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;

}

异常StackTrace

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

您可以通过以下方式查看FieldConverter正确构建的异常的源代码:

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?

推荐答案

将您的FileHelpers从2.0更新到3.1.5并添加以下内容:

Update your FileHelpers from 2.0 to 3.1.5 and add this:

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

这篇关于FileHelpers.Dynamic.ClassBuilder.CreateRecordClass错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:04