Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:59:06+00:00 2026-05-30T12:59:06+00:00

Can I build a class as shown below dynamically using reflection? There are no

  • 0

Can I build a class as shown below dynamically using reflection? There are no methods, just public variables, some have custom attributes.

Is the .Emit method required (from what I’ve seen, “Emit” looks a little challenging).

I’m using software from http://www.FileHelpers.net, and it requires a class. All my file definitions are in a database table, and I’d like to make everything more dynamic (i.e. no code changes when a new column appears in the file).

[FileHelpers.DelimitedRecord(",")]
public class FileRow
{
    [FileHelpers.FieldQuoted('"', QuoteMode.OptionalForBoth)] 
    public string Borrower_First_Name;
    [FileHelpers.FieldQuoted('"', QuoteMode.OptionalForBoth)] 
    public string Borrower_Last_Name;
    public string Borrower_Email;
}

Update 1: Based on Vlad’s answer below I needed to reference DLL, here’s how I did it:

    // need to reference the FileHelpers.dll from our own .exe directory 
    string diskFilenameFileHelpersDLL = 
        System.IO.Path.GetDirectoryName(
           System.Reflection.Assembly.GetExecutingAssembly().Location) + 
           @"\FileHelpers.dll";

Update 2: Also, after doing what Vlad suggested, this is how I call FileHelper and loop through the results. I’ll probably transfer the data to a list.

    Assembly assembly = compiledResult.CompiledAssembly;

    // Simple Data Test 
    lineContents = "John,Doe,jd123456@yahoo.com";
    FileHelperEngine engine = new FileHelperEngine(assembly.GetType("FileRow"));
    // FileRow[] FileRowArray = (FileRow[])engine.ReadString(lineContents);
    Object[] FileRowArray = engine.ReadString(lineContents);
    Object myObject = FileRowArray[0];  // only 1 row of data in this example 

    // Get the type handle of a specified class.
    Type myType = assembly.GetType("FileRow");
    // Get the fields of the specified class.
    FieldInfo[] myField = myType.GetFields();

    Console.WriteLine("\nDisplaying fields values:\n");
    for (int i = 0; i < myField.Length; i++)
    {
        Object objTest = myField.GetValue(i);

        string tempName = myField[i].Name;
        Object objTempValue = myField[i].GetValue(myObject);
        string tempValue = System.Convert.ToString(objTempValue);

        Console.WriteLine("The value of {0} is: {1}",
                            tempName, tempValue);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:59:07+00:00Added an answer on May 30, 2026 at 12:59 pm

    If you have your code stored in the database as string what you can do something like this to create an assembly:

    The reason I commented out attributes because I don’t have namespace for them. I am assuming you have namespace and you will need to add it to your code to compile.

    Code works in LINQPad, so you can just copy and paste.

    using System;
    using System.Reflection;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    
    void Main()
    {
        StringBuilder dc = new StringBuilder(512);
        dc.Append("public class FileRow");
        dc.Append("{");
        //dc.Append("[FileHelpers.FieldQuoted('\"', QuoteMode.OptionalForBoth)]");
        dc.Append("public string Borrower_First_Name;");
        //dc.Append("[FileHelpers.FieldQuoted('\"', QuoteMode.OptionalForBoth)]");
        dc.Append("public string Borrower_Last_Name;");
        dc.Append("public string Borrower_Email;");
        dc.Append("}");
    
        CompilerResults compiledResult = CompileScript(dc.ToString());
    
        if (compiledResult.Errors.HasErrors)
        {
            Console.WriteLine (compiledResult.Errors[0].ErrorText);
            throw new InvalidOperationException("Invalid Expression syntax");
        }
    
        Assembly assembly = compiledResult.CompiledAssembly;
    
        // This is just for testing purposes.
        FieldInfo field = assembly.GetType("FileRow").GetField("Borrower_First_Name");          
        Console.WriteLine (field.Name);         
        Console.WriteLine (field.FieldType);
    }
    
    public static CompilerResults CompileScript(string source) 
    { 
        CompilerParameters parms = new CompilerParameters(); 
    
        parms.GenerateExecutable = false; 
        parms.GenerateInMemory = true; 
        parms.IncludeDebugInformation = false; 
    
        CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp"); 
    
        return compiler.CompileAssemblyFromSource(parms, source); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a class as shown below: @Path(/myrequest) @Scope(request) @Component public class MyRESTCode implements IServicedResource<T>
Is there a built-in .NET class that can replace or work like WinHttp.WinHttpRequest? Thanks!
Is there anyway I can build a Select statement that uses the Contains function?
I have an object that can build itself from an XML string, and write
Is there a way I can build a bitmap strictly from RGB values for
I just found the IUI project on google code where you can build websites
I'm using Hudson and Maven 2 for my automated build/CI. I can build fine
Are there any good code profilers/analyzers for Erlang? I need something that can build
I'm using wx.aui to build my user interface. I'm defining a class that inherits
I have a mydatagridview class which inherits from the built-in DataGridView control, as shown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.