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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:45:45+00:00 2026-05-11T18:45:45+00:00

I’m writing a wrapper for the WinForms ComboBox control that will let me populate

  • 0

I’m writing a wrapper for the WinForms ComboBox control that will let me populate the dropdown with a List<T>, and has a Selected property that returns an item of type T (or null if nothing selected).

Rather than having a Selected property, I’d like it to be named based on the generic type automatically. For example:

  • MyDropDownList<User> would have a SelectedUser property
  • MyDropDownList<Department> would have a SelectedDepartment property
  • MyDropDownList<State> would have a SelectedState property

With LINQ, I can create anonymous types during grouping, like so:

var usersByGender = users
    .GroupBy(x => x.Gender)
    .Select(group => new {Gender = group.Key, List = group.ToList()});

Which will result in a list of the generated anonymous type containing a Gender property and List<User> that are detectable by IntelliSense. The question is, can I somehow do this with properties in a generic class?

EDIT: I now realize I’m essentially asking for “method_missing” in C# 3.0, which is probably impossible. I’m open to suggestions, though.

  • 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-11T18:45:46+00:00Added an answer on May 11, 2026 at 6:45 pm

    I don’t think you can do it. The only approach that has even a remote chance of working would be to create some kind of factory method that created the type and returned it. Basically your generic type becomes a base class and IL is dynamically written creating a concrete class that inherits from the generic but with the new property added in. Of course the object that is returned would be really anonymous and you’d only be able to access it through reflection since you wouldn’t have a type to actually cast to:

    public static class CrazyFactory
    {
         public static object CreateTypedComboList<T>()
         {
              //magic happens
              return object;
         }
    }
    

    Using it would then require

    object userCombo = CrazyFactory.CreateTypedComboList<User>();
    PropertyInfo selectedUserProperty = userCombo.GetType().GetProperty("SelectedUser");
    selectedUserProperty.SetValue(userCombo, user);
    

    Which is hardly a step up from a simple T SelectedItem property.


    From a brief look it appears that this type of thing will be easier when c# 4.0 brings us dynamic types. By implementing the IDynamicObject interface it will be possible to catch all calls to undefined properties and handle them so you could use logic like the following:

    public override MetaObject GetMember(GetMemberAction action, MetaObject[] args)
    {
         //not sure on the real property name here...
         string actionName = action.Name;
    
         if (actionName = "Selected" + typeof(T).Name)
         {
              return SelectedItem; //in some MetaObject wrapper 
         }
    }
    

    I’ve been following this article and haven’t touched dynamics myself yet, but it certainly appears like what you want is possible. I still wouldn’t do it though – you won’t get Intellisense and it seems like a fragile and complicated method for defining functionality.

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

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since ELMAH is open source, I modified the Error.CS file… May 12, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer It is a bit like the question "Take all my… May 12, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer This is because you're waiting in a loop for the… May 12, 2026 at 1:56 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.