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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:15:45+00:00 2026-06-05T08:15:45+00:00

I am trying to create a utility function for serializing objects, Normally, serialization would

  • 0

I am trying to create a utility function for serializing objects,
Normally, serialization would happen as follows:

[Serializable]
public CoolCat : ISerializable
{
    public string Name;

    public void CoolCar(SerializationInfo info, StreamingContext context)
    {
        Name = (string)info.GetValue("Name", typeof(string));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Name", Name);
    }
}

However, I want to be able to do the following:

[Serializable]
public CoolCat : ISerializable
{
    public string Name;

    public void CoolCar(SerializationInfo info, StreamingContext context)
    {
        Name = info.GetValue<string>(() => Name);
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue<string>(() => Name);
    }
}

I do this with the following two methods:

This one for deserializing the value:

public static T GetValue<T>(this SerializationInfo Source, Expression<Func<T>> MemberExpression)
{
    string Name = ((MemberExpression)MemberExpression.Body).Member.Name;
    return (T)Source.GetValue(Name, typeof(T));
}

and this one for serializing the value:

public static void AddValue<T>(this SerializationInfo Source, Expression<Func<T>> MemberExpression)
{
    MemberExpression Body = MemberExpression.Body as MemberExpression;

    if (Body == null)
    {
        UnaryExpression UnaryBody = MemberExpression.Body as UnaryExpression;

        if (UnaryBody != null)
        {
            Body = UnaryBody.Operand as MemberExpression;
        }
        else
        {
            throw new ArgumentException("Expression is not a MemberExpression", "MemberExpression");
        }
    }

    string Name = Body.Member.Name;

    if (Body.Member is FieldInfo)
    {
        T Value = (T)((FieldInfo)Body.Member).GetValue(((ConstantExpression)Body.Expression).Value);
        Source.AddValue(Name, Value, typeof(T));
    }
    else if (Body.Member is PropertyInfo)
    {
        T Value = (T)((PropertyInfo)Body.Member).GetValue(((ConstantExpression)Body.Expression, null);
        Source.AddValue(Name, Value, typeof(T));
    }
    else
    {
        throw new ArgumentException("Expression must refer to only a Field or a Property", "MemberExpression");
    }
}

I am getting an exception when trying to get the value from the Body.Member when it is a property (when it is a field, it works fine). How can I get this?

Other questions –
1) Are there any issues with the approach that I am taking?
2) Is there a better way perhaps to go about this whole thing?
3) When will the Body.Member be a FieldInfo and when will it be a PropertyInfo?

This is sort of an extension of my previous question Here

  • 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-05T08:15:46+00:00Added an answer on June 5, 2026 at 8:15 am

    Does the AddValue method have to be so complex? I assume the following would also work. Instead of using reflection, it compiles and evaluates the lambda expression in order to get the value.

    public static void AddValue<T>(
        this SerializationInfo source, 
        Expression<Func<T>> memberExpression)
    {
        MemberExpression body = memberExpression.Body as MemberExpression;
        string name = body.Member.Name;
        Func<T> valFunc = memberExpression.Compile();
        T val = valFunc();
    
        source.AddValue(name, val, typeof(T));
    }
    

    Edit: To cater for performance-sensitive situations, I usually define the extension method with two overloads:

    public static void AddValue<T>(
        this SerializationInfo source,
        Expression<Func<T>> memberExpression)
    {
        Func<T> valFunc = memberExpression.Compile();
        T val = valFunc();
    
        source.AddValue(val, memberExpression);
    }
    
    public static void AddValue<T>(
        this SerializationInfo source,
        T val,
        Expression<Func<T>> memberExpression)
    {
        MemberExpression body = memberExpression.Body as MemberExpression;
        string name = body.Member.Name;
    
        source.AddValue(name, val, typeof(T));
    }
    

    This way, you may call either of the following options:

    // Inefficient, since it requires compilation of lambda expression:
    info.AddValue<string>(() => Name);
    
    // Faster, but requires you to specify two parameters.
    info.AddValue<string>(Name, () => Name);
    

    The latter overload has a degree of redundancy in its parameters, but would address your performance concern (being actually faster than your reflection-based implementation) whilst still preserving refactor-safety.

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

Sidebar

Related Questions

My friend is trying to create a utility function that is given some Type
I am trying to create a utility class that accepts any type of POJO,
I'm trying to create a utility class for traversing all the files in a
I am trying to create a utility that doesn't open a window when executed,
I am trying to create a utility in C# using the MVC framework where
I am trying to build some utility to create document model from a string
I am trying to write a simple utility function in Bash, which will perform
I am trying to create a jQuery function that will take a string and
I am trying to create a utility keystroke app so i can do things
I am trying to create a utility method to perform mail merge-like functionality on

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.