Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:24:45+00:00 2026-05-16T20:24:45+00:00

So I have this class public static class MyClass { static MyClass() { …

  • 0

So I have this class

public static class MyClass
{
    static MyClass()
    {
        ...
    }
}

Which has no methods, fields or properties. All it does is wire up handlers to static events defined elsewhere.

Since the Type initializer never gets called, because the static class is never actually accessed, the events don’t get wired up.

So I was hoping to be able to invoke the type initializer via reflection ala typeof(MyClass).TypeInitializer().Invoke(...) which blows up, with an exception stating that MyClass is an abstract class.

Eventually the app will have other static classes with the same format that correspond to business rules. Before anything is saved to the DB static events are fired that correspond to the type of object being saved. So if what I want to do ends up not being possible, any refactoring recommendations would have to follow that structure.

EDIT:

I may not have been quite clear on exactly what I’m trying to do. Basically I have a data layer, where you can initialize an instance of a DataContext and then when SubmitChanges() is called I check the ChangeSet for for inserts/updates/deletes and trigger static events for each type that is getting inserted/updated/deleted. This all works great, I’m just looking for a way to wire up handlers to the events once when the app starts. So what I was playing with was this:

static DataContext()
{
    System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
        .Where(t => t.Namespace == 'Data.Business')
        .ToList()
        .ForEach( t => { 
            // invoke the static TypeInitializer here,
            // so that it can wire up it's event handlers.
        });
}

I could use a static Initialize method, but since these should only ever be initialized once, I was thinking TypeInitializer.

EDIT 2:

I’ve read up on MEF and this really provides a way to do what I wanted to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:24:46+00:00Added an answer on May 16, 2026 at 8:24 pm

    Why not just make a static method, i.e. Initialize(), that wires up all the event handlers and just call MyClass.Initialize()?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following class: Class myclass{ public static $item_row_name = array( array('Item 1','Item_1','Help
I have this static method on C# : public class MyClass { public MyClass()
I have a class which has a large number of static fields of a
i have this scenario: class MyClass { Producer p; Consumer c; public static void
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have this example: public class Inheritance { public static class Animal { public
I have this code that runs but never stops. class A { public static
Okay, so i have this code i wrote: class Connection { public static StreamWriter
say I have: class Test { public static int Hello = 5; } This
I have created my own Attached Property like this: public static class LabelExtension {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.