Работа с полями класса

http://stackoverflow.com/questions/9210428/how-to-convert-class-into-dictionarystring-string

someObject.GetType()
     .GetProperties(BindingFlags.Instance | BindingFlags.Public)
          .ToDictionary(prop => prop.Name, prop => prop.GetValue(someObject, null))

http://stackoverflow.com/questions/4943817/mapping-object-to-dictionary-and-vice-versa/4944547#4944547

public static class ObjectExtensions
{
    public static T ToObject<T>(this IDictionary<string, object> source)
        where T : class, new()
    {
            T someObject = new T();
            Type someObjectType = someObject.GetType();
 
            foreach (KeyValuePair<string, object> item in source)
            {
                someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);
            }
 
            return someObject;
    }
 
    public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        return source.GetType().GetProperties(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source, null)
        );
 
    }
}
 
class A
{
    public string Prop1
    {
        get;
        set;
    }
 
    public int Prop2
    {
        get;
        set;
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        dictionary.Add("Prop1", "hello world!");
        dictionary.Add("Prop2", 3893);
        A someObject = dictionary.ToObject<A>();
 
        IDictionary<string, object> objectBackToDictionary = someObject.AsDictionary();
    }
}

http://stackoverflow.com/questions/2051834/exclude-property-from-gettype-getproperties

public class SkipPropertyAttribute : Attribute
{
}
 
public static class TypeExtensions
{
    public static PropertyInfo[] GetFilteredProperties(this Type type)
    {
        return type.GetProperties().Where(pi => pi.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length == 0).ToArray();
    }       
}
 
public class Test
{
    public string One { get; set; }
 
    [SkipProperty]
    public string Two { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
        var t = new Test();
        Type ty = t.GetType();
 
        PropertyInfo[] pinfo = ty.GetFilteredProperties();
        foreach (PropertyInfo p in pinfo)
        {
            Console.WriteLine(p.Name);
        }
 
        Console.ReadKey();
    }
}

http://stackoverflow.com/questions/4951233/compare-two-objects-and-find-the-differences

static class extentions
{
    public static List<Variance> DetailedCompare<T>(this T val1, T val2)
    {
        List<Variance> variances = new List<Variance>();
        FieldInfo[] fi = val1.GetType().GetFields();
        foreach (FieldInfo f in fi)
        {
            Variance v = new Variance();
            v.Prop = f.Name;
            v.valA = f.GetValue(val1);
            v.valB = f.GetValue(val2);
            if (!v.valA.Equals(v.valB))
                variances.Add(v);
 
        }
        return variances;
    }
 
 
}
class Variance
{
    public string Prop { get; set; }
    public object valA { get; set; }
    public object valB { get; set; }
}

Оставить комментарий


Примечание - Вы можете использовать эти HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>