Design Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • walentys
    New Member
    • Jul 2008
    • 9

    Design Question

    Okay so I've got a delegate called "InstructionRun " (just a simple void with no parameters), and a class called "Instructio n" which contains a delegate InstructionRun object as a member variable, as well as a method called "Run" which just calls the InstructionRun object's current functionality. There is also a byte in this class called "code".

    Another class ("Instructions" ) has a static array of Instruction objects. It also has a static method whose purpose is to initialize all the Instruction objects.

    And then somewhere else are a bunch of static void() methods that will be stored into the "InstructionRun " object for each Instruction object in the array.

    My predicament is that I would like something like a simple text file or database to provide me with the data for this Instruction array. I want it to have a bunch of pairs, my "code" byte and then my static void() method. However, I would not want to resort to any of the following:

    1) Not using a data file and just putting hundreds of "new Instruction()" lines to fill the array. I hate mass blocks like that when the design could change and then I have to change hundreds of those lines. I want something like a simple text file to just match up "code"s with functionality.
    2) Putting the method's actual name in the data file, and then using reflection to read it during runtime and match it up with a method. Just seems sluggish and icky.

    Please help out! If I wasn't clear on something, I can explain more.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm guessing the end goal is to change the order of start-up events by changing the config.txt file. But ...
    [not] Putting the method's actual name in the data file
    yet
    Not using a data file and just putting hundreds of "new Instruction()" lines to fill the array. I hate mass blocks like that when the design could change and then I have to change hundreds of those lines.
    Implies that you do have hundreds of methods, that all match the same delegate.

    So, what... You're trying to create a text like


    27, add
    19, remove
    89, load

    Something like that?

    That will eventually become a sequence that effectively does

    Add(LoadedByte) ; // LoadedByte being 27
    Remove(LoadedBy te); // LoadedByte being 19
    Load(LoadedByte ); // 89 LoadedByte being 89

    Do I interpret the goal correctly?

    Comment

    • walentys
      New Member
      • Jul 2008
      • 9

      #3
      You are pretty much correct. I will have hundreds of methods, which is why I chose a delegate system as I don't want this:

      Code:
      switch (code)
      {
         case 0x28:
          RunADD();
          break;
         case 0x2A:
          RunJMP();
          break;
         //so on...
      }
      However, they are parameterless methods...they don't take a byte (actually I haven't decided that yet, they MIGHT have parameters down the line, but that's not important right now, right now I just need some way to match up the bytes with methods), but rather the byte determines which method to call.

      So yes, my text would be something like:

      28 RunADD
      2A RunJMP
      ...
      etc.

      and then there would just be:

      static void RunADD()
      static void RunJMP()
      ...
      etc.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Got it. Looks like you are making an assembly language simulator/translator.

        Why not just make the methods match the bytes then? If you have a byte of 2A then run static void 2A(), which is the JMP instruction? Why do the lookup table at all?

        Comment

        • walentys
          New Member
          • Jul 2008
          • 9

          #5
          That would be fine, I don't care what the methods are named....but even then, how would I go about matching up that byte coming in to that function named "2A"? Do I have to resort to reflection?

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Do I have to resort to reflection?
            You say that like it's a bad thing. What are you trying to avoid about reflection, or what is it that makes your project hard to deal with reflection?

            Comment

            • walentys
              New Member
              • Jul 2008
              • 9

              #7
              Nothing in particular, wouldn't it just slow it down? It seems a little excessive...get ting a method name from a file, passing the string to look up a symbol name of the very program you're running in currently...whe n all you really need to do (though it might be uglier) is just call the damn method in the program. I was merely trying to look for a non-ugly way, maybe, avoiding reflection.

              Thanks for being patient with me.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Doesn't seem that ugly to me

                Code:
                using System;
                using System.Reflection;
                 
                class CallMethodByName
                {
                   string name;
                    
                   CallMethodByName (string name)
                   {
                      this.name = name;
                   }
                    
                   public void DisplayName()      // method to call by name
                   {
                      Console.WriteLine (name);   // prove we called it
                   }
                    
                   static void Main()
                   {
                      // Instantiate this class
                      CallMethodByName cmbn = new CallMethodByName ("CSO");
                 
                      // Get the desired method by name: DisplayName
                      MethodInfo methodInfo = 
                         typeof (CallMethodByName).GetMethod ("DisplayName");
                 
                      // Use the instance to call the method without arguments
                      methodInfo.Invoke (cmbn, null);
                   }
                }

                Comment

                • walentys
                  New Member
                  • Jul 2008
                  • 9

                  #9
                  Okay, thanks for your help.

                  I just have seen that most people, including even in this community, frown upon retrieving a variable via a string (its name). I figured the same would apply here. But I guess in some cases it is acceptable for such behavior, especially when it would simplify the code so greatly.

                  Comment

                  Working...