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

  • SEARCH
  • Home
  • 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 4097770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:12:57+00:00 2026-05-20T20:12:57+00:00

Consider a class that has some events. This event list is going to grow.

  • 0

Consider a class that has some events. This event list is going to grow. Some are optional. Others are required.

To simplify some initial validation I have a custom attribute that marks an event as a required one.
For example:

    [RequiredEventSubscription("This event is required!")]
    public event EventHandler ServiceStarted;

So far so good.
To validate all events, using reflection I iterate the event list and grab the custom attributes.
But I need a way to determine if the event is subscribed or not.

Without reflection the ServiceStarted.GetInvocationList does the job. But the event must come from this list:
var eventList = this.GetType().GetEvents().ToList();

Is there any way to check if a given event, from an event list, is subscribed using reflection?

–[Update]–
Here’s a possible solution based on Ami’s answer:

    private void CheckIfRequiredEventsAreSubscribed()
    {
        var eventList = GetType().GetEvents().ToList().Where(e => Attribute.IsDefined(e, typeof(RequiredEventSubscription)));

        StringBuilder exceptionMessage = new StringBuilder();
        StringBuilder warnMessage = new StringBuilder();

        foreach (var evt in eventList)
        {
            RequiredEventSubscription reqAttr = (RequiredEventSubscription) evt.GetCustomAttributes(typeof(RequiredEventSubscription), true).First();
            var evtDelegate = this.GetType().GetField(evt.Name, BindingFlags.Instance | BindingFlags.NonPublic);
            if (evtDelegate.GetValue(this) == null)
            {
                warnMessage.AppendLine(reqAttr.warnMess);
                if (reqAttr.throwException) exceptionMessage.AppendLine(reqAttr.warnMess);
            }
        }
        if (warnMessage.Length > 0)
            Console.WriteLine(warnMessage);
        if (exceptionMessage.Length > 0)
            throw new RequiredEventSubscriptionException(exceptionMessage.ToString());
    }

Many thanks!!

  • 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-20T20:12:58+00:00Added an answer on May 20, 2026 at 8:12 pm

    There are some major design issues here. In general, there’s no way to ask an object who its events’ subscribers are. It’s highly unusual that anyone would want this functionality, but if you really want it, you should get classes to somehow expose this, for example, by implementing an interface with a method such as:

    public IEnumerable<Delegate> GetSubscribers(string eventName);
    

    Anyway, to answer the question as asked, you can use reflection, but only if you know exactly how the subscribers are being maintained. For example,
    assuming that all events are implemented with the current implementation of C# field-like events, you can do something like (strongly discouraged):

    object o = ...
    
    var unsubscribedEvents = 
      from e in o.GetType().GetEvents()
      where Attribute.IsDefined(e, typeof(RequiredEventSubscriptionAttribute))
      let field = o.GetType()
                   .GetField(e.Name, BindingFlags.NonPublic | BindingFlags.Instance)
                   .GetValue(o)
      where field == null
      select field;
    
    var isValid = !unsubscribedEvents.Any();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider one has some user defined types, and containers of those types that are
Consider the class below that represents a Broker: public class Broker { public string
Consider a hypothetical method of an object that does stuff for you: public class
Consider this: public class TestClass { private String a; private String b; public TestClass()
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this example (typical in OOP books): I have an Animal class, where each
I have a container class that has a generic parameter which is constrained to
I have a class myclass that has a private member param_map class some_class {
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()

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.