views:

99

answers:

1

I'm trying to do something rather... unique, and maybe there's a far better way to do it but... I'm doing an inversion of control(ish) system that uses extension methods to enable/disable components of the class, so before I get into more detail and confuse you, lets look at some code!

using TestComponents.CommunicationProtocols.RS232; 
                                             //this brings in the 
                                             //ConnectRS232 extension method

namespace TestMeNamespace
{
    public class Test  //Although this class is defined here, we extend it above
    {
        public void Start()
        {
           this.ConnectRS232(1, 9600); //calls the ConnectRS232 extension method
        }
    }
}

So in short, the using declaration extends Test in the same file that we DEFINE test. (inheritance would be fine as well) However there are some problems with this! first of all, the ugly requisite "this". blech. secondly it's a messy co-dependant system.

Here's what I'm attempting to achieve:

  • I want a way to easily extend static methods to a class (using declarations are fine)
  • I want to make statements simple: ConnectRS232();
  • I want to not have to futz with partial classes if I don't have to.
  • I'd be fine with using interface inheritance.

Feel free to ask me additional questions via comments but please don't post an answer unless you have an ANSWER!

Edit: In lieu of questions raised, I'm doing some JIT compilation of C#script (www.cs-script.com) in my system, and also these scripts will be mostly written by non-programmers who have been using a really "special" proprietary language for scripting for years. I want to keep things simple as hell, and a whole bunch of "this" calls look like clutter.

+1  A: 

I'm not sure I see the point in this...

Your "extensions" will be compile time only. The extension methods only work as static methods, and since you're building this on importing of namespaces, it's more of a compile time construct than any form of IoC. (Extension methods are just a compile time thing - they don't really do anything at runtime.)

Also, given the above statement, having this.Method() doesn't seem onerous (it's good practice to use normally, which is why tools such as StyleCop enforce that you do that on EVERY method call).

Can you give us a better example of how this would be used? Right now, it just seems like a way to put code in two places instead of one, with no real benefit...

Reed Copsey
Ah, but if he compiles without CommunicationProtocols.RS232, he'll have a smaller compiled program.
Brian
True Brian :-) Also as for the 'this' statements, i think it's VERY misleading to use them in methods that don't have to worry about threading or inheritance.
Firoso
Edit updated, please read the end.
Firoso
@firoso: I'd strongly disagree with the "this" statement. I'd read the comments on http://blogs.msdn.com/sourceanalysis/archive/2008/05/25/a-difference-of-style.aspx - they talk about the two main options used out there - either always using this., or prefixing with _ (which has other disadvantages)
Reed Copsey