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. Generating properties of a class dynamically

Generating properties of a class dynamically

Scheduled Pinned Locked Moved C#
help
6 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.
  • 0 Offline
    0 Offline
    0bx
    wrote on last edited by
    #1

    Over the project I created several classes and enum's; I hope it's bit self-explanatory:

    public enum weaponType{unarmed, dagger, sword, club, axe};
    public enum monsterType{ rat, bat, humanoid, feline, beast };

    class weapon : item
    {
    private weaponType type;
    public weaponType Type
    {
    get { return type; }
    set { type = value; }
    }

    private int requiredSkill;
    public int RequiredSkill
    {
    get { return requiredSkill; }
    set { requiredSkill = value; }
    }
    ...
    }

    class actor
    {
    private name;
    public Name{ get{return name; } set { name = value;} }
    ...

    private int daggerSkill;
    public int DaggerSkill { get{return name; } set { name = value;} }
    private int swordSkill;
    ...
    

    }

    class monster : actor
    {
    private monsterType;
    ...
    }

    class player : actor
    {
    private ratKills...
    }

    "Actors" need to have all the "weaponType-Skills" additionally, the player needs to have all the "monsterType-Kills" as separate properties. However, I would like to freely add and remove items from those enum's without much overhead; because the overhead would increase exponentially if the project becomes more complex. So what I rather like to do is the compiler to iterate through the enum and generate all the extra properties like "Skill" and "Kills" dynamically and to be able to use them as if they're fully written out. So far I haven't been able to figure out how, or if, this can be done programmatically; without resorting to make some code generator. As usual, any help is much appreciated.

    .

    R 1 Reply Last reply
    0
    • 0 0bx

      Over the project I created several classes and enum's; I hope it's bit self-explanatory:

      public enum weaponType{unarmed, dagger, sword, club, axe};
      public enum monsterType{ rat, bat, humanoid, feline, beast };

      class weapon : item
      {
      private weaponType type;
      public weaponType Type
      {
      get { return type; }
      set { type = value; }
      }

      private int requiredSkill;
      public int RequiredSkill
      {
      get { return requiredSkill; }
      set { requiredSkill = value; }
      }
      ...
      }

      class actor
      {
      private name;
      public Name{ get{return name; } set { name = value;} }
      ...

      private int daggerSkill;
      public int DaggerSkill { get{return name; } set { name = value;} }
      private int swordSkill;
      ...
      

      }

      class monster : actor
      {
      private monsterType;
      ...
      }

      class player : actor
      {
      private ratKills...
      }

      "Actors" need to have all the "weaponType-Skills" additionally, the player needs to have all the "monsterType-Kills" as separate properties. However, I would like to freely add and remove items from those enum's without much overhead; because the overhead would increase exponentially if the project becomes more complex. So what I rather like to do is the compiler to iterate through the enum and generate all the extra properties like "Skill" and "Kills" dynamically and to be able to use them as if they're fully written out. So far I haven't been able to figure out how, or if, this can be done programmatically; without resorting to make some code generator. As usual, any help is much appreciated.

      .

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      So why not use a List<string> for the skills. For example:

      public class actor
      {
      private List skills = new List();

      public bool HasSkill(string skill)
      {
          return skills.Contains(skill);
      }
      
      public void AddSkill(string skill)
      {
          skills.Add(skill);
      }
      
      public void RemoveSkill(string skill)
      {
          skills.Remove(skill);
      }
      

      }

      Etc... That way you don't have to modify the system to add skills, it just works. Is there a way to generate classes and properties dynamically? Yes, but its really not easy, requires use of low level MSIL and some pretty good knowledge of CodeDOM, really more of a headache than its worth.

      0 1 Reply Last reply
      0
      • R Ron Beyer

        So why not use a List<string> for the skills. For example:

        public class actor
        {
        private List skills = new List();

        public bool HasSkill(string skill)
        {
            return skills.Contains(skill);
        }
        
        public void AddSkill(string skill)
        {
            skills.Add(skill);
        }
        
        public void RemoveSkill(string skill)
        {
            skills.Remove(skill);
        }
        

        }

        Etc... That way you don't have to modify the system to add skills, it just works. Is there a way to generate classes and properties dynamically? Yes, but its really not easy, requires use of low level MSIL and some pretty good knowledge of CodeDOM, really more of a headache than its worth.

        0 Offline
        0 Offline
        0bx
        wrote on last edited by
        #3

        Thanks for your reply. I can't use that because Skills will be integers. But you gave me the idea to use an array instead. It would look something like this: int[skillType.Length - 1]. It's indeed good enough and still pretty easy to make. I've done some assembler in the past, but I don't know MSIL. I was hoping there's some function in the .NET library that could do it; but I couldn't find it.

        .

        R F 2 Replies Last reply
        0
        • 0 0bx

          Thanks for your reply. I can't use that because Skills will be integers. But you gave me the idea to use an array instead. It would look something like this: int[skillType.Length - 1]. It's indeed good enough and still pretty easy to make. I've done some assembler in the past, but I don't know MSIL. I was hoping there's some function in the .NET library that could do it; but I couldn't find it.

          .

          R Offline
          R Offline
          Ron Beyer
          wrote on last edited by
          #4

          Its because it's not there, it would be nice to be able to add properties to a type at runtime but you can't. You can generate them dynamically, but you have to generate the entire class (although you could use derived classes for base functionality). There are other things you can do dynamically, such as anonymous types, but I don't think they would fit the bill since you have to know all the properties at instantiation. Instead of using an array of integers, you can also use an array of your skill enum. That way you don't have to type cast later and you can extend just by adding elements to your enum. Another way would be to add the Flags attribute to your enum, then you can combine flags, like:

          enum Skills { skill1, skill2, skill3, skill4 }

          //Then in your class:
          private Skills mySkills = Skills.skill1 | Skills.skill2;

          //Which now holds both skills

          You can then use the Skills.HasFlag to determine if any of the flags (skills) are set.

          1 Reply Last reply
          0
          • 0 0bx

            Thanks for your reply. I can't use that because Skills will be integers. But you gave me the idea to use an array instead. It would look something like this: int[skillType.Length - 1]. It's indeed good enough and still pretty easy to make. I've done some assembler in the past, but I don't know MSIL. I was hoping there's some function in the .NET library that could do it; but I couldn't find it.

            .

            F Offline
            F Offline
            Freak30
            wrote on last edited by
            #5

            I think a Dictionary would be more appropriate. This way you can save the skills of your player object to disc and reload them later, even after you have added more possible skills. You could of course also use an enum as key instead of the string as suggested.

            0 1 Reply Last reply
            0
            • F Freak30

              I think a Dictionary would be more appropriate. This way you can save the skills of your player object to disc and reload them later, even after you have added more possible skills. You could of course also use an enum as key instead of the string as suggested.

              0 Offline
              0 Offline
              0bx
              wrote on last edited by
              #6

              Been working on this for two days now, so it's a bit too troublesome to go back and review the core after I got it all working. I'm indeed using enum's as a key to translate raw csv into useful game data. The downside is that making some changes like removing or switching around items may indeed break existing game-files, but it's not a big issue since everything else scales just fine. Not expecting this will become a problem though. I'll put a version tag on everthing just in case.

              .

              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