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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:53:00+00:00 2026-05-21T07:53:00+00:00

I’m using the following code to set Control properties in a thread-safe manner: private

  • 0

I’m using the following code to set Control properties in a thread-safe manner:

private delegate void SetPropertyThreadSafeDelegate<TPropertyType>(Control @this, Expression<Func<TPropertyType>> property, TPropertyType value);

public static void SetPropertyThreadSafe<TPropertyType>(this Control @this, Expression<Func<TPropertyType>> property, TPropertyType value)
{
  var propertyInfo = (property.Body as MemberExpression ?? (property.Body as UnaryExpression).Operand as MemberExpression).Member as PropertyInfo;

  if (propertyInfo == null ||
      !propertyInfo.ReflectedType.IsAssignableFrom(@this.GetType()) ||
      @this.GetType().GetProperty(propertyInfo.Name, propertyInfo.PropertyType) == null)
  {
    throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control.");
  }

  if (propertyInfo.PropertyType.IsValueType &&
      !propertyInfo.PropertyType.IsAssignableFrom(typeof(TPropertyType)))
  {
    throw new ArgumentException(string.Format("Attempted to assign incompatible value type: expecting {0}, got {1}.", propertyInfo.PropertyType, typeof(TPropertyType)));
  }

  if (@this.InvokeRequired)
  {
    @this.Invoke(new SetPropertyThreadSafeDelegate<TPropertyType>(SetPropertyThreadSafe), new object[] { @this, property, value });
  }
  else
  {
    @this.GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null, @this, new object[] { value });
  }
}

It’s called like so:

downloadProgressBar.SetPropertyThreadSafe(() => downloadProgressBar.Step, 32);

The reason for doing this is to get compile-time checking of property names and type assignments. It works perfectly for standard objects, but everything goes a bit pear-shaped with value types because the compiler is happy to accept the following, which of course bombs at runtime:

downloadProgressBar.SetPropertyThreadSafe(() => downloadProgressBar.Step, 'c');
downloadProgressBar.SetPropertyThreadSafe(() => downloadProgressBar.Step, long.MaxValue);

I’ve already modified the SetPropertyThreadSafe method to handle the case when value types are used, and throw an exception if the incorrect type is used as an argument, but what I’m really loooking for is the ability to get this method to perform compile-time type checking for 100% of cases, i.e. objects and value types. Is this even possible and if so how would I need to modify my code to do this?

  • 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-21T07:53:01+00:00Added an answer on May 21, 2026 at 7:53 am

    Change the contract to:

    public static void SetPropertyThreadSafe<TPropertyType, TValue>(
            this Control self,
            Expression<Func<TPropertyType>> property,
            TValue value)
            where TValue : TPropertyType
    

    Note that with this you no longer need to do the IsAssignableFrom check since the compiler will enforce it.

    The reasons your example compiled is because the compiler made a guess as to what the type parameter was. Here is what the compiler turns those calls into:

    progBar.SetPropertyThreadSafe<int>(() => progBar.Step, 'c');
    progBar.SetPropertyThreadSafe<long>(() => progBar.Step, long.MaxValue);
    

    Notice how the first one is int, that’s because ProgressBar.Step is an int and ‘c’ is a char which has an implicit conversion to int. Same with the next example, int has an implicit conversion to long, and the second one is long, so the compiler guesses that it is long.

    If you want inheritance and conversions like those to work, don’t make the compiler guess. Your two solutions are:

    1. Always specify the type perameter. In this case, you would have noticed that the second one is long, and fixed the problem.

    Of course this is less than ideal, because then you are basically hard coding in the type of the Func. What you really want to do is let the compiler determine both types and tell you if they are compatible.

    1. Provide a different type for both so that the compiler can figure it out for you.

    NOTE: Below is the code that I would use, which is entirely different from yours:

        public static void SetPropertyThreadSafe<TControl>(this TControl self, Action<TControl> setter)
            where TControl : Control
        {
            if (self.InvokeRequired)
            {
                var invoker = (Action)(() => setter(self));
                self.Invoke(invoker);
            }
            else
            {
                setter(self);
            }
        }
    
        public static void Example()
        {
            var progBar = new ProgressBar();
            progBar.SetPropertyThreadSafe(p => p.Step = 3);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
In order to apply a triggered animation to all ToolTip s in my app,

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.