No problem, here you go (i think your forum software destroys indentation
though):


using System;
using System.ComponentModel;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Helpers
    {
        public static IEnumerable<PropertyValue> GetProperties(object o)
        {
            if (o != null) {
                PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(o);
                foreach (PropertyDescriptor prop in props) {
                    object val = prop.GetValue(o);
                    if (val != null) {
                        yield return new PropertyValue { Name = prop.Name, Value
= val };
                    }
                }
            }
        }

        public sealed class PropertyValue
        {
            public string Name { get; set; }
            public object Value { get; set; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var anon=new { Key1="Val1", Key2="Val2", Key3="Val3", Key4="Val4"};
            
            foreach (Helpers.PropertyValue nameValue in
Helpers.GetProperties(anon))
            {
                Console.WriteLine("{0}={1}", nameValue.Name, nameValue.Value);
            }
            
            Console.ReadKey();
        }
    }
}



-- 
<http://forum.pspad.com/read.php?2,67851,67896>
PSPad freeware editor http://www.pspad.com

Odpovedet emailem