Skip to content

Namespace could not be found #430

@LucaNitti

Description

@LucaNitti

I tried to create a generalized function to create a csv file from every class omitting the object field (only string, number, boolean date etc etc)

when i run it i get the following error

Line 157: The type or namespace name 'it' could not be found (are you missing a using directive or an assembly reference?)

and in the Stacktrace is

in FileHelpers.Dynamic.ClassBuilder.ClassFromString(String classStr, String className, NetLanguage lang, List`1 additionalReferences)

in FileHelpers.Dynamic.ClassBuilder.CreateRecordClass()

The namespace of my application is something like
it.mycompany.client.project.main.lib.Model.ModelEdmx for autogenerated class

it.mycompany.client.project.main.web.Management.AsyncTask for the class where the function is.

The class has using it.mycompany.client.project.main.lib.Model.ModelEdmx

Method:

private static string CreateFileGeneric<T>(IEnumerable<T> records, string name, PerformContext context)
        {
            context.Info($"Create file for {name}");
            var basePath = System.AppDomain.CurrentDomain.BaseDirectory + AppConfig.TASK_ARCHIVE_LOCAL_FOLDER;
            var HeaderLine = String.Join(";", typeof(T).GetFields().Select(f => $"\"{f.Name}\"").ToList());
            string filename = name.Replace(" ", "_").Replace(".", "_") + "_" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".csv";
            string path = Path.Combine(basePath, filename);
            var cb = new DelimitedClassBuilder(typeof(T).Name, ";") { IgnoreEmptyLines = true };
            foreach (var prop in typeof(T).GetProperties())
            {
                if (!(prop.PropertyType.IsClass && prop.PropertyType != typeof(string)))
                {
                    var fieldBuilder = cb.AddField(prop.Name, prop.PropertyType);
                    if (fieldBuilder.FieldType == "System.String")
                    {
                        cb.LastField.TrimMode = TrimMode.Both;
                        cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
                        cb.LastField.QuoteChar = '"';
                    }
                    if (fieldBuilder.FieldType == "System.DateTime")
                    {
                        cb.LastField.Converter.Arg1 = "dd/MM/yyyy HH:mm:ss:fff";
                    }
                }
            }
            var recordClass = cb.CreateRecordClass();
            var fileHelper = new FileHelperEngine(recordClass);
            fileHelper.HeaderText = HeaderLine;
            fileHelper.WriteFile(path, (IEnumerable<object>)records);
            return filename;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions