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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:23:45+00:00 2026-05-12T12:23:45+00:00

I am trying to serialize something based upon meeting particular criteria. To this end

  • 0

I am trying to serialize something based upon meeting particular criteria.

To this end my original hope was to use attributes containing a lambda expression on an object’s properties.

However, as this cannot be done I’ve settled for having a Func<T,bool> member within the class and passing the type (or first parameter type) and name of this Func through the property attribute. E.g.:

Func<SomeObject, bool> func = (p => p.Value == 4);
[FuncAtt(typeof(SomeObject), "func")]
public SomeObject PropertyName { get; set;}

In my serializer I need to call this Func<T, bool>.

Let’s assume I have a Type t which is equal to typeof(SomeObject) in this case, or more abstractly, typeof(T). I can also get the Func<T,bool> itself, but only through reflection as an object.

My naive approach is something along these lines:

object func = typeof(MyClass).GetField(attribute.FuncName).GetValue(MyClassInstance);
Type funcType = typeof(Func<,>).MakeGenericType(attribute.Type, typeof(bool));

ParameterExpression p = Expression.Parameter(attribute.Type, objectToSerialize);
LambdaExpression l = Expression.Lambda(funcType, func, p); /* Won't work */

But this leads to the problem of casting a lambda to a delegate which is apparently erroneous.

I tried this in place of ‘func’:

(Expression)((Action)(() => func))

But that relies on func being a method call not a lambda.

So, can anyone point me in the right direction?

  • 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-12T12:23:46+00:00Added an answer on May 12, 2026 at 12:23 pm

    You can just do something like this, without need for expressions:

    public static class Test
    {
        public static Predicate<int> func = s => s > 20;
    }
    

    and to get the value:

        private void Form1_Load(object sender, EventArgs e)
        {
            var a = typeof(Test).GetField("func");
    
            bool validates = ((Predicate<int>)a.GetValue(null)).Invoke(100);
        }
    

    edit to get the value without knowing the type:

    bool validates = (bool)((Delegate)a.GetValue(null)).DynamicInvoke(100);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to serialize date time object? I am trying something like this. my_date_time =
I have been trying to serialize a pretty big object. This object uses dictionaries
I am getting this error when trying to serialize. The answer to this question:
I'm trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I've
I am trying to serialize a JFrame containing a JDesktopPane with several JInternalFrames. I
I'm trying to use this C++ class as a base for my own application's
I am trying to write a class to serialize/deserialize something as simple as: <table
I'm trying to do something like this in seaside 2.9 html button onClick: (html
I have been trying to serialize a list that contains arrays and lists. I
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like

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.