3

I have a class called initialize that runs at the beginning of my program. Originally I explicitly hard coded all the classes that it was supposed to instantiate but I would like to make it more generic and remove the hard coded classes so I can mark the class closed for modification.

My first thought was to create a queue of Types that my initialize class would cycle through and instantiate all the Types that are in the queue.

I then wanted to decide on a per class basis if it should be added to the queue or not. By adding itself to the queue from within the class. The problem is that I cant add a class to the queue unless it is already been instantiated. I know that variables can be initialized before running but obviously not methods. So Im stuck on figuring out weather what I would like to do is possible on not.

Something along the Lines of:

MyClass
{

 initalize.instance.Enqueue(typeof(MyClass));
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
MichaelTaylor3D
  • 1,615
  • 3
  • 18
  • 32
  • 4
    It's not really clear what your initialization is. It would *really* help if you'd give us a sample. Are you perhaps looking for an IoC container? – Jon Skeet Sep 07 '12 at 18:42
  • 1
    Is this a very large and evolving program? Make sure you consider [YAGNI](http://en.wikipedia.org/wiki/You_ain%27t_gonna_need_it). – ChaosPandion Sep 07 '12 at 18:43
  • Yes its a pretty large program, that will constantly evolve and change. – MichaelTaylor3D Sep 07 '12 at 18:44
  • Also - can you not decorate classes that need to be "initialized" using a custom attribute and have the "initializer" find and "initialize" those classes? – Ani Sep 07 '12 at 18:45
  • @ananthonline thats an interesting Idea, and im looking it up now but Im having trouble finding examples. – MichaelTaylor3D Sep 07 '12 at 19:06

3 Answers3

3

I meant something along these lines.

    public static class Initializer
    {
        public static void Initialize()
        {
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                foreach (var type in assembly.GetTypes())
                    if (type.IsDefined(typeof(InitializeAttribute), true))
                        Console.WriteLine("Need to initialize {0}", type.FullName);
        }
    }

    [AttributeUsage(AttributeTargets.Class)]
    public sealed class InitializeAttribute : Attribute
    { 
    }

    [Initialize]
    public sealed class ToBeInitialized
    { 
    }
Ani
  • 10,826
  • 3
  • 27
  • 46
  • This was awesome, Exactly what I was looking for. – MichaelTaylor3D Sep 07 '12 at 19:16
  • I have this working perfectly but now Im intrigue by its usage and im trying to understand it better, where is [Initialize] being defined as the name of the attribute? Why wouldn't [InitializeAttribute] be used? – MichaelTaylor3D Sep 07 '12 at 19:50
  • 1
    In C# - when decorating a supported element with a type that derives from System.Attribute and is called XXXAttribute, the "Attribute" portion is optional and can be left out. – Ani Sep 07 '12 at 19:52
2

The pattern that you're looking for, if I understand your question correctly, is the factory method pattern. Take a look at this link: http://en.wikipedia.org/wiki/Factory_method_pattern#C.23

alvonellos
  • 1,009
  • 1
  • 9
  • 27
  • I dont think thats it because from what I understand of the factory method it still needs inputs to tell it what to create. Its getting in the inputs during initialization Im having trouble with. Id like to go in any arbitrary class and set a flag or whatever to tell it to initialize. – MichaelTaylor3D Sep 07 '12 at 19:11
  • You can use the factory pattern with parameters... Another thing you could do is do as David suggested and use reflection ( http://stackoverflow.com/questions/578495/dynamically-create-an-object-of-type ) to create instances of those classes and store them in the queue. Just out of curiosity, why are you doing this, exactly? It's kind of odd to have a non-deterministic initialization sequence going on... Another thing, calling arbitrary constructors with arbitrary types and arbitrary parameters (which is what I'm thinking that you want to do) is a very dangerous thing to do. – alvonellos Sep 07 '12 at 19:23
  • I want to mark the status my initalizer class as Closed for modification but that can never happen if I have to constantly go into the class to add new classes that needs to be initialized. ananthonline 's method allows me to close that class and never revisit it. Thats pretty much what I wanted to achieve. – MichaelTaylor3D Sep 07 '12 at 19:31
1

If you're initializing class static state then this isn't necessary. Just add a static constructor to all of your types and the .NET runtime will lazily call the static constructor before any static members of that class are accessed (be it a field, method, property or event). This can also work for the singleton pattern - however as I said, static constructors are JIT-evaluated which can introduce non-deterministic delays in your program.

The alternative is to use reflection to iterate through each Type in the assembly and perform your own initialization. This is how plugin systems work.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • It would seem that you can not have a static constructor to tell a class to create an instance of itself to run automatically. Or maybe there is something I dont understand? Inside of MyClass I put the following constructor with no results. static Myclass() { new MyClass; } – MichaelTaylor3D Sep 07 '12 at 18:57