简体   繁体   中英

“The type does not exist in the namespace” when specifing custom FieldConverter

I'm trying to use my custom converter with FileHelpers v. 3.1.5 After trying:

classBuilder.LastField.Converter.TypeName = typeof(NoValueConverter).ToString();

I get "The type or namespace name 'DN' could not be found". When I try t specify the namespace of my custom converter:

classBuilder.Namespace = "DN.CustodianDataImport.Parsers";
classBuilder.LastField.Converter.TypeName = typeof(NoValueConverter).ToString();

it throws: The type or namespace name 'NoValueConverter' does not exist in the namespace 'DN.CustodianDataImport.Parsers' (are you missing an assembly reference?)

When I specify ConverterType only, code works fine:

classBuilder.LastField.Converter.Kind = ConverterKind.Double;

but what I need to do is to tell parser to treat "N/A" as null, rather than throw Error Converting 'N/A' to type: 'Double'

Any ideas?

Try using typeof(NoValueConverter).FullName instead of ToString() which may only be giving the class name without the full namespace prefix.

EDIT:

I have now confirmed that ToString() is definitely not using a valid name:

public override ToString()
{
    return ("Type: " + name);
}

It works fine for me the way you're doing it (without even specifying classBuilder.Namespace ).

Here's a working example:

using System;
using FileHelpers;
using FileHelpers.Dynamic;
using System.Data;
using NUnit.Framework;

namespace OtherNameSpace
{
    public class MyConverter : ConverterBase
    {
        public override object StringToField(string from)
        {
            return from;
        }
    }

}

namespace ConsoleApplication1
{
    class Program
    {
        private static void Main(string[] args)
        {
            var cb = new DelimitedClassBuilder("Customer", ",");

            cb.AddField("SomeField", typeof(String));
            cb.LastField.Converter.TypeName = typeof(OtherNameSpace.MyConverter).ToString();

            Type recordClass = cb.CreateRecordClass();

            var engine = new FileHelperEngine(recordClass);
            var records = engine.ReadStringAsDT("aaa");

            Assert.AreEqual("aaa", records.Rows[0].Field<string>(0));

            Console.WriteLine("All OK");
            Console.ReadKey();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM