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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:32:07+00:00 2026-06-06T12:32:07+00:00

I am looking for a method to pass property itself to a function. Not

  • 0

I am looking for a method to pass property itself to a function. Not value of property. Function doesn’t know in advance which property will be used for sorting. Simplest way in this example is: creating 4 overwrites with different parameter types. Other way is using of typeof() inside function. Both these ways are unacceptable when Class1 has hundreds properties. So far I found following method:

class Class1
{
    string vehName;
    int maxSpeed;
    int fuelCapacity;
    bool isFlying;
}

class Processor
{
    List<Class1> vehicles = null;
    Processor(List<Class1> input)
    {
        vehicles = input;
    }

    List<Class1> sortBy(List<Class1> toSort, string propName)
    {
        if (toSort != null && toSort.Count > 0)
        {
            return toSort.OrderBy(x => typeof(Class1).GetProperty(propName).GetValue(x, null)).ToList();
        }
        else return null;
    }
}

class OuterUser
{
    List<Class1> vehicles = new List<Class1>();
    // ... fill the list
    Processor pr = new Processor(vehicles);
    List<Class1> sorted = pr.sortBy("maxSpeed");
}

I don’t like this method because of risk of “human error” when passing string to processing function. When the string is generated by other part of code this is going be even more ugly.
Please, suggest more elegant way to implement passing of Class1 property to function for further processing. The best option for usage IMHO will be (or something like this):

vehicles = sortBy(vehicles, Class1.maxSpeed);
  • 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-06T12:32:09+00:00Added an answer on June 6, 2026 at 12:32 pm

    You can pass a property accessor to the method.

    List<Class1> SortBy(List<Class1> toSort, Func<Class1, IComparable> getProp)
    {
        if (toSort != null && toSort.Count > 0) {
            return toSort
                .OrderBy(x => getProp(x))
                .ToList();
        }
        return null;
    }
    

    You would call it like this:

    var result = SortBy(toSort, x => x.maxSpeed);
    

    But you could go one step further and write your own extension method.

    public static class CollectionExtensions
    {
        public static List<TSource> AsOrderedListOrNull<TSource, TKey>(
            this ICollection<TSource> collection, Func<TSource,TKey> keySelector)
    
            if (collection != null && collection.Count > 0) {
                return collection
                    .OrderBy(x => keySelector(x))
                    .ToList();
            }
            return null;
        }
    }
    

    Now you can sort like this

    List<Class1> sorted = toSort.AsOrderedListOrNull(x => x.maxSpeed);
    

    but also

    Person[] people = ...;
    List<Person> sortedPeople = people.AsOrderedListOrNull(p => p.LastName);
    

    Note that I declared the first parameter as ICollection<T> because it must fulfill two conditions:

    1. It must have a Count property
    2. It must be an IEnumerable<T> in order to be able to apply the LINQ method OrderBy.

    List<Class1> is an ICollection<T> but also an array Person[] as many other collections.


    So far, I have shown how you can read a property. If the method needs to set a property, you need to pass it a setter delegate as well

    void ReadAndWriteProperty(Func<Class1, T> getProp, Action<Class1, T> setProp)
    

    Where T is the type of the property.

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

Sidebar

Related Questions

I am looking to pass an event to a helper function. This function will
I'm looking for a method that will flatten a json hash into a flattened
I was really looking at the differences between pass by value and how Java
I'm looking to create an inline function (method) inside my NVelocity template. The solution
Looking for a way to pass an associative array to a method. I'm looking
I'm looking for method of transferring user entered data from a banner Advertisement to
I'm looking for method to add gradually fading or maybe blured border (I don't
I Looking for a method that convert time string into Calendar look like this:
I am looking for a method to generate graphs from a within a console
I am looking for some method in vim-script json.dumps(join(getlines(1,'$'),'\n')) just like python's json module

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.