0

I a writing a C# app that reads data from a text file that is divided into record types of fixed content. Sort of a text version of the old ISAM format. I then want to write out the data to an Access database that has a different table structure than the input file.

I am reading each record type of the input file into a separate class. I have the layout of each input record within the class definition that is then fed to a generic parser that loads the data into the class. All this works just fine.

The issue I have is cleanly creating the SQL insert statements to write to the Access database. The Access database has a different table structure than the input File. I would like to create a mapping of the columns within an Access table to the classes that contain the input data. e. g. output-table-A.column_C gets its data from input-record-1.column_1. I could then write a generic function to gather up the data for the insert SQL statement to load output-table-A.

The problem is that the reference to the input data objects would be a string. How do I get from a string to the actual instance of the object to retrieve the data?

This flexibility would be useful as some of the users will want to specify the mappings from input to output themselves within a file that would be read at run time by the program. I know this sounds strange, but I am dealing with a database schema that is allowed to be altered by users. Don't ask me why anyone would write a system that allowed such, but they did.

2

1 Answer 1

1

You want to create an object from a string with the name of the class?

class MyClass
{
    public string Name;
    public int Age;
}

Type MyType = Type.GetType("MyClass");

var MyObject = Activator.CreateInstance(MyType);    

I don't know how much that will help you. It sounds like you are still going to have to do some if/elsing to deal with the class.

Sign up to request clarification or add additional context in comments.

1 Comment

The objects already exist. I just need to know which one to use. I have seen the activator mentioned in other posts. Perhaps there is a way to use this to create the classes I need on the fly. I do wonder if I need to rethink the problem from the inside out and do something counter-intuitive. I am trying to avoid some very convoluted if/else logic, but there may not be a way around it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.