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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:12:49+00:00 2026-05-19T04:12:49+00:00

I was googling trying to find a way to call Control.DataBindings.Add without using a

  • 0

I was googling trying to find a way to call Control.DataBindings.Add without using a string literal but getting the property name from the property itself, which I think would be less error prone, at least for my particular case, since I normally let Visual Studio do the renaming when renaming a property. So my code would look something like DataBindings.Add(GetName(myInstance.myObject)... instead of DataBindings.Add("myObject".... So I found this:

    static string GetName<T>(T item) where T : class
    {
        var properties = typeof(T).GetProperties();
        if (properties.Length != 1) throw new Exception("Length must be 1");
        return properties[0].Name;
    }

That would be called, assuming I have a property called One, this way: string name = GetName(new { this.One }); which would give me "One". I have no clue why does it work and whether is safe to use it or not. I don’t even know what that new { this.One } means. And I don’t know on which case could it happens that properties.Length is not 1.

By the way, I just tested to rename my property One to Two and Visual Studio turned new { this.One } into new { One = this.Two }, which when used with the GetName function gave me "One", which make the whole thing useless since the name I would be passing to Control.DataBindings.Add would be still “One” after renaming the property.

  • 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-19T04:12:50+00:00Added an answer on May 19, 2026 at 4:12 am

    new { this.One } creates an instance of an anonymous type with one Property, which is, because you didn’t specify a name, called “One”. That’s why it works.

    if you use new { One = this.Two }, you give the property the name “One”. If you would leave out the part “One = “, it would work again.

    However, the method you are using might be misunderstood if one does not know how it’s intended to be used and if one does not call it using an anonymous type.

    There is another way if you don’t want to use string literals, here is one of the examples you can find on the web:
    http://www.codeproject.com/Tips/57234/Getting-Property-Name-using-LINQ.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have been googling/binging trying to find an answer on this but drawing
I've been trying to find out the name for something, but without knowning the
I've been Googling my butt off trying to find out how to do this:
I have been doing lots of googling lately trying to find a decent documentation/tutorial
I've been googling a lot lately trying to find articles, books , or even
I have been googling for several hour now trying to find an example on
I've been googling this for some time now, but I could never find an
No amount of googling seems to find the answer to this one... I'm trying
I am trying to find a way to decode the REG_BINARY value for HKLM\Software\Microsoft\Ole\DefaultLaunchPermission
I was googling this problem for many days but I didn't find a solution

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.