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

  • Home
  • SEARCH
  • 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 7986049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:44:09+00:00 2026-06-04T11:44:09+00:00

I want to set the property value on an object without knowing the object

  • 0

I want to set the property value on an object without knowing the object type at compile time; I want it to be fast (i.e. not using Reflection every time); I know property name and type.

The fastest way (afaik) is to use delegates; so this is what I have so far:

class User // this is an example.. Assume I don't know which type this is.
 {
    public string Name {get;set;}   
 }

public static Action<object, object> CreatePropertySetter(Type targetType, string propertyName)
{
    ParameterExpression targetObjParamExpr = Expression.Parameter(targetType);
    ParameterExpression valueParamExpr = Expression.Parameter(targetType.GetProperty(propertyName).PropertyType);

    MemberExpression propertyExpr = Expression.Property(targetObjParamExpr, propertyName);

    BinaryExpression assignExpr = Expression.Assign(targetObjParamExpr, valueParamExpr);

    Action<object, object> result = Expression.Lambda<Action<object, object>>(assignExpr, targetObjParamExpr, valueParamExpr).Compile();
    return result;
}

Then I’d make this call:

User user = new User();
var userNameSetter = CreatePropertySetter(user.GetType(), "Name");
userNameSetter(user, "Bob");

However, it doesn’t like the fact that I pass User type object instead of Object, and fails with “ParameterExpression of type ‘User’ cannot be used for delegate parameter of type ‘System.Object“.

I’m new to expression trees, so a bit lost here. Why can’t it cast User to object ? Do I need a cast somewhere ?

The “Action” doesn’t look great either; would be nicer to just return a delegate taking arguments (User user, string propertyValue). Again, not sure how to accomplish that. Actually, I’ve tried it with Delegate.CreateDelegate, but it invokes using .Invoke() method, which is slow (is this the only way?); same with Expression.Lambda (non-generic).

Any thoughts ?

Also, is there a good (better than msdn) documentation on Expression Trees? The msdn version really lacks details.

  • 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-04T11:44:10+00:00Added an answer on June 4, 2026 at 11:44 am

    If you want to use Expression, then: Convert…

    static void Main()
    {
        var setter = CreatePropertySetter(typeof (User), "Name");
        var obj = new User();
        setter(obj, "Fred");
    }
    public static Action<object, object> CreatePropertySetter(Type targetType, string propertyName)
    {
        var target = Expression.Parameter(typeof (object), "obj");
        var value = Expression.Parameter(typeof (object), "value");
        var property = targetType.GetProperty(propertyName);
        var body = Expression.Assign(
            Expression.Property(Expression.Convert(target, property.DeclaringType), property),
            Expression.Convert(value, property.PropertyType));
    
        var lambda = Expression.Lambda<Action<object, object>>(body, target, value);
        return lambda.Compile();
    }
    

    However! You might want to look at FastMember (also available on NuGet), which wraps all of this up for you very conveniently (and uses raw IL for silly craziness).

    If you want to use a typed delegate, you’ll need to know the types in advance. If you know the types, you could add some generic:

    static void Main()
    {
        var setter = CreatePropertySetter<User,string>("Name");
        var obj = new User();
        setter(obj, "Fred");
    }
    public static Action<TType, TValue> CreatePropertySetter<TType, TValue>(string propertyName)
    {
        var target = Expression.Parameter(typeof (TType), "obj");
        var value = Expression.Parameter(typeof (TValue), "value");
        var property = typeof(TType).GetProperty(propertyName);
        var body = Expression.Assign(
            Expression.Property(target, property),
            value);
    
        var lambda = Expression.Lambda<Action<TType, TValue>>(body, target, value);
        return lambda.Compile();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set a property of an object that is an array type.
I want to set a DateTime property to the previous day at time 00:00:00.
If i want to set a colour property to something thats non-standard (i.e. not
I have a TabPanel whose HeaderText property I want to set using a code
i write a function want to set or get style property value: function $(ID){return
Is it possible to set the value behind a two-way binding directly, without knowing
My SessionContext class is given below,I want to set User property as SiteUserDomainModel(it is
I want to set the maxChars property of the TextInput of an editable ComboBox.
I want to set the tabIndex property for a row of textbox(s) that are
I want to set the MaxReceivedMessageSize property to some higher limit (Due to (400)

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.