Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can I add new fields to an existing type using reflection( FieldBuilder) ?

Can I add new fields to an existing type using reflection( FieldBuilder) ?

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charith Jayasundara
    wrote on last edited by
    #1

    Hi, If can, Could you please tell me how? Thanks!

    Charith Jayasundara

    C 1 Reply Last reply
    0
    • C Charith Jayasundara

      Hi, If can, Could you please tell me how? Thanks!

      Charith Jayasundara

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You can't. Reflection tells you what is already there, it doesn't allow you to change the shape of the type.

      Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You can't. Reflection tells you what is already there, it doesn't allow you to change the shape of the type.

        Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

        L Offline
        L Offline
        Lev Danielyan
        wrote on last edited by
        #3

        You can however do that using Mono.Cecil opensource library, the trick is that cecil doesn't load the assembly into AppDomain, so you can make changes. Here is a code snippet for example: FieldDefinition field = new FieldDefinition("TestField", sometype, FieldAttributes.Private); sometype.Fields.Add(field); This adds a private field of type sometype to sometype. Unfortunately there is no documentation for cecil, but you can ask questions in the Mono Cecil Google Group[^]

        Regards, Lev

        C 1 Reply Last reply
        0
        • L Lev Danielyan

          You can however do that using Mono.Cecil opensource library, the trick is that cecil doesn't load the assembly into AppDomain, so you can make changes. Here is a code snippet for example: FieldDefinition field = new FieldDefinition("TestField", sometype, FieldAttributes.Private); sometype.Fields.Add(field); This adds a private field of type sometype to sometype. Unfortunately there is no documentation for cecil, but you can ask questions in the Mono Cecil Google Group[^]

          Regards, Lev

          C Offline
          C Offline
          Charith Jayasundara
          wrote on last edited by
          #4

          "the trick is that cecil doesn't load the assembly into AppDomain" How this will affect the other stuff? Thanks!

          Charith Jayasundara

          L 1 Reply Last reply
          0
          • C Charith Jayasundara

            "the trick is that cecil doesn't load the assembly into AppDomain" How this will affect the other stuff? Thanks!

            Charith Jayasundara

            L Offline
            L Offline
            Lev Danielyan
            wrote on last edited by
            #5

            What do you mean "other stuff"? You can edit the assembly using cecil, save it, and then do whatever you want to do with reflection (unless you get charmed by cecil and want to switch to it ;))

            Regards, Lev

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups