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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:39:07+00:00 2026-06-14T08:39:07+00:00

I am having a little trouble with something I am working on. I initially

  • 0

I am having a little trouble with something I am working on. I initially created a generic layer that sits between my business objects and the data access layer which is working fine. I then recently read about something called Expression Trees which is apparently more efficient and has proven to be so as I swapped Activator.CreateInstance() with an expression and has improved my generic layer exponentially.

I am still doing some reading about the whole area (Expressions) but I came across some code which I want to try making generic. At the moment, you have to pass in a concrete type such as a string, int, decimal etc. I was this bit to be generic. I Tried a couple of things but failed. The bit I want generic is Action, I don’t want to pass in a string I want to be able to pass in the type of the property generically, i.e. typeof(T).GetProperty(“Forename”).PropertyType. Is this possible? Was thinking of doing a switch statement which is kinda foo bar.

Thanks in advance, Onam.

public class TTT<T> where T : new()
{
    public void Do(object t)
    {
        MethodInfo info = typeof(T).GetProperty("Forename").GetSetMethod();

        ParameterExpression param = Expression.Parameter(typeof(string), "val");

        MethodCallExpression call = Expression.Call(Expression.Constant(t), info,
            new ParameterExpression[] { param });

        Action<string> action = Expression.Lambda<Action<string>>(call, param).Compile();

        action("hi");
    }
}
  • 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-14T08:39:08+00:00Added an answer on June 14, 2026 at 8:39 am

    First, note that this is not a good way to do this; there is no performance advantage if you are building an Expression per-call, and then Compile()-ing it, and then invoking it. Reflection would be faster. If you need performance, look at a library such as “FastMember”, where this would just be:

    var accessor = TypeAccessor.Create(typeof(T));
    accessor[target, "Forename"] = value;
    

    (where that is fully optimized via meta-programming and automatic caching)


    If you want the type to be dynamic, then there are two options:

    • type it using Expression.GetActionType and use DynamicInvoke – really bad performance (hint: don’t do this)
    • type it as Action<object> and do a cast inside the expression (fine)

    So something like:

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    class Foo
    {
        public string Forename {get;set;}
    }
    class Test<T>
    {
        public void Do(object target, object value)
        {
            var obj = Expression.Constant(target, typeof(T));
            var member = Expression.PropertyOrField(obj, "Forename");
            var param = Expression.Parameter(typeof(object));
            Type type;
            switch(member.Member.MemberType)
            {
                case MemberTypes.Field:
                    type = ((FieldInfo)member.Member).FieldType; break;
                case MemberTypes.Property:
                    type = ((PropertyInfo)member.Member).PropertyType; break;
                default:
                    throw new NotSupportedException(member.Member.MemberType.ToString());
            }
            var body = Expression.Assign(member, Expression.Convert(param, type));
            var lambda = Expression.Lambda<Action<object>>(body, param);
            lambda.Compile()(value);
        }
    }
    static class Program
    {
        static void Main()
        {
            var obj = new Foo();
            new Test<Foo>().Do(obj, "abc");
            Console.WriteLine(obj.Forename);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to
I m having little trouble finding a relation between the movement at centre and
I am having a little trouble with jScrollPane . I created a list of
I'm having a little trouble with RadioGroups. I created a blank radiogroup in layout.
Hey I'm having a little trouble. I've been working with xna for a while,
I'm fairly new to JQuery and am having trouble with something that I'm sure
I'm having a little trouble with a jQuery plugin wrote. I've got it working
I'm having a little trouble with some MySQL relationships. I think I'm missing something
My python script is working now, but I'm having a little trouble: Here is
I am having a little trouble retrieving the values from a JSON object that

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.