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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:02:48+00:00 2026-06-03T06:02:48+00:00

How do you create a lambda expression for a function that can handle unknown

  • 0

How do you create a lambda expression for a function that can handle unknown types? Sorry, I know the question is vague and I had a hard time forming it. I can only hope you have a minute and read through my story, which should clear things up a bit.

My goal is to deserialize an array of string values into an object using a predefined data contract. The members of the data contract have a position number. The deserializer’s simple job is to map a value to a data member (after doing the appropriate type conversion), and build the object.

The problem is that the deserialization performance sucks! After running VS Profiler, I found that PropertyInfo.SetValue(), which is what is used to populate the members of an object, is taking the most time. My program has to deserialize thousands of object at any given time. A data contract usually has 100 members. So we are talking 100,000 calls to SetValue() for each 1000 objects, and it’s dragging. Here’s a sample of the call to SetValue:

// for each data contract type
// go through each property and set the value
foreach(PropertyInfo pi in pis)
{
    object data = convertStringToMemberType(pi, attributeArray, valueStringArray);
    pi.SetValue(objectToBuild, data, null);
}

Then I found this page from Unknown Recipes, which has a promising solution to this performance problem.
It looks like I need to use a compiled lambda expression to replace SetValue, but I’m running into a problem with casting. Following the example form the link above, I now have a replacement for SetValue(). The replacements are Action delegates, which are compiled lambda expressions.

First, I extended the PropertyInfo class.

public static class PropertyInfoExtensions
{

    public static Action<object, object> GetValueSetter(this PropertyInfo propertyInfo)
    {
        var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
        var argument = Expression.Parameter(typeof(object), "a");
        var setterCall = Expression.Call(
            instance,
            propertyInfo.GetSetMethod(),
            Expression.Convert(argument, propertyInfo.PropertyType));
        return (Action<object, object>)Expression.Lambda(setterCall, instance, argument).Compile();
    }
}

Then I built a Dictionary<PropertyInfo, Action<object, object> object, which ties each propertyInfo object to its corresponding Action delegate. This way I can “cache” the compiled lambda and reuse it in a batch of deserialization. And this is how I call it now:

foreach(PropertyInfo pi in pis)
{
    object data = convertStringToMemberType(pi, attributeArray, valueStringArray);
    var setValueDelegate = _actionDelegateDict[pi];
    setValueDelegate(objectToBuild, data);
}

However, I am getting the following exception:

Unable to cast object of type 'System.Action`2[Test.DataContract1,System.Object]' to type 'System.Action`2[System.Object,System.Object]'.

Here DataContract1 is the type of the object I’m trying to build. It is known only at run time, which is unlike the scenario in the example from Unknown Recipes, where the type is known at compile time. How would you go about making this lambda expression work?

Thank you so very much for your time!

  • 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-03T06:02:49+00:00Added an answer on June 3, 2026 at 6:02 am

    Sounds a lot like what I did with my FastReflection library. You are pretty much there, you would just change the instance parameter to object type and then do a cast on that expression to the actual type.

    I think your code you have right now would work if it were changed to this.

    public static class PropertyInfoExtensions
    {
        public static Action<object, object> GetValueSetter(this PropertyInfo propertyInfo)
        {
            var instance = Expression.Parameter(typeof(object), "i");
            var argument = Expression.Parameter(typeof(object), "a");
            var setterCall = Expression.Call(
                Expression.Convert(instance, propertyInfo.DeclaringType),
                propertyInfo.GetSetMethod(),
                Expression.Convert(argument, propertyInfo.PropertyType));
            return Expression.Lambda<Action<object,object>>(setterCall, instance, argument).Compile();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a lambda expression (Linq, C# 3.5) that can perform
Is it possible to create a custom lambda function that I can replace with
how can I create a dynamic lambda expression to pass to use in my
I'd like to create a class where the client can store a lambda expression
I want to create dynamic lambda expressions so that I can filter a list
I am trying to create a dynamic lambda expression (parsed from text), that does
I'm trying to create linq lambda expression to return customer whose first or last
I recently discovered that I can use lambdas to create simple event handlers. I
You can use Lambda Expression Objects to represent a lambda as an expression. How
Is it possible to create a method which returns a lambda expression? I couldn't

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.