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

  • Home
  • SEARCH
  • 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 9188777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:05:38+00:00 2026-06-17T20:05:38+00:00

I managed to create this class at run time with Reflection Emit: [DelimitedRecord(,)] public

  • 0

I managed to create this class at run time with Reflection Emit:

[DelimitedRecord(",")]
    public partial class Person
    {
        [FieldOrder(0)]
        private string firstName;

        [FieldOrder(1)]
        private string lastName;

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

like this:

            //create the builder
            AssemblyName assembly = new AssemblyName("FileHelpersTests");
            AppDomain appDomain = System.Threading.Thread.GetDomain();
            AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(assembly, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assembly.Name);

            //create the class
            TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                                                TypeAttributes.BeforeFieldInit, typeof(System.Object));

            //create the Delimiter attribute
            Type[] delimiterAttributeParams = new Type[] { typeof(string) };
            ConstructorInfo delimiterAttrInfo = typeof(DelimitedRecordAttribute).GetConstructor(delimiterAttributeParams);
            CustomAttributeBuilder delimiterAttributeBuilder = new CustomAttributeBuilder(delimiterAttrInfo, new object[] { ";" });
            typeBuilder.SetCustomAttribute(delimiterAttributeBuilder);

            //create the firstName field
            FieldBuilder firstNameField = typeBuilder.DefineField("firstName", typeof(System.String), FieldAttributes.Private);

            //create the firstName attribute [FieldOrder(0)]
            Type[] firstNameFieldOrderAttributeParams = new Type[] { typeof(int) };
            ConstructorInfo firstNameFieldOrderAttrInfo = typeof(FieldOrderAttribute).GetConstructor(firstNameFieldOrderAttributeParams);
            CustomAttributeBuilder firstNameFieldOrderAttributeBuilder = new CustomAttributeBuilder(firstNameFieldOrderAttrInfo, new object[] { 0 });
            firstNameField.SetCustomAttribute(firstNameFieldOrderAttributeBuilder);

            //create the FirstName property
            PropertyBuilder firstNameProperty = typeBuilder.DefineProperty("FirstName", PropertyAttributes.HasDefault, typeof(System.String), null);

            //create the FirstName Getter
            MethodBuilder firstNamePropertyGetter = typeBuilder.DefineMethod("get_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
                                                                              MethodAttributes.HideBySig, typeof(System.String), Type.EmptyTypes);
            ILGenerator firstNamePropertyGetterIL = firstNamePropertyGetter.GetILGenerator();
            firstNamePropertyGetterIL.Emit(OpCodes.Ldarg_0);
            firstNamePropertyGetterIL.Emit(OpCodes.Ldfld, firstNameField);
            firstNamePropertyGetterIL.Emit(OpCodes.Ret);

            //create the FirstName Setter
            MethodBuilder firstNamePropertySetter = typeBuilder.DefineMethod("set_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
                                                                MethodAttributes.HideBySig, null, new Type[] { typeof(System.String) });
            ILGenerator firstNamePropertySetterIL = firstNamePropertySetter.GetILGenerator();
            firstNamePropertySetterIL.Emit(OpCodes.Ldarg_0);
            firstNamePropertySetterIL.Emit(OpCodes.Ldarg_1);
            firstNamePropertySetterIL.Emit(OpCodes.Stfld, firstNameField);
            firstNamePropertySetterIL.Emit(OpCodes.Ret);

            //assign getter and setter
            firstNameProperty.SetGetMethod(firstNamePropertyGetter);
            firstNameProperty.SetSetMethod(firstNamePropertySetter);


            //create the lastName field
            FieldBuilder lastNameField = typeBuilder.DefineField("lastName", typeof(System.String), FieldAttributes.Private);

            //create the lastName attribute [FieldOrder(1)]
            Type[] lastNameFieldOrderAttributeParams = new Type[] { typeof(int) };
            ConstructorInfo lastNameFieldOrderAttrInfo = typeof(FieldOrderAttribute).GetConstructor(lastNameFieldOrderAttributeParams);
            CustomAttributeBuilder lastNameFieldOrderAttributeBuilder = new CustomAttributeBuilder(lastNameFieldOrderAttrInfo, new object[] { 1 });
            lastNameField.SetCustomAttribute(lastNameFieldOrderAttributeBuilder);

            //create the LastName property
            PropertyBuilder lastNameProperty = typeBuilder.DefineProperty("LastName", PropertyAttributes.HasDefault, typeof(System.String), null);

            //create the LastName Getter
            MethodBuilder lastNamePropertyGetter = typeBuilder.DefineMethod("get_LastName", MethodAttributes.Public | MethodAttributes.SpecialName |
                                                                              MethodAttributes.HideBySig, typeof(System.String), Type.EmptyTypes);
            ILGenerator lastNamePropertyGetterIL = lastNamePropertyGetter.GetILGenerator();
            lastNamePropertyGetterIL.Emit(OpCodes.Ldarg_0);
            lastNamePropertyGetterIL.Emit(OpCodes.Ldfld, lastNameField);
            lastNamePropertyGetterIL.Emit(OpCodes.Ret);

            //create the FirstName Setter
            MethodBuilder lastNamePropertySetter = typeBuilder.DefineMethod("set_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
                                                                MethodAttributes.HideBySig, null, new Type[] { typeof(System.String) });
            ILGenerator lastNamePropertySetterIL = lastNamePropertySetter.GetILGenerator();
            lastNamePropertySetterIL.Emit(OpCodes.Ldarg_0);
            lastNamePropertySetterIL.Emit(OpCodes.Ldarg_1);
            lastNamePropertySetterIL.Emit(OpCodes.Stfld, lastNameField);
            lastNamePropertySetterIL.Emit(OpCodes.Ret);

            //assign getter and setter
            lastNameProperty.SetGetMethod(lastNamePropertyGetter);
            lastNameProperty.SetSetMethod(lastNamePropertySetter);

