public class atrstr : System.Attribute
public class atrint : System.Attribute
[atrstr(wartosc = "nazwa",wartoscint=999)]
public static class helper1
public static System.Collections.Generic.List<System.ComponentModel.PropertyDescriptor> getprop<T>(this T t)
return System.ComponentModel.TypeDescriptor.GetProperties(typeof (T)).OfType<System.ComponentModel.PropertyDescriptor>().ToList();
public static System.Collections.Generic.List<System.ComponentModel.PropertyDescriptor> getprop(this object t)
return System.ComponentModel.TypeDescriptor.GetProperties(t.GetType()).OfType<System.ComponentModel.PropertyDescriptor>().ToList();
public static T getAttr<T>(this System.ComponentModel.PropertyDescriptor pd)
return pd.Attributes.OfType<System.Attribute>().Where(r => r.GetType() == typeof (T)).OfType<T>().FirstOrDefault();
public static System.Nullable<TO> val<T, TO>(this T t, Func<T, System.Nullable<TO>> f)where TO : struct
return (t == null ? null : f(t));
public static dynamic valdyn<T>(this T t,Func<T,dynamic> f)
return (t==null ? null: f(t));
public static TO valnull<T, TO>(this T t, Func<T, TO> f)where TO : class
return (t == null ? null : f(t));
public static string getatrstr(this System.ComponentModel.PropertyDescriptor pd)
return pd.getatrdyn<atrstr,string>(r => r.wartosc);
public static T2 getatrdyn<T,T2>(this System.ComponentModel.PropertyDescriptor pd,Func<T,dynamic> f)
return pd.getAttr<T>().valdyn<T>(f);
public static int? getatrint(this System.ComponentModel.PropertyDescriptor pd)
return pd.getatrdyn<atrint,int?>(r => r.war);
public static int getatrint1(this System.ComponentModel.PropertyDescriptor pd)
return pd.getatrdyn<atrint,int>(r => r.war);
public static System.Nullable<int> getatrint2(this System.ComponentModel.PropertyDescriptor pd)
return pd.getatrdyn<atrstr,System.Nullable<int>>(r=>r.wartoscint);
public static dynamic getwardyn<T>(this System.ComponentModel.PropertyDescriptor pd,object obj,Func<object,dynamic> f)
return new object[1]{pd.GetValue(obj)}.OfType<T>().FirstOrDefault();
public static T war<T>(this object o,string pole)
var prop=o.getprop().Where(p=>p.Equals(pole)).FirstOrDefault();
System.Console.WriteLine("prop.count={0}",prop.GetValue(o));
if (prop==null) throw new System.Exception("blad");
return prop.getwardyn<T>(0,r=>(T)r);
public static void Main()
var k2=new{pole=1,pole2="string",pole3=(int?)4};
var p1 = pd[0].getatrstr();
var p1a = pd[0].getatrint2();
var p2 = pd[1].getatrint();
var p3 = pd[2].getatrint();
Console.WriteLine("Hello World, p1:{0},p2:{1},p1a:{2},p3:{3},v1:{4},v2:{5},k2.getprop.count={6}", p1,p2,p1a,p3,1,2,k2.getprop()[0].GetValue(k2));