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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:51:31+00:00 2026-05-23T09:51:31+00:00

I have two classes, which are passed to Serialization method and I would like

  • 0

I have two classes, which are passed to Serialization method and I would like to access two properties of these classes in Serialization method. The problem is that Serialization method parameter are passed as generic type and I do not know how to access properties of passed class in this case. The example below.

    public class MyClass1 
    {

            public string MyProperty1 { get; set; }

            //These properties are shared in both classes
            public bool Result { get; set; }
            public string EngineErrorMessage { get; set; }

    }
    public class MyClass2 
    {

            public string MyProperty2 { get; set; }

            //These properties are shared in both classes
            public bool Result { get; set; }
            public string EngineErrorMessage { get; set; }

    }


//The method is used to serialize classes above, classes are passed as generic types
    public void Serialization<T>(ref T engine)
            {
                try
                {
                 //Do some work with passed class
                 }
                catch (Exception e)
                {

                   //If Exception occurs I would like to write values to passed class properties, how to do that?
                   Result = false;
                   EngineErrorMessage = e.Message;
                }
    }

Full method code

     public void Submit<T>(ref T engine)
        {
            try
            {

                var workingDir = Path.Combine(Settings.FileStoragePath, Helpers.GetRandomInt(9).ToString());



                Directory.CreateDirectory(workingDir);
                var inputFile = Path.Combine(workingDir, Settings.InFileName);
                var outputFile = Path.Combine(workingDir, Settings.OutFileName);
                var deleteFile = Path.Combine(workingDir, Settings.DelFileName);

                try
                {



                    using (var stream = new FileStream(inputFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        Serializer.Serialize(stream, engine);
                    }


                    CheckStatus(outputFile);


                    using (var stream = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        engine = Serializer.Deserialize<T>(stream);                        
                    }


                }
                finally
                {
                    File.Create(deleteFile).Dispose();
                }
            }
            catch (Exception e)
            {
                //ToDo: Not implemented yet.
/*               Result = false;
               ErrorMessage = e.Message;*/
            }
        }
  • 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-23T09:51:32+00:00Added an answer on May 23, 2026 at 9:51 am

    Declare an interface containing the properties Result and EngineErrorMessage. Now you have two options:

    1. Add a constraint to your serialization type parameter so that only types that derive from the interface mentioned above can be serialized, or
    2. In your catch block try to cast engine to the interface mentioned above. If the cast succeeds, write the propertie values, otherwise do nothing.

    Sample:

    public interface ISerializationErrorWriter
    {
        bool Result { set; get; }
        string EngineErrorMessage { set; get; }
    }
    
    public class MyClass1 : ISerializationErrorWriter
    {
        public string MyProperty1 { get; set; }
    
        public bool Result { get; set; }
        public string EngineErrorMessage { get; set; }
    }
    
    public class MyClass2 : ISerializationErrorWriter
    {
        public string MyProperty2 { get; set; }
    
        public bool Result { get; set; }
        public string EngineErrorMessage { get; set; }
    }
    
    // Option 1:
    public void Serialization_1<T>(ref T engine) where T : ISerializationErrorWriter
    {
        try
        {
            //Do some work with passed class
        }
        catch (Exception e)
        {
            engine.Result = false;
            engine.EngineErrorMessage = e.Message;
        }
    }
    
    // Option 2:
    public void Serialization_2<T>(ref T engine)
    {
        try
        {
            //Do some work with passed class
        }
        catch (Exception e)
        {
            var serializationErrorWriter = engine as ISerializationErrorWriter;
            if(serializationErrorWriter != null)
            {
                serializationErrorWriter.Result = false;
                serializationErrorWriter.EngineErrorMessage = e.Message;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two classes which have some common methods like funcA(), funcB() and some
I have implemented the two classes shown at http://tomcat.apache.org/tomcat-6.0-doc/aio.html which gives a messenger application
Okay, so I have two classes, call them A and B--in that order in
Say I have two classes which extend Event : public class CustomEventOne extends Event
Ok so I have two classes in which they are not really associated with
I have two classes: container which contains dynamic ordered list of Element . What
I have two classes, A and B. B knows about A, and A doesn't
Okay, so here's a problem I'm running into. I have some classes in my
I have an SQL query that takes these parameters: @SearchFor nvarchar(200) = null ,@SearchInLat
I have two classes in a model . One is Sdr_Layer class . Other

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.