开发者

Iterate over object fields in C#

开发者 https://www.devze.com 2023-04-01 01:56 出处:网络
Foo is a class with a lot of string fields. I want to create a method Wizardify that performs an operation on many of the fields of the object. I could do it like this:

Foo is a class with a lot of string fields. I want to create a method Wizardify that performs an operation on many of the fields of the object. I could do it like this:

Foo Wizardify(Foo input)
{
    Foo result;
    result.field1 = Bar(input.field1);
    result.field2 = Bar(input.field2);
    result.field3 = Bar(input.field3);
    ...

This is some easily generated cod开发者_StackOverflow社区e, but I prefer not to waste fifty lines on this. Is there a way to go over selected fields of an object? Note that there are four or five fields I want to work on in a different way and they should be excluded from the iteration.


try

foreach ( FieldInfo FI in input.GetType().GetFields () )
{
    FI.GetValue (input)
    FI.SetValue (input, someValue)
}

Though I would not recommend the reflection approach for known Types - it is slow and depending on your specific scenario could pose some permission issue at runtime...


This is what I have - it gives me a list (names) of all properties in my classes, that later I can work on with Reflection or "Expression trees":

private static string xPrev = "";
        private static List<string> result;

        private static List<string> GetContentPropertiesInternal(Type t)
        {
            System.Reflection.PropertyInfo[] pi = t.GetProperties();

            foreach (System.Reflection.PropertyInfo p in pi)
            {
                string propertyName = string.Join(".", new string[] { xPrev, p.Name });

                if (!propertyName.Contains("Parent"))
                {
                    Type propertyType = p.PropertyType;

                    if (!propertyType.ToString().StartsWith("MyCms"))
                    {
                        result.Add(string.Join(".", new string[] { xPrev, p.Name }).TrimStart(new char[] { '.' }));
                    }
                    else
                    {
                        xPrev = string.Join(".", new string[] { xPrev, p.Name });
                        GetContentPropertiesInternal(propertyType);
                    }
                }
            }

            xPrev = "";

            return result;
        }

        public static List<string> GetContentProperties(object o)
        {
            result = new List<string>();
            xPrev = "";

            result = GetContentPropertiesInternal(o.GetType());

            return result;
        }

Usage: List<string> myProperties = GetContentProperties(myObject);


Loop through typeof(YourType).GetProperties() and call GetValue or SetValue.

Note that reflection is rather slow.


You could use the Dynamic Language Runtime to generate a lambda of the type Func. You'll just need to generate the lambda once (you can cache it away) and there'll be no reflection performance hit.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号