Home > Backend Development > C++ > How can I create, sort, and filter dynamic properties in C# at runtime?

How can I create, sort, and filter dynamic properties in C# at runtime?

Linda Hamilton
Release: 2025-01-05 01:22:41
Original
603 people have browsed it

How can I create, sort, and filter dynamic properties in C# at runtime?

Creating Dynamic Properties in C

Dynamic Property Addition and Modification

To create dynamic properties in a class at runtime, you can utilize a dictionary to store property names and values. Consider the following code:

Dictionary<string, object> properties = new Dictionary<string, object>();
Copy after login

This dictionary can be used to define dynamic properties for the class. For example, to add a dynamic property named "test" with a value of 100, you would write:

properties["test"] = 100;
Copy after login

Sorting and Filtering Dynamic Properties

Once you have created dynamic properties, you can also add sorting and filtering capabilities to your objects. Here's an example of using LINQ for filtering:

var filtered = from obj in objects
             where (int)obj["test"] >= 150
             select obj;
Copy after login

And here's an example of using a custom comparer for sorting:

Comparer<int> c = new Comparer<int>("test");
objects.Sort(c);
Copy after login

Example Implementation

The following complete code sample demonstrates the creation, modification, sorting, and filtering of dynamic properties in C#:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class ObjectWithProperties
    {
        Dictionary<string, object> properties = new Dictionary<string, object>();

        public object this[string name]
        {
            get
            {
                if (properties.ContainsKey(name))
                {
                    return properties[name];
                }
                return null;
            }
            set
            {
                properties[name] = value;
            }
        }
    }

    class Comparer : IComparer where T : IComparable
    {
        string m_attributeName;

        public Comparer(string attributeName)
        {
            m_attributeName = attributeName;
        }

        public int Compare(ObjectWithProperties x, ObjectWithProperties y)
        {
            return ((T)x[m_attributeName]).CompareTo((T)y[m_attributeName]);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // create some objects and fill a list
            var obj1 = new ObjectWithProperties();
            obj1["test"] = 100;
            var obj2 = new ObjectWithProperties();
            obj2["test"] = 200;
            var obj3 = new ObjectWithProperties();
            obj3["test"] = 150;
            var objects = new List(new ObjectWithProperties[] { obj1, obj2, obj3 });

            // filtering:
            Console.WriteLine("Filtering:");
            var filtered = from obj in objects
                           where (int)obj["test"] >= 150
                           select obj;
            foreach (var obj in filtered)
            {
                Console.WriteLine(obj["test"]);
            }

            // sorting:
            Console.WriteLine("Sorting:");
            Comparer c = new Comparer("test");
            objects.Sort(c);
            foreach (var obj in objects)
            {
                Console.WriteLine(obj["test"]);
            }
        }
    }
}
Copy after login

The above is the detailed content of How can I create, sort, and filter dynamic properties in C# at runtime?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template