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 8178635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:48:51+00:00 2026-06-06T23:48:51+00:00

I asked this question last week about how to manually serialize objects, and I

  • 0

I asked this question last week about how to manually serialize objects, and I have since been trying to make generic wrapper that will solve my problem. (I’m doing it this way since I’m stuck between 3rd party code.) I pretty much have everything working except for the events.

Since I am technically creating a new instance of the object when I deserialize, I no longer retain my event subscriptions. Is there a way to copy over the subscriptions, or forward all events of the deserialized version back to the original version?

Here is something similar to what I am currently using:

[Serializable]
public class Wrapper<T> : ISerializable
{
    public T Wrappee { get; set; }

    public Wrapper(T wrappee)
    {
        Wrappee = wrappee;
    }

    protected Wrapper(SerializationInfo info, StreamingContext context)
    {
        Wrappee = (T)FormatterServices.GetUninitializedObject(typeof(T));

        FieldInfo[] fields = typeof(T).GetFields();
        foreach (FieldInfo fieldInfo in fields)
        {
            var value = info.GetValue(fieldInfo.Name, fieldInfo.FieldType);
            fieldInfo.SetValue(Wrappee, value);
        }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        FieldInfo[] fields = typeof(T).GetFields();
        foreach (FieldInfo fieldInfo in fields)
        {
            var value = fieldInfo.GetValue(Wrappee);
            info.AddValue(fieldInfo.Name, value);
        }
    }
}

Well that’s just the general idea of it. Basically I was wondering if there was a way to do something with the events similar to what I did for the fields. I know I can get the EventInfo the same way, but I don’t know how this would translate to fixing the subscriptions.

Any help would be greatly appreciated.

Edit
So I am still trying to find a way to do this, and I came across this question. Would I be able to do something like this, and then forward the events back to the original appdomain without having to fix the subscriptions on that end?

  • 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-06-06T23:48:52+00:00Added an answer on June 6, 2026 at 11:48 pm

    The question is related to the question of how object graphs can be serialized. An event holds references to objects plus references to methods of these objects. How could object graphs be serialized?

    I would give each object to be serialized a unique ID. If you do not want to store this ID in the objects, you can store them in a Dictionary<TObject, TID>. In a first pass generate the IDs and add them to the dictionary. In a second pass serialize the objects together with their IDs. Instead of references to other objects, serialize the IDs of the related objects that you can find in the dictionary.

    For events, store a method name in addition to the referenced object’s ID.

    When de-serializing the objects, you will need a reverse dictionary: Dictionary<TID, TObject>. In a first pass de-serialize the objects and add them to the dictionary keyed by their IDs. Also add the de-serialized objects to a list. In a second pass go through the list and fix the references and events.

    I hope this gives you an idea of a possible procedure.


    UPDATE

    You can get the event info with the GetInvocationList method

    class ClassWithEvent
    {
        public event EventHandler SomeEvent;
    
        public Delegate[] GetSomeEventInvocationList()
        {
            return SomeEvent.GetInvocationList();
        }
    }
    

    I tested the method with

    class Subscriber
    {
        public void SomeMethod(object sender, EventArgs e)
        {
        }
    }
    
    static class Serialize
    {
        public static void Test()
        {
            var objWithEvent = new ClassWithEvent();
            var subscriber1 = new Subscriber();
            var subscriber2 = new Subscriber();
    
            objWithEvent.SomeEvent += subscriber1.SomeMethod;
            objWithEvent.SomeEvent += subscriber2.SomeMethod;
    
            Delegate[] eventInfo = objWithEvent.GetSomeEventInvocationList();
            foreach (Delegate d in eventInfo) {
                Console.WriteLine("Target = {0},   Method = {1}", 
                                  d.Target, d.Method.Name);
            }
        }
    }
    

    Calling Serialize.Test(); outputs this in the console

    Target = StackOverflowTests.SerializeEvent.Subscriber, Method = SomeMethod
    Target = StackOverflowTests.SerializeEvent.Subscriber, Method = SomeMethod

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

Sidebar

Related Questions

This is similar to this question I asked last week. My dataGrid is populated
I asked a question last week about how to loop a YouTube video from
This is related to another question I asked last week, but the current issue
Sorry for being to vague last time I asked this question. I have this
this question may have been asked many times, but could not find any suitable
I know that this question has been asked loads of times, but I have
I asked this question last week on SO- SQL Exclusion Query It looks like
So based on this question ( here ), of which I asked last week,
This is a follow-up to a question I asked last year about setting up
Variations of this question have been asked before, but it seems like the issue

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.