Click here to Skip to main content
16,005,290 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 6:13
sitebuilderLuc Pattyn15-Apr-10 6:13 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:41
mvePIEBALDconsult15-Apr-10 7:41 
GeneralRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 7:47
sitebuilderLuc Pattyn15-Apr-10 7:47 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:56
mvePIEBALDconsult15-Apr-10 7:56 
AnswerRe: How to force static instances of a class to initialize? Pin
thugthug15-Apr-10 8:36
thugthug15-Apr-10 8:36 
GeneralRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 9:06
sitebuilderLuc Pattyn15-Apr-10 9:06 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 9:40
mvePIEBALDconsult15-Apr-10 9:40 
GeneralRe: How to force static instances of a class to initialize? Pin
thugthug15-Apr-10 10:36
thugthug15-Apr-10 10:36 
One last change that I threw in was to combine a couple efforts,

1) Using reflection.
2) Using assembly attributes.

First add a custom attribute to the assembly info for FrenchTranslator that looks like this:

// Apply this attribute to the assembly so
// that it will be created when the assembly
// is loaded into memory.
[assembly: Bootstrapper()]

[AttributeUsage(AttributeTargets.Assembly)]
class BootstrapperAttribute : Attribute
{
    public BootstrapperAttribute()
    {
        Translators.FrenchTranslator.Initialize();
    }
}


So if this attribute ever did load, it would call the the method Translators.FrenchTranslator.Initialize(), and the static instance would come into being. Problem is no one is every going to look for this attribute and it will not ever be loaded!

To get the attribute loaded, I added an initialize method in the translator class looks that looks like this and is called on first access to the dictionary.

static private void initialize()
{
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        // This will cause each assembly to load its attributes.
        // A custom attribute has been added to translator dlls
        // that will cause them to intialize the static members
        // of the class.
        assembly.GetCustomAttributes(true);
    }
}


So someone accesses the dictionary for the first time, the initialize method causes the all the assemblies with the specified attribute to tell there contained classes to initialize, and poof the static members exist.

The benefit of this over the previous method is that now I don't have to check every single type that has been loaded in. Smile | :)


Much Thanks to all.
James
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 11:41
mvePIEBALDconsult15-Apr-10 11:41 
AnswerRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 12:05
mvePIEBALDconsult15-Apr-10 12:05 
GeneralRe: How to force static instances of a class to initialize? Pin
thugthug16-Apr-10 2:09
thugthug16-Apr-10 2:09 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult16-Apr-10 4:25
mvePIEBALDconsult16-Apr-10 4:25 
QuestionXmlSerializer doesn't write the namespace Pin
buchstaben15-Apr-10 4:02
buchstaben15-Apr-10 4:02 
AnswerRe: XmlSerializer doesn't write the namespace Pin
Tarakeshwar Reddy15-Apr-10 4:54
professionalTarakeshwar Reddy15-Apr-10 4:54 
QuestionRe: XmlSerializer doesn't write the namespace Pin
buchstaben15-Apr-10 19:43
buchstaben15-Apr-10 19:43 
AnswerRe: XmlSerializer doesn't write the namespace Pin
Tarakeshwar Reddy16-Apr-10 5:01
professionalTarakeshwar Reddy16-Apr-10 5:01 
GeneralRe: XmlSerializer doesn't write the namespace Pin
buchstaben18-Apr-10 20:20
buchstaben18-Apr-10 20:20 
QuestionMessage Removed Pin
15-Apr-10 1:56
kulkarni.ajay15-Apr-10 1:56 
AnswerRe: How to solve->Could not connect to remote machine error Pin
Calla15-Apr-10 3:41
Calla15-Apr-10 3:41 
GeneralRe: How to solve->Could not connect to remote machine error Pin
kulkarni.ajay15-Apr-10 3:57
kulkarni.ajay15-Apr-10 3:57 
GeneralRe: How to solve->Could not connect to remote machine error Pin
OriginalGriff15-Apr-10 4:17
mveOriginalGriff15-Apr-10 4:17 
JokeRe: How to solve->Could not connect to remote machine error Pin
Calla15-Apr-10 21:27
Calla15-Apr-10 21:27 
GeneralRe: How to solve->Could not connect to remote machine error Pin
Luc Pattyn6-May-10 2:07
sitebuilderLuc Pattyn6-May-10 2:07 
AnswerRe: How to solve->Could not connect to remote machine error Pin
Rod Kemp15-Apr-10 18:39
Rod Kemp15-Apr-10 18:39 
QuestionData relations and data set Pin
eli1502197915-Apr-10 0:53
eli1502197915-Apr-10 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.