What i would really like is to declare my class like this:

 public partial class Person
    {
        private string firstName;

        private string lastName;

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

and at run time only add those attributes.
For the first name field i am thinking at something like this

    //get the builder to that field
    FieldBuilder firstNameField = 

    //create the attribute [FieldOrder(0)]
    Type[] firstNameFieldOrderAttributeParams = new Type[] { typeof(int) };
    ConstructorInfo firstNameFieldOrderAttrInfo = typeof(FieldOrderAttribute).GetConstructor(firstNameFieldOrderAttributeParams);
    CustomAttributeBuilder firstNameFieldOrderAttributeBuilder = new CustomAttributeBuilder(firstNameFieldOrderAttrInfo, new object[] { 0 });
    firstNameField.SetCustomAttribute(firstNameFieldOrderAttributeBuilder);

How to get the builder to that field?

  • 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-06-17T20:05:40+00:00Added an answer on June 17, 2026 at 8:05 pm

    As rpgmaker already said in a comment, you can’t modify a class after it’s loaded. There are some ways around that:

    1. Use Mono Cecil to modify existing assembly.
    2. Use something like PostSharp as a post-build step to add the attributes to the assembly.
    3. Create a new class at runtime, that is basically a copy of your class with the attributes added.
    4. Use something like Roslyn to modify your source code before you actually compile it.

    But all of them are just workarounds. And your end goal is not actually to add attributes to a file, it seems it’s to read a CSV file using FileHelpers without having to specify the necessary attributes.

    There is a page on the FileHelpers site describing how to do that, using several different approaches, including loading the format from XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need this roadmap of a Hibernate managed object instance. First, I create an
I'm having a hard time getting this working in C++, I've managed it in
Is there a way to create a class's constants dynamically? I know this sounds
I've created a simple buffer manager class to be used with asyncroneous sockets. This
I followed this way. To create a workspace to manage your source-controlled files 1.
I managed to create a dynamic three dropdown list using Javascript code and it
I managed to create a .deb file from a source program,tar.gz how can i
I have managed to create a custom action in C# using MakeSfxCA which is
I already managed to create function which checks if user is using a trial/demo
I have managed to create a simple app which deletes (bypassing the recycle bin)

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.